-
Notifications
You must be signed in to change notification settings - Fork 154
/
Configuration.swift
304 lines (288 loc) · 11.4 KB
/
Configuration.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
// The MIT License
//
// Copyright (c) 2015 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/// Configuration exposes properties that affect both the parsing and the
/// rendering of Mustache templates.
///
///
/// ### What can be configured
///
/// Configuration covers:
///
/// - **Content type**: HTML templates escape rendered strings, while Text
/// templates do not. Text templates are HTML-escaped as a whole when included
/// in HTML templates.
///
/// - **Context stack**: values stored in a Configuration's context are readily
/// available to templates.
///
/// - **Tag delimiters**: default Mustache delimiters are `{{` and `}}`. These
/// are configurable.
///
///
/// ### Usage
///
/// You setup a configuration *before* loading templates:
///
/// // Template loaded later will not HTML-escape the rendered strings:
/// Mustache.DefaultConfiguration.contentType = .text
///
/// // A text template
/// let template = try! Template(string: "...")
///
///
/// ### Configuration levels
///
/// There are three levels of configuration:
///
/// `Mustache.DefaultConfiguration` is a global variable that applies
/// by default:
///
/// Mustache.DefaultConfiguration.contentType = .text
///
/// // A text template
/// let template = try! Template(named: "Document")
///
/// `TemplateRepository.configuration` only applies to templates loaded from the
/// template repository:
///
/// let repository = TemplateRepository(directoryPath: "/path/to/templates")
/// repository.configuration.contentType = .text
///
/// // A text template
/// let template = try! repository.template(named: "Document")
///
/// Templates can also be configured individually. See the documentation of each
/// Configuration method for more details.
public struct Configuration {
/// The default configuration that is used unless specified otherwise by a
/// TemplateRepository.
public static var `default`: Configuration {
Configuration()
}
// =========================================================================
// MARK: - Factory Configuration
/// Creates a factory configuration.
///
/// Its contentType is HTML, baseContext empty, tag delimiters `{{` and `}}`.
///
/// For example:
///
/// // Make sure the template repository uses factory configuration,
/// // regardless of changes made to `Mustache.DefaultConfiguration`:
/// let repository = TemplateRepository(directoryPath: "/path/to/templates")
/// repository.configuration = Configuration()
public init() {
contentType = .html
baseContext = Context()
tagDelimiterPair = ("{{", "}}")
}
// =========================================================================
// MARK: - Content Type
/// The content type of strings rendered by templates built with this
/// configuration.
///
/// It affects the HTML-escaping of your data:
///
/// - The `.html` content type has templates render HTML. This is the
/// default behavior. HTML template escape the input of variable tags such
/// as `{{name}}`. Use triple mustache tags `{{{content}}}` in order to
/// avoid HTML-escaping.
///
/// - The `.text` content type has templates render text. They do not
/// HTML-escape their input: `{{name}}` and `{{{name}}}` have identical,
/// non-escaped, renderings.
///
/// GRMustache safely keeps track of the content type of templates: should a
/// HTML template embed a text template, the content of the text template
/// would be HTML-escaped, as a whole.
///
/// Setting the contentType of a configuration affects the contentType of
/// all templates loaded afterwards:
///
/// // Globally, with Mustache.DefaultConfiguration:
///
/// Mustache.DefaultConfiguration.contentType = .text
/// let textTemplate = try! Template(named: "Script")
///
/// // Locally, using a TemplateRepository:
///
/// let repository = TemplateRepository(bundle: Bundle.main)
/// repository.configuration.contentType = .html
/// let HTMLTemplate = try! repository.template(named: "HTMLDocument")
///
/// In order to set the content type of an individual templates, use pragma tags
/// right in the content of your templates:
///
/// - `{{% CONTENT_TYPE:TEXT }}` turns a template into a text template.
/// - `{{% CONTENT_TYPE:HTML }}` turns a template into a HTML template.
///
/// For example:
///
/// {{! This template renders a bash script. }}
/// {{% CONTENT_TYPE:TEXT }}
/// export LANG={{ENV.LANG}}
/// ...
///
/// These pragmas must be found early in the template (before any value
/// tag). Should several pragmas be found in a template content, the last
/// one wins.
public var contentType: ContentType
// =========================================================================
// MARK: - Context Stack
/// The base context for templates rendering. All templates built with this
/// configuration can access values stored in the base context.
///
/// The default base context is empty.
///
/// You can set it to some custom context, or extend it with the
/// `extendBaseContext(_)` and `register(_:forKey:)` methods.
///
/// // Globally, with Mustache.DefaultConfiguration:
///
/// Mustache.DefaultConfiguration.baseContext = Context(["foo": "bar"])
///
/// // "bar"
/// let template1 = try! Template(string: "{{foo}}")
/// try! template1.render()
///
/// // Locally, using a TemplateRepository:
///
/// let repository = TemplateRepository(bundle: Bundle.main)
/// repository.configuration.baseContext = Context(["foo": "bar"])
///
/// // "bar"
/// let template2 = try! repository.template(string: "{{foo}}")
/// try! template2.render()
///
/// The base context can also be set for individual templates:
///
/// let template3 = try! Template(string: "{{foo}}")
/// template3.baseContext = Context(["foo": "bar"])
///
/// // "bar"
/// try! template3.render()
///
/// - seealso: extendBaseContext(_)
/// - seealso: register(_:forKey:)
public var baseContext: Context
/// Extends the base context with the provided value. All templates built
/// with this configuration can access the value.
///
/// // Globally, with Mustache.DefaultConfiguration:
///
/// Mustache.DefaultConfiguration.extendBaseContext(["foo": "bar"])
///
/// // "bar"
/// let template1 = try! Template(string: "{{foo}}")
/// try! template1.render()
///
/// // Locally, using a TemplateRepository:
///
/// let repository = TemplateRepository(bundle: Bundle.main)
/// repository.configuration.extendBaseContext(["foo": "bar"])
///
/// // "bar"
/// let template2 = try! repository.template(string: "{{foo}}")
/// try! template2.render()
///
/// The base context can also be extended for individual templates:
///
/// let template3 = try! Template(string: "{{foo}}")
/// template3.extendBaseContext(["foo": "bar"])
///
/// // "bar"
/// try! template3.render()
///
/// - parameter value: The value pushed on the top of the context stack.
///
/// - seealso: baseContext
/// - seealso: register(_:forKey:)
public mutating func extendBaseContext(_ value: Any?) {
baseContext = baseContext.extendedContext(value)
}
/// Registers a key/value pair in the base context. All renderings will be
/// able to access the provided value through this key.
///
/// Registered keys are looked up first when evaluating Mustache tags.
///
/// // Globally, with Mustache.DefaultConfiguration:
///
/// Mustache.DefaultConfiguration.register("bar", forKey: "foo")
///
/// // Renders "bar"
/// let template1 = try! Template(string: "{{foo}}")
/// try! template1.render()
///
/// // Renders "bar" again, because the registered value "bar" has priority.
/// try! template1.render(["foo": "qux"])
///
/// // Locally, using a TemplateRepository:
///
/// let repository = TemplateRepository(bundle: Bundle.main)
/// repository.configuration.register("bar", forKey: "foo")
///
/// // "bar"
/// let template2 = try! repository.template(string: "{{foo}}")
/// try! template2.render()
///
/// Keys can also be registered in the base context of individual templates:
///
/// let template3 = try! Template(string: "{{foo}}")
/// template3.register("bar", forKey: "foo")
///
/// // "bar"
/// try! template3.render()
///
///
/// - parameter key: An identifier.
/// - parameter value: The value registered for *key*.
///
/// - seealso: baseContext
/// - seealso: extendBaseContext(_)
public mutating func register(_ value: Any?, forKey key: String) {
baseContext = baseContext.extendedContext(withRegisteredValue: value, forKey: key)
}
// =========================================================================
// MARK: - Tag delimiters
/// The delimiters for Mustache tags. All templates built with this
/// configuration are parsed using those delimiters.
///
/// The default value is `("{{", "}}")`.
///
/// Setting the tagDelimiterPair of a configuration affects all templates
/// loaded afterwards:
///
/// // Globally, with Mustache.DefaultConfiguration:
///
/// Mustache.DefaultConfiguration.tagDelimiterPair = ("<%", "%>")
/// let template = try! Template(string: "<% name %>)
///
/// // Locally, using a TemplateRepository:
///
/// let repository = TemplateRepository()
/// repository.configuration.tagDelimiterPair = ("[[", "]]")
/// let template = try! repository.template(string: "[[ name ]]")
///
/// You can also change the delimiters right in your templates using a "Set
/// Delimiter tag": `{{=[[ ]]=}}` changes delimiters to `[[` and `]]`.
public var tagDelimiterPair: TagDelimiterPair
}