-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_cfgr.go
169 lines (165 loc) · 3.89 KB
/
template_cfgr.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
package go2html
import (
"fmt"
"github.com/Contra-Culture/go2html/fragments"
"github.com/Contra-Culture/go2html/node"
)
type (
TemplateCfgr struct {
template *Template
parentContext *fragments.Context
}
)
func (tcp *TemplateCfgr) Elem(
name string,
configureSelf func(*ElemCfgr),
configureNested func(*NestedNodesCfgr),
) {
node := node.New(node.ELEM_NODE_KIND, []string{name})
tcp.template.nodes = append(tcp.template.nodes, node)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(fmt.Sprintf("<%s", name))
configureSelf(
&ElemCfgr{
tcp: tcp,
node: node,
context: c,
})
typ := elemTyp(name)
if typ == VOID_ELEM_TYPE {
c.Append("/>")
return
}
c.Append(">")
configureNested(
&NestedNodesCfgr{
tcp: tcp,
parent: node,
context: c,
})
c.Append(fmt.Sprintf("</%s>", name))
})
}
func (tcp *TemplateCfgr) Template(key string, t *Template) {
if len(key) == 0 {
key = t.key
}
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.TEMPLATE_NODE_KIND, []string{key}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(
&Template{
key: key,
nodes: t.nodes,
fragments: t.fragments,
})
})
}
func (tcp *TemplateCfgr) TemplateInjection(key string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.TEMPLATE_INJECTION_NODE_KIND, []string{key}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(templateInjection(key))
})
}
func (tcp *TemplateCfgr) Comment(text string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.COMMENT_NODE_KIND, []string{}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(fmt.Sprintf("<!-- %s -->", text))
})
}
func (tcp *TemplateCfgr) Doctype() {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.DOCTYPE_NODE_KIND, []string{}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append("<!DOCTYPE html>")
})
}
func (tcp *TemplateCfgr) TextInjection(key string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.TEXT_INJECTION_NODE_KIND, []string{key}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(
injection{
key: key,
modifiers: []func(string) string{
HTMLEscape,
},
})
})
}
func (tcp *TemplateCfgr) UnsafeTextInjection(key string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.UNSAFE_TEXT_INJECTION_NODE_KIND, []string{key}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(
injection{
key: key,
})
})
}
func (tcp *TemplateCfgr) Text(text string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.TEXT_NODE_KIND, []string{}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(safeTextReplacer.Replace(text))
})
}
func (tcp *TemplateCfgr) UnsafeText(text string) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.UNSAFE_TEXT_NODE_KIND, []string{}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(text)
})
}
func (tcp *TemplateCfgr) Repeat(key string, t *Template) {
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.REPEAT_NODE_KIND, []string{key}),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(
repetition{
key: key,
template: t,
})
})
}
func (tcp *TemplateCfgr) Variants(vars map[string]*Template, dv *Template) {
keys := []string{}
for k, v := range vars {
if v != nil {
keys = append(keys, k)
continue
}
panic(fmt.Sprintf("variant %s has no specified template", k))
}
tcp.template.nodes = append(
tcp.template.nodes,
node.New(node.VARIANTS_NODE_KIND, keys),
)
tcp.template.fragments.InContext(func(c *fragments.Context) {
c.Append(
variants{
variants: vars,
defaultVariant: dv,
})
})
}