main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build ignore
  16. package main
  17. import (
  18. "encoding/json"
  19. "log"
  20. "os"
  21. "regexp"
  22. "strings"
  23. "code.google.com/p/go.net/html"
  24. )
  25. var trans = make(map[string]string)
  26. var attrRe = regexp.MustCompile(`\{\{'([^']+)'\s+\|\s+translate\}\}`)
  27. func generalNode(n *html.Node) {
  28. translate := false
  29. if n.Type == html.ElementNode {
  30. for _, a := range n.Attr {
  31. if a.Key == "translate" {
  32. translate = true
  33. break
  34. } else {
  35. if matches := attrRe.FindStringSubmatch(a.Val); len(matches) == 2 {
  36. translation(matches[1])
  37. }
  38. }
  39. }
  40. } else if n.Type == html.TextNode {
  41. v := strings.TrimSpace(n.Data)
  42. if len(v) > 1 && !(strings.HasPrefix(v, "{{") && strings.HasSuffix(v, "}}")) {
  43. log.Println("Untranslated text node:")
  44. log.Print("\t" + v)
  45. }
  46. }
  47. for c := n.FirstChild; c != nil; c = c.NextSibling {
  48. if translate {
  49. inTranslate(c)
  50. } else {
  51. generalNode(c)
  52. }
  53. }
  54. }
  55. func inTranslate(n *html.Node) {
  56. if n.Type == html.TextNode {
  57. translation(n.Data)
  58. } else {
  59. log.Println("translate node with non-text child <")
  60. log.Println(n)
  61. }
  62. if n.FirstChild != nil {
  63. log.Println("translate node has children:")
  64. log.Println(n.Data)
  65. }
  66. }
  67. func translation(v string) {
  68. v = strings.TrimSpace(v)
  69. if _, ok := trans[v]; !ok {
  70. av := strings.Replace(v, "{%", "{{", -1)
  71. av = strings.Replace(av, "%}", "}}", -1)
  72. trans[v] = av
  73. }
  74. }
  75. func main() {
  76. fd, err := os.Open(os.Args[1])
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. err = json.NewDecoder(fd).Decode(&trans)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. fd.Close()
  85. fd, err = os.Open(os.Args[2])
  86. if err != nil {
  87. log.Fatal(err)
  88. }
  89. doc, err := html.Parse(fd)
  90. if err != nil {
  91. log.Fatal(err)
  92. }
  93. fd.Close()
  94. generalNode(doc)
  95. bs, err := json.MarshalIndent(trans, "", " ")
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. os.Stdout.Write(bs)
  100. os.Stdout.WriteString("\n")
  101. }