aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/extpo/html.go
blob: d53cbfa7e76601d002e3b74e3a4e7d636995e5c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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)
	}
}