diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2025-11-03 15:51:56 +0100 |
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2025-11-03 15:51:56 +0100 |
| commit | ef4e140949408a917b45cc253dd38d229e34f46b (patch) | |
| tree | f0b877c76e94b84b7c55cf2bf54a5bd280ddb358 /cmd/extpo/html.go | |
| parent | 15f841ab43fd9b431e93d2e870c23ae7695929cc (diff) | |
Support extracting translations from SQL scripts
Diffstat (limited to 'cmd/extpo/html.go')
| -rw-r--r-- | cmd/extpo/html.go | 114 |
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) + } +} |