-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMustacheNode.swift
312 lines (249 loc) · 7.88 KB
/
MustacheNode.swift
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// MustacheNode.swift
// Noze.io
//
// Created by Helge Heß on 6/1/16.
// Copyright © 2016 ZeeZide GmbH. All rights reserved.
//
/// One node of the Mustache template. A template is parsed into a tree of the
/// various nodes.
public enum MustacheNode {
case Empty
/// Represents the top-level node of a Mustache template, contains the list
/// of nodes.
case Global([ MustacheNode])
/// Regular CDATA in the template
case Text(String)
/// A section, can be either a repetition (if the value evaluates to a
/// Sequence) or a conditional (if the value resolves to true/false).
/// If the value is false or an empty list, the contained nodes won't be
/// rendered.
/// If the value is a Sequence, the contained items will be rendered n-times,
/// once for each member. The rendering context is changed to the item before
/// rendering.
/// If the value is not a Sequence, but considered 'true', the contained nodes
/// will get rendered once.
///
/// A Mustache section is introduced with a "{{#" tag and ends with a "{{/"
/// tag.
/// Example:
///
/// {{#addresses}}
/// Has address in: {{city}}
/// {{/addresses}}
///
case Section(String, [ MustacheNode ])
/// An inverted section either displays its contents or not, it never repeats.
///
/// If the value is 'false' or an empty list, the contained nodes will get
/// rendered.
/// If it is 'true' or a non-empty list, it won't get rendered.
///
/// An inverted section is introduced with a "{{^" tag and ends with a "{{/"
/// tag.
/// Example:
///
/// {{^addresses}}
/// The person has no addresses assigned.
/// {{/addresses}}
///
case InvertedSection(String, [ MustacheNode ])
/// A Mustache Variable. Will try to lookup the given string as a name in the
/// current context. If the current context doesn't have the name, the lookup
/// is supposed to continue at the parent contexts.
///
/// The resulting value will be HTML escaped.
///
/// Example:
///
/// {{city}}
///
case Tag(String)
/// This is the same like Tag, but the value won't be HTML escaped.
///
/// Use triple braces for unescaped variables:
///
/// {{{htmlToEmbed}}}
///
/// Or use an ampersand, like so:
///
/// {{^ htmlToEmbed}}
///
case UnescapedTag(String)
/// A partial. How this is looked up depends on the rendering context
/// implementation.
///
/// Partials are included via "{{>", like so:
///
/// {{#names}}
/// {{> user}}
/// {{/names}}
///
case Partial(String)
}
// MARK: - Rendering
public extension MustacheNode {
func render(object o: Any?, cb: ( String ) -> Void) {
let ctx = MustacheDefaultRenderingContext(o)
render(inContext: ctx, cb: cb)
}
func render(object o: Any?) -> String {
let ctx = MustacheDefaultRenderingContext(o)
render(inContext: ctx)
return ctx.string
}
func render(nodes nl : [MustacheNode],
inContext ctx : MustacheRenderingContext)
{
nl.forEach { node in node.render(inContext: ctx) }
}
func render(inContext ctx: MustacheRenderingContext,
cb: ( String ) -> Void)
{
render(inContext: ctx) // TODO: make async for partials
cb(ctx.string)
}
func render(inContext ctx: MustacheRenderingContext) {
switch self {
case .Empty: return
case .Global(let nodes):
render(nodes: nodes, inContext: ctx)
case .Text(let text):
ctx.append(string: text)
case .Section(let tag, let nodes):
render(section: tag, nodes: nodes, inContext: ctx)
case .InvertedSection(let tag, let nodes):
let v = ctx.value(forTag: tag)
guard !ctx.isMustacheTrue(value: v) else { return }
render(nodes: nodes, inContext: ctx)
case .Tag(let tag):
if let v = ctx.value(forTag: tag) {
if let s = v as? String {
ctx.append(string: ctx.escape(string: s))
}
else {
ctx.append(string: ctx.escape(string: "\(v)"))
}
}
case .UnescapedTag(let tag):
if let v = ctx.value(forTag: tag) {
if let s = v as? String {
ctx.append(string: s)
}
else {
ctx.append(string: "\(v)")
}
}
case .Partial(let name):
guard let partial = ctx.retrievePartial(name: name) else { return }
partial.render(inContext: ctx)
}
}
func render(lambda cb : MustacheRenderingFunction,
nodes nl : [ MustacheNode ],
inContext ctx : MustacheRenderingContext)
{
let mustache = nl.asMustacheString
let result = cb(mustache) { mustacheToRender in
let tree : MustacheNode
if mustache == mustacheToRender { // slow, lame
tree = MustacheNode.Global(nl)
}
else { // got a new sub-template to render by the callback
let parser = MustacheParser()
tree = parser.parse(string: mustache)
}
let lambdaCtx = ctx.newLambdaContext()
tree.render(inContext: lambdaCtx)
return lambdaCtx.string
}
ctx.append(string: result)
}
}
// MARK: - Convert parsed nodes back to a String template
public extension MustacheNode {
var asMustacheString : String {
var s = String()
self.append(toString: &s)
return s
}
}
public extension MustacheNode {
func render(section tag: String, nodes : [ MustacheNode ],
inContext ctx: MustacheRenderingContext)
{
let v = ctx.value(forTag: tag)
guard let vv = v else { return } // nil
// Is it a rendering function?
if let cb = v as? MustacheRenderingFunction {
render(lambda: cb, nodes: nodes, inContext: ctx)
return
}
else if let cb = vv as? MustacheSimpleRenderingFunction {
render(lambda: { text, _ in return cb(text) },
nodes: nodes, inContext: ctx)
}
// Is it a plain false?
guard ctx.isMustacheTrue(value: vv) else { return }
// Reflect on section value
let mirror = Mirror(reflecting: vv)
let ds = mirror.displayStyle
if ds == nil { // e.g. Bool in Swift 3
render(nodes: nodes, inContext: ctx)
return
}
switch ds! {
case .collection:
for ( _, value ) in mirror.children {
ctx.enter(scope: value)
render(nodes: nodes, inContext: ctx)
ctx.leave()
}
case .class, .dictionary: // adjust cursor
if ctx.isFoundationBaseType(value: vv) {
render(nodes: nodes, inContext: ctx)
}
else {
ctx.enter(scope: vv)
render(nodes: nodes, inContext: ctx)
ctx.leave()
}
default:
// keep cursor for non-collections?
render(nodes: nodes, inContext: ctx)
}
}
}
// MARK: - Convert parsed nodes back to a String template
public extension MustacheNode {
func append(toString s : inout String) {
switch self {
case .Empty: return
case .Text(let text):
s += text
case .Global(let nodes):
nodes.forEach { $0.append(toString: &s) }
case .Section(let key, let nodes):
s += "{{#\(key)}}"
nodes.forEach { $0.append(toString: &s) }
s += "{{/\(key)}}"
case .InvertedSection(let key, let nodes):
s += "{{^\(key)}}"
nodes.forEach { $0.append(toString: &s) }
s += "{{/\(key)}}"
case .Tag(let key):
s += "{{\(key)}}"
case .UnescapedTag(let key):
s += "{{{\(key)}}}"
case .Partial(let key):
s += "{{> \(key)}}"
}
}
}
public extension Sequence where Iterator.Element == MustacheNode {
var asMustacheString : String {
var s = String()
forEach { $0.append(toString: &s) }
return s
}
}