aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/extpo/main.go
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2025-11-03 15:51:56 +0100
committerThomas Voss <mail@thomasvoss.com> 2025-11-03 15:51:56 +0100
commitef4e140949408a917b45cc253dd38d229e34f46b (patch)
treef0b877c76e94b84b7c55cf2bf54a5bd280ddb358 /cmd/extpo/main.go
parent15f841ab43fd9b431e93d2e870c23ae7695929cc (diff)
Support extracting translations from SQL scripts
Diffstat (limited to 'cmd/extpo/main.go')
-rw-r--r--cmd/extpo/main.go109
1 files changed, 5 insertions, 104 deletions
diff --git a/cmd/extpo/main.go b/cmd/extpo/main.go
index 6aa1503..5db7b7c 100644
--- a/cmd/extpo/main.go
+++ b/cmd/extpo/main.go
@@ -140,110 +140,11 @@ msgstr ""
func process(path string) {
currentPath = path
currentFile = try2(os.ReadFile(path))
- 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 {
- processNode(t.Root)
- }
-}
-
-func processNode(node parse.Node) {
- switch n := node.(type) {
- case *parse.ListNode:
- for _, m := range n.Nodes {
- processNode(m)
- }
- case *parse.PipeNode:
- for _, m := range n.Cmds {
- processNode(m)
- }
- case *parse.TemplateNode:
- processNode(n.Pipe)
- case *parse.IfNode:
- processBranch(n.BranchNode)
- case *parse.RangeNode:
- processBranch(n.BranchNode)
- case *parse.WithNode:
- processBranch(n.BranchNode)
- case *parse.BranchNode:
- processBranch(*n)
- case *parse.ActionNode:
- processNode(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 {
- processNode(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 {
- processNode(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 processBranch(n parse.BranchNode) {
- processNode(n.List)
- if n.ElseList != nil {
- processNode(n.ElseList)
+ switch {
+ case strings.HasSuffix(path, ".html.tmpl"):
+ processHtml(path)
+ case strings.HasSuffix(path, ".sql"):
+ processSql(path)
}
}