aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/extpo/html.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/extpo/html.go')
-rw-r--r--cmd/extpo/html.go114
1 files changed, 114 insertions, 0 deletions
diff --git a/cmd/extpo/html.go b/cmd/extpo/html.go
new file mode 100644
index 0000000..d53cbfa
--- /dev/null
+++ b/cmd/extpo/html.go
@@ -0,0 +1,114 @@
+package main
+
+import (
+ "strings"
+ "text/template/parse"
+)
+
+func processHtml(path string) {
+ trees := make(map[string]*parse.Tree)
+ t := parse.New(path)
+ t.Mode |= parse.ParseComments | parse.SkipFuncCheck
+ try2(t.Parse(string(currentFile), "", "", trees))
+ for _, t := range trees {
+ processHtmlNode(t.Root)
+ }
+}
+
+func processHtmlNode(node parse.Node) {
+ switch n := node.(type) {
+ case *parse.ListNode:
+ for _, m := range n.Nodes {
+ processHtmlNode(m)
+ }
+ case *parse.PipeNode:
+ for _, m := range n.Cmds {
+ processHtmlNode(m)
+ }
+ case *parse.TemplateNode:
+ processHtmlNode(n.Pipe)
+ case *parse.IfNode:
+ processHtmlBranch(n.BranchNode)
+ case *parse.RangeNode:
+ processHtmlBranch(n.BranchNode)
+ case *parse.WithNode:
+ processHtmlBranch(n.BranchNode)
+ case *parse.BranchNode:
+ processHtmlBranch(*n)
+ case *parse.ActionNode:
+ processHtmlNode(n.Pipe)
+ case *parse.CommandNode:
+ if len(n.Args) == 0 {
+ break
+ }
+
+ var funcname string
+ f, ok := n.Args[0].(*parse.FieldNode)
+ if !ok || len(f.Ident) == 0 {
+ ff, ok := n.Args[0].(*parse.VariableNode)
+ if !ok || len(ff.Ident) == 0 {
+ fff, ok := n.Args[0].(*parse.IdentifierNode)
+ if !ok {
+ for _, pipe := range n.Args {
+ processHtmlNode(pipe)
+ }
+ break
+ }
+ funcname = fff.Ident
+ } else {
+ funcname = ff.Ident[len(ff.Ident)-1]
+ }
+ } else {
+ funcname = f.Ident[len(f.Ident)-1]
+ }
+
+ cfg, ok := configs[funcname]
+ if !ok {
+ for _, pipe := range n.Args {
+ processHtmlNode(pipe)
+ }
+ break
+ }
+
+ var (
+ tl translation
+ linenr int
+ )
+
+ if sn, ok := n.Args[cfg.arg].(*parse.StringNode); ok {
+ tl.msgid = sn.Text
+ linenr = getlinenr(sn.Pos.Position())
+ } else {
+ break
+ }
+ if cfg.plural != -1 {
+ if sn, ok := n.Args[cfg.plural].(*parse.StringNode); ok {
+ tl.msgidPlural = sn.Text
+ }
+ }
+ if cfg.context != -1 {
+ if sn, ok := n.Args[cfg.context].(*parse.StringNode); ok {
+ tl.msgctxt = sn.Text
+ }
+ }
+
+ ti := translations[tl]
+ if lastComment != "" {
+ ti.comment = lastComment
+ lastComment = ""
+ }
+ ti.locs = append(ti.locs, loc{currentPath, linenr})
+ translations[tl] = ti
+ case *parse.CommentNode:
+ if strings.HasPrefix(n.Text, "/* TRANSLATORS:") {
+ lastComment = strings.TrimSpace(n.Text[2 : len(n.Text)-2])
+ }
+ }
+}
+
+func processHtmlBranch(n parse.BranchNode) {
+ processHtmlNode(n.List)
+ if n.ElseList != nil {
+ processHtmlNode(n.ElseList)
+ }
+}