2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-07-31 10:30:19 +00:00
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
2014-07-20 11:49:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-08-06 12:41:46 +00:00
|
|
|
"regexp"
|
2014-07-20 11:49:26 +00:00
|
|
|
"strings"
|
|
|
|
|
2014-11-29 23:17:00 +00:00
|
|
|
"golang.org/x/net/html"
|
2014-07-20 11:49:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var trans = make(map[string]string)
|
2014-08-06 12:41:46 +00:00
|
|
|
var attrRe = regexp.MustCompile(`\{\{'([^']+)'\s+\|\s+translate\}\}`)
|
2014-07-20 11:49:26 +00:00
|
|
|
|
|
|
|
func generalNode(n *html.Node) {
|
|
|
|
translate := false
|
|
|
|
if n.Type == html.ElementNode {
|
|
|
|
for _, a := range n.Attr {
|
|
|
|
if a.Key == "translate" {
|
|
|
|
translate = true
|
|
|
|
break
|
2014-08-06 12:41:46 +00:00
|
|
|
} else {
|
|
|
|
if matches := attrRe.FindStringSubmatch(a.Val); len(matches) == 2 {
|
|
|
|
translation(matches[1])
|
|
|
|
}
|
2014-07-20 11:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if n.Type == html.TextNode {
|
|
|
|
v := strings.TrimSpace(n.Data)
|
|
|
|
if len(v) > 1 && !(strings.HasPrefix(v, "{{") && strings.HasSuffix(v, "}}")) {
|
|
|
|
log.Println("Untranslated text node:")
|
|
|
|
log.Print("\t" + v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
if translate {
|
|
|
|
inTranslate(c)
|
|
|
|
} else {
|
|
|
|
generalNode(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func inTranslate(n *html.Node) {
|
|
|
|
if n.Type == html.TextNode {
|
2014-08-06 12:41:46 +00:00
|
|
|
translation(n.Data)
|
2014-07-20 11:49:26 +00:00
|
|
|
} else {
|
|
|
|
log.Println("translate node with non-text child <")
|
|
|
|
log.Println(n)
|
|
|
|
}
|
|
|
|
if n.FirstChild != nil {
|
|
|
|
log.Println("translate node has children:")
|
|
|
|
log.Println(n.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 12:41:46 +00:00
|
|
|
func translation(v string) {
|
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if _, ok := trans[v]; !ok {
|
|
|
|
av := strings.Replace(v, "{%", "{{", -1)
|
|
|
|
av = strings.Replace(av, "%}", "}}", -1)
|
|
|
|
trans[v] = av
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 11:49:26 +00:00
|
|
|
func main() {
|
|
|
|
fd, err := os.Open(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(fd).Decode(&trans)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
fd, err = os.Open(os.Args[2])
|
2014-07-20 11:49:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
doc, err := html.Parse(fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
2014-07-20 11:49:26 +00:00
|
|
|
generalNode(doc)
|
|
|
|
bs, err := json.MarshalIndent(trans, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Stdout.Write(bs)
|
2014-07-31 07:08:08 +00:00
|
|
|
os.Stdout.WriteString("\n")
|
2014-07-20 11:49:26 +00:00
|
|
|
}
|