-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ztpl.go
181 lines (161 loc) · 3.66 KB
/
ztpl.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package internal
import (
"errors"
htmlTemplate "html/template"
"io"
"sort"
"strings"
"sync"
textTemplate "text/template"
"text/template/parse"
)
var Templates = new(LockedTpl)
type (
Template interface {
ExecuteTemplate(io.Writer, string, interface{}) error
Lookup(string) Template
GetTree(string) *parse.Tree
TemplateNames() []string
}
// LockedTpl adds a lock to template.Template for reloading in dev without
// race conditions.
LockedTpl struct {
sync.Mutex
html Template
text Template
}
htmlWrap struct{ *htmlTemplate.Template }
textWrap struct{ *textTemplate.Template }
)
func (t htmlWrap) GetTree(n string) *parse.Tree { return t.Template.Lookup(n).Tree }
func (t textWrap) GetTree(n string) *parse.Tree { return t.Template.Lookup(n).Tree }
func (t htmlWrap) Lookup(n string) Template {
tt := t.Template.Lookup(n)
if tt == nil {
return nil
}
return htmlWrap{tt}
}
func (t textWrap) Lookup(n string) Template {
tt := t.Template.Lookup(n)
if tt == nil {
return nil
}
return textWrap{tt}
}
func (t htmlWrap) TemplateNames() []string {
tpl := t.Template.Templates()
names := make([]string, 0, len(tpl))
for _, n := range tpl {
names = append(names, n.Name())
}
return names
}
func (t textWrap) TemplateNames() []string {
tpl := t.Template.Templates()
names := make([]string, 0, len(tpl))
for _, n := range tpl {
names = append(names, n.Name())
}
return names
}
func (t *LockedTpl) Set(html *htmlTemplate.Template, text *textTemplate.Template) {
t.Lock()
defer t.Unlock()
if html == nil {
html = htmlTemplate.New("")
}
t.html = htmlWrap{html}
if text == nil {
text = textTemplate.New("")
}
t.text = textWrap{text}
}
func (t *LockedTpl) Has(name string) bool {
t.Lock()
defer t.Unlock()
if t.html == nil && t.text == nil {
return false
}
if t.html.Lookup(name) != nil {
return true
}
return t.text.Lookup(name) != nil
}
func (t *LockedTpl) List() []string {
t.Lock()
defer t.Unlock()
l := append(t.html.TemplateNames(), t.text.TemplateNames()...)
sort.Strings(l)
return l
}
type Trace map[string][]interface{}
func (t Trace) Names() []string {
names := make([]string, 0, len(t))
for tt := range t {
names = append(names, tt)
}
sort.Strings(names)
return names
}
var (
TraceEnabled bool
Ran Trace
)
func (t *LockedTpl) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
t.Lock()
defer t.Unlock()
if t.html == nil && t.text == nil {
return errors.New("ztpl.ExecuteTemplate: not initialized; call ztpl.Init()")
}
var tpl Template = t.html
if strings.HasSuffix(name, ".gotxt") {
tpl = t.text
}
if TraceEnabled {
Ran[name] = append(Ran[name], data)
invoks := make(map[string]struct{})
for _, n := range tpl.GetTree(name).Root.Nodes {
for _, k := range lsTpl(tpl, n) {
invoks[k] = struct{}{}
}
}
for k := range invoks {
// TODO: need to process TemplateNode.Pipe to get the data
Ran[k] = append(Ran[k], nil)
}
}
return tpl.ExecuteTemplate(wr, name, data)
}
func lsTpl(t Template, n parse.Node) []string {
var (
tpls []string
nodes []parse.Node
)
switch nn := n.(type) {
case *parse.TemplateNode:
tpls = append(tpls, nn.Name)
for _, n2 := range t.GetTree(nn.Name).Root.Nodes {
tpls = append(tpls, lsTpl(t, n2)...)
}
case *parse.IfNode:
if nn.List != nil {
nodes = append(nodes, nn.List.Nodes...)
}
if nn.ElseList != nil {
nodes = append(nodes, nn.ElseList.Nodes...)
}
case *parse.ListNode:
nodes = nn.Nodes
case *parse.CommandNode:
nodes = nn.Args
case *parse.PipeNode:
for _, n2 := range nn.Cmds {
nodes = append(nodes, n2.Args...)
}
}
for _, n2 := range nodes {
tpls = append(tpls, lsTpl(t, n2)...)
}
return tpls
}