-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
317 lines (274 loc) · 9 KB
/
index.js
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
313
314
315
316
317
/**
* Copyright (c) 2012-present, The Dojo Foundation .<http://dojofoundation.org/>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <https://lodash.com/license>
* Copyright (c) 2015-present, Jon Schlinkert.
*/
'use strict';
const assign = require('assign-deep');
const utils = require('./utils');
/**
* Create an instance of `Engine` with the given options.
*
* ```js
* const Engine = require('engine');
* const engine = new Engine();
* ```
* @param {Object} `options`
* @api public
*/
class Engine {
constructor(options = {}) {
this.options = options;
this.imports = this.options.imports || {};
this.options.variable = '';
this.settings = {};
this.counter = 0;
this.cache = {};
// regex
this.options.escape = this.options.escape || utils.reEscape;
this.options.evaluate = this.options.evaluate || utils.reEvaluate;
this.options.interpolate = this.options.interpolate || utils.reInterpolate;
// register helpers
if (this.options.helpers) {
this.helpers(this.options.helpers);
}
// load data
if (this.options.data) {
this.data(this.options.data);
}
}
/**
* Register a template helper.
*
* ```js
* engine.helper('upper', function(str) {
* return str.toUpperCase();
* });
*
* engine.render('<%= upper(user) %>', {user: 'doowb'});
* //=> 'DOOWB'
* ```
* @param {String} `prop`
* @param {Function} `fn`
* @return {Object} Instance of `Engine` for chaining
* @api public
*/
helper(prop, fn) {
if (utils.isObject(prop)) {
this.helpers(prop);
} else {
this.imports[prop] = fn;
}
return this;
}
/**
* Register an object of template helpers.
*
* ```js
* engine.helpers({
* upper: function(str) {
* return str.toUpperCase();
* },
* lower: function(str) {
* return str.toLowerCase();
* }
* });
*
* // Or, just require in `template-helpers`
* engine.helpers(require('template-helpers')._);
* ```
* @param {Object|Array} `helpers` Object or array of helper objects.
* @return {Object} Instance of `Engine` for chaining
* @api public
*/
helpers(helpers) {
if (utils.isObject(helpers)) {
for (let key of Object.keys(helpers)) {
this.helper(key, helpers[key]);
}
}
return this;
}
/**
* Add data to be passed to templates as context.
*
* ```js
* engine.data({first: 'Brian'});
* engine.render('<%= last %>, <%= first %>', {last: 'Woodward'});
* //=> 'Woodward, Brian'
* ```
* @param {String|Object} `key` Property key, or an object
* @param {any} `value` If key is a string, this can be any typeof value
* @return {Object} Engine instance, for chaining
* @api public
*/
data(prop, value) {
if (typeof prop === 'object') {
if (utils.isObject(prop)) {
for (let key of Object.keys(prop)) {
this.data(key, prop[key]);
}
}
} else {
if (!this.cache.data) this.cache.data = {};
this.cache.data[prop] = value;
}
return this;
}
/**
* Generate the regex to use for matching template variables.
* @param {Object} `opts`
* @return {RegExp}
*/
_regex(options) {
let opts = Object.assign({}, this.options, options);
if (!opts.interpolate && !opts.regex && !opts.escape && !opts.evaluate) {
return utils.delimiters;
}
let interpolate = opts.interpolate || utils.reNoMatch;
if (opts.regex instanceof RegExp) {
interpolate = opts.regex;
}
let reString = (opts.escape || utils.reNoMatch).source
+ '|' + interpolate.source
+ '|' + (interpolate === utils.reInterpolate ? utils.reEsTemplate : utils.reNoMatch).source
+ '|' + (opts.evaluate || utils.reNoMatch).source;
return new RegExp(reString + '|$', 'g');
}
/**
* Creates a compiled template function that can interpolate data properties
* in "interpolate" delimiters, HTML-escape interpolated data properties in
* "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
* properties may be accessed as free variables in the template. If a setting
* object is provided it takes precedence over `engine.settings` values.
*
* ```js
* let fn = engine.compile('Hello, <%= user %>!');
* //=> [function]
*
* fn({user: 'doowb'});
* //=> 'Hello, doowb!'
*
* fn({user: 'halle'});
* //=> 'Hello, halle!'
* ```
* @param {String} `str` The template string.
* @param {Object} `options` The options object.
* @param {RegExp} [options.escape] The HTML "escape" delimiter.
* @param {RegExp} [options.evaluate] The "evaluate" delimiter.
* @param {Object} [options.imports] An object to import into the template as free variables.
* @param {RegExp} [options.interpolate] The "interpolate" delimiter.
* @param {String} [options.sourceURL] The sourceURL of the template's compiled source.
* @param {String} [options.variable] The data object variable name.
* @param {Object} `settings` Engine settings
* @returns {Function} Returns the compiled template function.
* @api public
*/
compile(input, options = {}, settings) {
let engine = this;
if (!(this instanceof Engine)) {
if (!utils.isObject(options)) options = {};
engine = new Engine(options);
}
let str = String(input);
settings = assign({}, engine.settings, options && options.settings, settings);
let opts = assign({}, engine.options, settings, options);
let imports = assign({}, opts.imports, opts.helpers, settings.imports);
imports.escape = utils.escape;
assign(imports, utils.omit(engine.imports, 'engine'));
assign(imports, utils.omit(engine.cache.data, 'engine'));
imports.engine = engine;
let keys = Object.keys(imports);
let values = keys.map(key => imports[key]);
let isEvaluating;
let isEscaping;
let source = "__p += '";
let idx = 0;
// Use a sourceURL for easier debugging.
let sourceURL = '//# sourceURL='
+ ('sourceURL' in opts ? opts.sourceURL : (`engine.templateSources[${(++engine.counter)}]`))
+ '\n';
// Compile the regexp to match each delimiter.
let re = engine._regex(opts);
str.replace(re, (match, esc, interp, es6, evaluate, offset) => {
if (!interp) interp = es6;
// Escape characters that can't be included in str literals.
source += str.slice(idx, offset).replace(utils.reUnescapedString, utils.escapeStringChar);
// Replace delimiters with snippets.
if (esc) {
isEscaping = true;
source += `' +\n__e(${esc}) +\n'`;
}
if (evaluate) {
isEvaluating = true;
source += `';\n${evaluate};\n__p += '`;
}
if (interp) {
source += `' +\n((__t = (${interp})) == null ? '' : __t) +\n'`;
}
idx = offset + match.length;
// The JS engine embedded in Adobe products requires returning the `match`
// str in order to produce the correct `offset` value.
return match;
});
source += "';\n";
// If `variable` is not specified wrap a with-statement around the generated
// code to add the data object to the top of the scope chain.
let variable = opts.variable;
if (!variable) {
source = `with (obj) {\n${source}\n}\n`;
}
// Cleanup code by stripping empty strings.
source = (isEvaluating ? source.replace(utils.reEmptyStringLeading, '') : source)
.replace(utils.reEmptyStringMiddle, '$1')
.replace(utils.reEmptyStringTrailing, '$1;');
// Frame code as the function body.
source = 'function('
+ (variable || 'obj') + ') {\n'
+ (variable ? '' : '(obj || (obj = {}));\n')
+ 'let __t, __p = ""'
+ (isEscaping ? ', __e = escape' : '')
+ (isEvaluating ? ', __j = Array.prototype.join;\nfunction print() { __p += __j.call(arguments, "") }\n' : ';\n')
+ source + 'return __p\n}';
let result = utils.tryCatch(function() {
return Function(keys, sourceURL + `return ${source}`).apply(null, values);
});
// Provide the compiled function's source by its `toString` method or
// the `source` property as a convenience for inlining compiled templates.
result.source = source;
if (result instanceof Error) {
throw result;
}
return result;
}
/**
* Renders templates with the given data and returns a string.
*
* ```js
* engine.render('<%= user %>', {user: 'doowb'});
* //=> 'doowb'
* ```
* @param {String} `str`
* @param {Object} `data`
* @return {String}
* @api public
*/
render(fn, locals, options) {
let ctx = Object.assign({}, this.cache.data, locals);
if (ctx.imports) ctx = Object.assign({}, ctx, ctx.imports);
if (ctx.helpers) ctx = Object.assign({}, ctx, ctx.helpers);
if (typeof fn === 'string') {
fn = this.compile(fn, options);
}
return fn(ctx);
}
static get utils() {
return utils;
}
}
/**
* Expose `Engine`
*/
module.exports = Engine;