-
Notifications
You must be signed in to change notification settings - Fork 10
/
template.go
226 lines (187 loc) · 4.24 KB
/
template.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2012-2017, Rolf Veen and contributors.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ogdl
import (
"bytes"
)
// NewTemplate parses a text template given as a string and converts it to a Graph.
// Templates have fixed and variable parts. Variables all begin with '$'.
//
// A template is a text file in any format: plain text, HTML, XML, OGDL or
// whatever. The dolar sign acts as an escape character that switches from the
// text to the variable plane. Parsed templates are converted back to text
// by evaluating the variable parts against a Graph object, by means of the
// Process() method.
//
// Template grammar
//
// template ::= ( text | variable )*
//
// variable ::= ('$' path) | ('$' '(' expression ')') | ('$' '{' expression '}')
// path ::= as defined in path.go
// expression ::= as defined in expression.go
//
// Some variables act as directives: $if, $else, $end, $for, $break.
//
// $if(expression)
// $else
// $end
//
// $for(destPath,sourcepath)
// $break
// $end
//
func NewTemplate(s string) *Graph {
p := newStringParser(s)
p.Template()
t := p.graphTop(TypeTemplate)
t.ast()
t.simplify()
t.flow()
return t
}
// NewTemplateBytes has the same function as NewTemplate except that the input stream
// is a byte array.
func NewTemplateFromBytes(b []byte) *Graph {
p := newBytesParser(b)
p.Template()
t := p.graphTop(TypeTemplate)
t.ast()
t.simplify()
t.flow()
return t
}
// Process processes the parsed template, returning the resulting text in a byte array.
// The variable parts are resolved out of the Graph given.
func (tpl *Graph) Process(ctx *Graph) []byte {
buffer := &bytes.Buffer{}
tpl.process(ctx, buffer)
return buffer.Bytes()
}
func (g *Graph) process(c *Graph, buffer *bytes.Buffer) bool {
if g == nil || g.Out == nil {
return false
}
falseIf := false
for _, n := range g.Out {
s := n.ThisString()
switch s {
case TypePath:
i := c.Eval(n)
buffer.WriteString(_text(i))
case TypeExpression:
// Silent evaluation
c.Eval(n)
case TypeIf:
// evaluate the expression
b := c.evalBool(n.GetAt(0).GetAt(0))
if b {
n.GetAt(1).process(c, buffer)
falseIf = false
} else {
falseIf = true
}
case TypeElse:
// if there was a previous if evaluating to false:
if falseIf {
n.process(c, buffer)
falseIf = false
}
case TypeFor:
// The first subnode (of !g) is a path
// The second is an expression evaluating to a list of elements
i := c.Eval(n.GetAt(0).GetAt(1))
// Check that i is iterable
gi, ok := i.(*Graph)
if !ok || gi == nil {
continue
}
// The third is the subtemplate to travel
// println ("for type: ",reflect.TypeOf(i).String(), "ok",ok)
// Assing expression value to path
// XXX if not Graph
varname := n.GetAt(0).GetAt(0).GetAt(0).String()
c.Delete(varname)
it := c.Add(varname)
for _, ee := range gi.Out {
it.Out = nil
it.Add(ee)
brk := n.GetAt(1).process(c, buffer)
if brk {
break
}
}
case TypeBreak:
return true
default:
buffer.WriteString(n.ThisString())
}
}
return false
}
// simplify converts !p TYPE in !TYPE for keywords if, end, else, for and break.
func (g *Graph) simplify() {
if g == nil {
return
}
for _, node := range g.Out {
if TypePath == node.ThisString() {
s := node.GetAt(0).ThisString()
switch s {
case "if":
node.This = TypeIf
node.DeleteAt(0)
case "end":
node.This = TypeEnd
node.DeleteAt(0)
case "else":
node.This = TypeElse
node.DeleteAt(0)
case "for":
node.This = TypeFor
node.DeleteAt(0)
case "break":
node.This = TypeBreak
node.DeleteAt(0)
}
}
}
}
// flow nests 'if' and 'for' loops.
func (g *Graph) flow() {
n := 0
var nod *Graph
for i := 0; i < g.Len(); i++ {
node := g.Out[i]
s := node.ThisString()
if s == TypeIf || s == TypeFor {
n++
if n == 1 {
nod = node.Add(TypeTemplate)
continue
}
}
if s == TypeElse {
if n == 1 {
nod.flow()
nod = node
continue
}
}
if s == TypeEnd {
n--
if n == 0 {
nod.flow()
g.DeleteAt(i)
i--
continue
}
}
if n > 0 {
nod.Add(node)
g.DeleteAt(i)
i--
}
}
}