@kosatyi/ejs 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/ejs.cjs +961 -0
- package/dist/ejs.js +959 -0
- package/dist/ejs.min.js +1 -0
- package/dist/ejs.mjs +924 -0
- package/package.json +46 -0
package/dist/ejs.cjs
ADDED
|
@@ -0,0 +1,961 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var fs = require('fs');
|
|
5
|
+
var chokidar = require('chokidar');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
10
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
11
|
+
var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
|
|
12
|
+
|
|
13
|
+
var defaults = {};
|
|
14
|
+
defaults["export"] = 'ejs.precompiled';
|
|
15
|
+
defaults.path = 'views';
|
|
16
|
+
defaults.resolver = null;
|
|
17
|
+
defaults.extension = {
|
|
18
|
+
template: 'ejs',
|
|
19
|
+
module: 'mjs'
|
|
20
|
+
};
|
|
21
|
+
defaults.vars = {
|
|
22
|
+
EXTEND: '$$$',
|
|
23
|
+
BUFFER: '$$a',
|
|
24
|
+
OUTPUT: '$$i',
|
|
25
|
+
LAYOUT: '$$l',
|
|
26
|
+
MACROS: '$$m',
|
|
27
|
+
PRINT: '$$j',
|
|
28
|
+
BLOCKS: '$$b',
|
|
29
|
+
ERROR: '$$e',
|
|
30
|
+
SCOPE: '$$s',
|
|
31
|
+
SAFE: '$$v'
|
|
32
|
+
};
|
|
33
|
+
defaults.token = {
|
|
34
|
+
start: '<%',
|
|
35
|
+
end: '%>',
|
|
36
|
+
regex: '([\\s\\S]+?)'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var typeProp = function typeProp() {
|
|
40
|
+
var args = [].slice.call(arguments);
|
|
41
|
+
var callback = args.shift();
|
|
42
|
+
return args.filter(callback).pop();
|
|
43
|
+
};
|
|
44
|
+
var isFunction = function isFunction(v) {
|
|
45
|
+
return typeof v === 'function';
|
|
46
|
+
};
|
|
47
|
+
var isString = function isString(v) {
|
|
48
|
+
return typeof v === 'string';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
var isNode = new Function('try {return this===global;}catch(e){return false;}');
|
|
52
|
+
var symbolEntities = {
|
|
53
|
+
"'": "'",
|
|
54
|
+
'\\': '\\',
|
|
55
|
+
'\r': 'r',
|
|
56
|
+
'\n': 'n',
|
|
57
|
+
'\t': 't',
|
|
58
|
+
"\u2028": 'u2028',
|
|
59
|
+
"\u2029": 'u2029'
|
|
60
|
+
};
|
|
61
|
+
var htmlEntities = {
|
|
62
|
+
'&': '&',
|
|
63
|
+
'<': '<',
|
|
64
|
+
'>': '>',
|
|
65
|
+
'"': '"',
|
|
66
|
+
"'": '''
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
var regexKeys = function regexKeys(obj) {
|
|
70
|
+
return new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
var htmlEntitiesMatch = regexKeys(htmlEntities);
|
|
74
|
+
var symbolEntitiesMatch = regexKeys(symbolEntities);
|
|
75
|
+
var entities = function entities() {
|
|
76
|
+
var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
77
|
+
return ('' + string).replace(htmlEntitiesMatch, function (match) {
|
|
78
|
+
return htmlEntities[match];
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
var symbols = function symbols(string) {
|
|
82
|
+
return ('' + string).replace(symbolEntitiesMatch, function (match) {
|
|
83
|
+
return '\\' + symbolEntities[match];
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
var safeValue = function safeValue(value, escape, check) {
|
|
87
|
+
return (check = value) == null ? '' : escape ? entities(check) : check;
|
|
88
|
+
};
|
|
89
|
+
var getPath = function getPath(context, name) {
|
|
90
|
+
var data = context;
|
|
91
|
+
var chunk = name.split('.');
|
|
92
|
+
var prop = chunk.pop();
|
|
93
|
+
chunk.forEach(function (part) {
|
|
94
|
+
data = data[part] = data[part] || {};
|
|
95
|
+
});
|
|
96
|
+
return [data, prop];
|
|
97
|
+
};
|
|
98
|
+
var extend = function extend() {
|
|
99
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
100
|
+
args[_key] = arguments[_key];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
var target = args.shift();
|
|
104
|
+
return args.filter(function (source) {
|
|
105
|
+
return source;
|
|
106
|
+
}).reduce(function (target, source) {
|
|
107
|
+
return Object.assign(target, source);
|
|
108
|
+
}, target);
|
|
109
|
+
};
|
|
110
|
+
var noop = function noop() {};
|
|
111
|
+
var each = function each(object, callback) {
|
|
112
|
+
var prop;
|
|
113
|
+
|
|
114
|
+
for (prop in object) {
|
|
115
|
+
if (hasProp(object, prop)) {
|
|
116
|
+
callback(object[prop], prop, object);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var map = function map(object, callback, context) {
|
|
121
|
+
var result = [];
|
|
122
|
+
each(object, function (value, key, object) {
|
|
123
|
+
var item = callback(value, key, object);
|
|
124
|
+
|
|
125
|
+
if (item !== undefined) {
|
|
126
|
+
result.push(item);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return result;
|
|
130
|
+
};
|
|
131
|
+
var filter = function filter(object, callback, context) {
|
|
132
|
+
var isArray = object instanceof Array;
|
|
133
|
+
var result = isArray ? [] : {};
|
|
134
|
+
each(object, function (value, key, object) {
|
|
135
|
+
var item = callback(value, key, object);
|
|
136
|
+
|
|
137
|
+
if (item !== undefined) {
|
|
138
|
+
if (isArray) {
|
|
139
|
+
result.push(item);
|
|
140
|
+
} else {
|
|
141
|
+
result[key] = item;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return result;
|
|
146
|
+
};
|
|
147
|
+
var omit = function omit(object, list) {
|
|
148
|
+
return filter(object, function (value, key) {
|
|
149
|
+
if (list.indexOf(key) === -1) {
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
var hasProp = function hasProp(object, prop) {
|
|
155
|
+
return object && object.hasOwnProperty(prop);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
159
|
+
var space = ' ';
|
|
160
|
+
var quote = '"';
|
|
161
|
+
var equal = '=';
|
|
162
|
+
var slash = '/';
|
|
163
|
+
var lt = '<';
|
|
164
|
+
var gt = '>';
|
|
165
|
+
function element(tag, attrs, content) {
|
|
166
|
+
var result = [];
|
|
167
|
+
var hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
168
|
+
var attributes = map(attrs, function (value, key) {
|
|
169
|
+
if (value !== null && value !== undefined) {
|
|
170
|
+
return [entities(key), [quote, entities(value), quote].join('')].join(equal);
|
|
171
|
+
}
|
|
172
|
+
}).join(space);
|
|
173
|
+
result.push([lt, tag, space, attributes, gt].join(''));
|
|
174
|
+
|
|
175
|
+
if (content) {
|
|
176
|
+
result.push(content instanceof Array ? content.join('') : content);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (hasClosedTag) {
|
|
180
|
+
result.push([lt, slash, tag, gt].join(''));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result.join('');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
var tags = [{
|
|
187
|
+
symbol: '-',
|
|
188
|
+
format: function format(value) {
|
|
189
|
+
return "'+\n".concat(this.SAFE, "(").concat(value, ",1)+\n'");
|
|
190
|
+
}
|
|
191
|
+
}, {
|
|
192
|
+
symbol: '=',
|
|
193
|
+
format: function format(value) {
|
|
194
|
+
return "'+\n".concat(this.SAFE, "(").concat(value, ")+\n'");
|
|
195
|
+
}
|
|
196
|
+
}, {
|
|
197
|
+
symbol: '#',
|
|
198
|
+
format: function format(value) {
|
|
199
|
+
return "'+\n/**".concat(value, "**/+\n'");
|
|
200
|
+
}
|
|
201
|
+
}, {
|
|
202
|
+
symbol: '',
|
|
203
|
+
format: function format(value) {
|
|
204
|
+
return "')\n".concat(value, "\n").concat(this.BUFFER, "('");
|
|
205
|
+
}
|
|
206
|
+
}];
|
|
207
|
+
|
|
208
|
+
var match = function match(regex, text, callback) {
|
|
209
|
+
var index = 0;
|
|
210
|
+
text.replace(regex, function () {
|
|
211
|
+
var params = [].slice.call(arguments, 0, -1);
|
|
212
|
+
var offset = params.pop();
|
|
213
|
+
var match = params.shift();
|
|
214
|
+
callback(params, index, offset);
|
|
215
|
+
index = offset + match.length;
|
|
216
|
+
return match;
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
*
|
|
221
|
+
* @param {Object} config
|
|
222
|
+
* @return {function(*, *): Function}
|
|
223
|
+
* @constructor
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
var Compiler = function Compiler(config) {
|
|
228
|
+
var token = config.token;
|
|
229
|
+
var vars = config.vars;
|
|
230
|
+
var module = config.extension.module;
|
|
231
|
+
var matches = [];
|
|
232
|
+
var formats = [];
|
|
233
|
+
var slurp = {
|
|
234
|
+
match: '[ \\t]*',
|
|
235
|
+
start: [token.start, '_'],
|
|
236
|
+
end: ['_', token.end]
|
|
237
|
+
};
|
|
238
|
+
tags.forEach(function (item) {
|
|
239
|
+
matches.push(token.start.concat(item.symbol).concat(token.regex).concat(token.end));
|
|
240
|
+
formats.push(item.format.bind(vars));
|
|
241
|
+
});
|
|
242
|
+
var regex = new RegExp(matches.join('|').concat('|$'), 'g');
|
|
243
|
+
var slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
|
|
244
|
+
var slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
|
|
245
|
+
/**
|
|
246
|
+
* @type Function
|
|
247
|
+
* @name Compile
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
return function (content, path) {
|
|
251
|
+
var SCOPE = vars.SCOPE,
|
|
252
|
+
SAFE = vars.SAFE,
|
|
253
|
+
BUFFER = vars.BUFFER;
|
|
254
|
+
var extension = path.split('.').pop();
|
|
255
|
+
content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
|
|
256
|
+
content = content.replace(slurpStart, slurp.start).replace(slurpEnd, slurp.end);
|
|
257
|
+
|
|
258
|
+
if (extension === module) {
|
|
259
|
+
content = [token.start, content, token.end].join('\n');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
var source = "".concat(BUFFER, "('");
|
|
263
|
+
match(regex, content, function (params, index, offset) {
|
|
264
|
+
source += symbols(content.slice(index, offset));
|
|
265
|
+
params.forEach(function (value, index) {
|
|
266
|
+
if (value) source += formats[index](value);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
source += "');";
|
|
270
|
+
source = "with(".concat(SCOPE, "){").concat(source, "}");
|
|
271
|
+
source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
|
|
272
|
+
source += "\n//# sourceURL=".concat(path);
|
|
273
|
+
var result = null;
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
result = new Function(SCOPE, BUFFER, SAFE, source);
|
|
277
|
+
result.source = "(function(".concat(SCOPE, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})"); //result.source = result.toString()
|
|
278
|
+
} catch (e) {
|
|
279
|
+
e.filename = path;
|
|
280
|
+
e.source = source;
|
|
281
|
+
throw e;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return result;
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
var Wrapper = function Wrapper(config) {
|
|
289
|
+
var name = config["export"];
|
|
290
|
+
return function (list) {
|
|
291
|
+
var out = '(function(o){\n';
|
|
292
|
+
list.forEach(function (item) {
|
|
293
|
+
out += 'o[' + JSON.stringify(item.name) + ']=' + String(item.content) + '\n';
|
|
294
|
+
});
|
|
295
|
+
out += '})(window["' + name + '"] = window["' + name + '"] || {});\n';
|
|
296
|
+
return out;
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
var HttpRequest = function HttpRequest(template) {
|
|
301
|
+
return window.fetch(template).then(function (response) {
|
|
302
|
+
return response.text();
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
var FileSystem = function FileSystem(template) {
|
|
307
|
+
return new Promise(function (resolve, reject) {
|
|
308
|
+
fs__default["default"].readFile(template, function (error, data) {
|
|
309
|
+
if (error) {
|
|
310
|
+
reject(error);
|
|
311
|
+
} else {
|
|
312
|
+
resolve(data.toString());
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
var Watcher = function Watcher(path, cache) {
|
|
319
|
+
return chokidar__default["default"].watch('.', {
|
|
320
|
+
cwd: path
|
|
321
|
+
}).on('change', function (name) {
|
|
322
|
+
cache.remove(name);
|
|
323
|
+
}).on('error', function (error) {
|
|
324
|
+
console.log('watcher error: ' + error);
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
var Template = function Template(config, cache, compile) {
|
|
329
|
+
var path = config.path;
|
|
330
|
+
config.token || {};
|
|
331
|
+
var resolver = isFunction(config.resolver) ? config.resolver : isNode() ? FileSystem : HttpRequest;
|
|
332
|
+
|
|
333
|
+
var normalize = function normalize(template) {
|
|
334
|
+
template = [path, template].join('/');
|
|
335
|
+
template = template.replace(/\/\//g, '/');
|
|
336
|
+
return template;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
var resolve = function resolve(template) {
|
|
340
|
+
return resolver(normalize(template));
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
var result = function result(content, template) {
|
|
344
|
+
cache.set(template, content);
|
|
345
|
+
return content;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
var get = function get(template) {
|
|
349
|
+
if (cache.exist(template)) {
|
|
350
|
+
return cache.resolve(template);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
var content = resolve(template).then(function (content) {
|
|
354
|
+
return result(compile(content, template), template);
|
|
355
|
+
});
|
|
356
|
+
return result(content, template);
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
if (config.watch && isNode()) {
|
|
360
|
+
Watcher(path, cache);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return get;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
function _defineProperty(obj, key, value) {
|
|
367
|
+
if (key in obj) {
|
|
368
|
+
Object.defineProperty(obj, key, {
|
|
369
|
+
value: value,
|
|
370
|
+
enumerable: true,
|
|
371
|
+
configurable: true,
|
|
372
|
+
writable: true
|
|
373
|
+
});
|
|
374
|
+
} else {
|
|
375
|
+
obj[key] = value;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return obj;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
var resolve = function resolve(list) {
|
|
382
|
+
return Promise.all(list).then(function (list) {
|
|
383
|
+
return list.join('');
|
|
384
|
+
});
|
|
385
|
+
};
|
|
386
|
+
/**
|
|
387
|
+
*
|
|
388
|
+
* @return {function}
|
|
389
|
+
*/
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
var Buffer = function Buffer() {
|
|
393
|
+
var store = [],
|
|
394
|
+
array = [];
|
|
395
|
+
|
|
396
|
+
function buffer(value) {
|
|
397
|
+
array.push(value);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
buffer.start = function () {
|
|
401
|
+
array = [];
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
buffer.backup = function () {
|
|
405
|
+
store.push(array.concat());
|
|
406
|
+
array = [];
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
buffer.restore = function () {
|
|
410
|
+
var result = array.concat();
|
|
411
|
+
array = store.pop();
|
|
412
|
+
return resolve(result);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
buffer.end = function () {
|
|
416
|
+
return resolve(array);
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
return buffer;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
*
|
|
424
|
+
* @param {{}} instance
|
|
425
|
+
* @method create
|
|
426
|
+
*/
|
|
427
|
+
|
|
428
|
+
var Component = function Component(instance) {
|
|
429
|
+
var defaults = extend({}, instance.props);
|
|
430
|
+
var create = instance.create;
|
|
431
|
+
return {
|
|
432
|
+
element: element,
|
|
433
|
+
create: create,
|
|
434
|
+
render: function render(props) {
|
|
435
|
+
return this.create(extend({}, defaults, props));
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
var Scope = function Scope(config, methods) {
|
|
441
|
+
var _Object$definePropert;
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
*
|
|
445
|
+
*/
|
|
446
|
+
var _config$vars = config.vars,
|
|
447
|
+
EXTEND = _config$vars.EXTEND,
|
|
448
|
+
MACROS = _config$vars.MACROS,
|
|
449
|
+
LAYOUT = _config$vars.LAYOUT,
|
|
450
|
+
BLOCKS = _config$vars.BLOCKS,
|
|
451
|
+
BUFFER = _config$vars.BUFFER;
|
|
452
|
+
/**
|
|
453
|
+
*
|
|
454
|
+
* @param data
|
|
455
|
+
* @constructor
|
|
456
|
+
*/
|
|
457
|
+
|
|
458
|
+
function Scope() {
|
|
459
|
+
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
460
|
+
extend(this, data);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
*
|
|
464
|
+
*/
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
Object.defineProperties(Scope.prototype, (_Object$definePropert = {}, _defineProperty(_Object$definePropert, BUFFER, {
|
|
468
|
+
value: Buffer(),
|
|
469
|
+
writable: false,
|
|
470
|
+
configurable: false,
|
|
471
|
+
enumerable: false
|
|
472
|
+
}), _defineProperty(_Object$definePropert, BLOCKS, {
|
|
473
|
+
value: {},
|
|
474
|
+
writable: false,
|
|
475
|
+
configurable: false,
|
|
476
|
+
enumerable: false
|
|
477
|
+
}), _defineProperty(_Object$definePropert, EXTEND, {
|
|
478
|
+
value: false,
|
|
479
|
+
writable: true,
|
|
480
|
+
configurable: false,
|
|
481
|
+
enumerable: false
|
|
482
|
+
}), _defineProperty(_Object$definePropert, LAYOUT, {
|
|
483
|
+
value: false,
|
|
484
|
+
writable: true,
|
|
485
|
+
configurable: false,
|
|
486
|
+
enumerable: false
|
|
487
|
+
}), _defineProperty(_Object$definePropert, "setBuffer", {
|
|
488
|
+
value: function value(_value) {
|
|
489
|
+
this[BUFFER] = _value;
|
|
490
|
+
},
|
|
491
|
+
writable: false,
|
|
492
|
+
configurable: false
|
|
493
|
+
}), _defineProperty(_Object$definePropert, "getBuffer", {
|
|
494
|
+
value: function value() {
|
|
495
|
+
return this[BUFFER];
|
|
496
|
+
},
|
|
497
|
+
writable: false,
|
|
498
|
+
configurable: false
|
|
499
|
+
}), _defineProperty(_Object$definePropert, "setBlocks", {
|
|
500
|
+
value: function value(_value2) {
|
|
501
|
+
this[BLOCKS] = _value2;
|
|
502
|
+
},
|
|
503
|
+
writable: false,
|
|
504
|
+
configurable: false
|
|
505
|
+
}), _defineProperty(_Object$definePropert, "getBlocks", {
|
|
506
|
+
value: function value() {
|
|
507
|
+
return this[BLOCKS];
|
|
508
|
+
},
|
|
509
|
+
writable: false,
|
|
510
|
+
configurable: false
|
|
511
|
+
}), _defineProperty(_Object$definePropert, "setExtend", {
|
|
512
|
+
value: function value(_value3) {
|
|
513
|
+
this[EXTEND] = _value3;
|
|
514
|
+
},
|
|
515
|
+
writable: false,
|
|
516
|
+
configurable: false
|
|
517
|
+
}), _defineProperty(_Object$definePropert, "getExtend", {
|
|
518
|
+
value: function value() {
|
|
519
|
+
return this[EXTEND];
|
|
520
|
+
},
|
|
521
|
+
writable: false,
|
|
522
|
+
configurable: false
|
|
523
|
+
}), _defineProperty(_Object$definePropert, "setLayout", {
|
|
524
|
+
value: function value(layout) {
|
|
525
|
+
this[LAYOUT] = layout;
|
|
526
|
+
},
|
|
527
|
+
writable: false,
|
|
528
|
+
configurable: false
|
|
529
|
+
}), _defineProperty(_Object$definePropert, "getLayout", {
|
|
530
|
+
value: function value() {
|
|
531
|
+
return this[LAYOUT];
|
|
532
|
+
},
|
|
533
|
+
writable: false,
|
|
534
|
+
configurable: false
|
|
535
|
+
}), _Object$definePropert));
|
|
536
|
+
|
|
537
|
+
Scope.helpers = function (methods) {
|
|
538
|
+
extend(Scope.prototype, methods);
|
|
539
|
+
};
|
|
540
|
+
/**
|
|
541
|
+
* @lends Scope.prototype
|
|
542
|
+
*/
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
Scope.helpers(methods);
|
|
546
|
+
/**
|
|
547
|
+
* @lends Scope.prototype
|
|
548
|
+
*/
|
|
549
|
+
|
|
550
|
+
Scope.helpers({
|
|
551
|
+
/**
|
|
552
|
+
* @return {*}
|
|
553
|
+
*/
|
|
554
|
+
clone: function clone(exclude_blocks) {
|
|
555
|
+
var filter = [LAYOUT, EXTEND, MACROS, BUFFER];
|
|
556
|
+
|
|
557
|
+
if (exclude_blocks === true) {
|
|
558
|
+
filter.push(BLOCKS);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return omit(this, filter);
|
|
562
|
+
},
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Join values to output buffer
|
|
566
|
+
* @memberOf global
|
|
567
|
+
* @type Function
|
|
568
|
+
*/
|
|
569
|
+
echo: function echo() {
|
|
570
|
+
var buffer = this.getBuffer();
|
|
571
|
+
var params = [].slice.call(arguments);
|
|
572
|
+
params.forEach(function (item) {
|
|
573
|
+
buffer(item);
|
|
574
|
+
});
|
|
575
|
+
},
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Buffered output callback
|
|
579
|
+
* @type Function
|
|
580
|
+
* @param {Function} callback
|
|
581
|
+
* @param {Boolean} [echo]
|
|
582
|
+
* @return {Function}
|
|
583
|
+
*/
|
|
584
|
+
macro: function macro(callback, echo) {
|
|
585
|
+
var buffer = this.getBuffer();
|
|
586
|
+
|
|
587
|
+
var macro = function () {
|
|
588
|
+
buffer.backup();
|
|
589
|
+
|
|
590
|
+
if (isFunction(callback)) {
|
|
591
|
+
callback.apply(this, arguments);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
var result = buffer.restore();
|
|
595
|
+
return echo === true ? this.echo(result) : result;
|
|
596
|
+
}.bind(this);
|
|
597
|
+
|
|
598
|
+
macro.ctx = this;
|
|
599
|
+
return macro;
|
|
600
|
+
},
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* @memberOf global
|
|
604
|
+
* @param value
|
|
605
|
+
* @param callback
|
|
606
|
+
* @return {Promise<unknown>}
|
|
607
|
+
*/
|
|
608
|
+
resolve: function resolve(value, callback) {
|
|
609
|
+
return Promise.resolve(value).then(callback.bind(this));
|
|
610
|
+
},
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* @memberOf global
|
|
614
|
+
*/
|
|
615
|
+
async: function async(promise, callback) {
|
|
616
|
+
var _this = this;
|
|
617
|
+
|
|
618
|
+
this.echo(this.resolve(promise, function (data) {
|
|
619
|
+
return _this.macro(callback)(data);
|
|
620
|
+
}));
|
|
621
|
+
},
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* @memberOf global
|
|
625
|
+
*/
|
|
626
|
+
node: element,
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* @memberOf global
|
|
630
|
+
*/
|
|
631
|
+
element: function element$1(tag, attr, content) {
|
|
632
|
+
if (isFunction(content)) {
|
|
633
|
+
content = this.macro(content)();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
this.echo(this.resolve(content, function (content) {
|
|
637
|
+
return element(tag, attr, content);
|
|
638
|
+
}));
|
|
639
|
+
},
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* @memberOf global
|
|
643
|
+
* @param {String} namespace
|
|
644
|
+
* @param {Object} instance
|
|
645
|
+
*/
|
|
646
|
+
component: function component(namespace, instance) {
|
|
647
|
+
instance = Component(instance);
|
|
648
|
+
this.set(namespace, function (props) {
|
|
649
|
+
this.echo(instance.render(props));
|
|
650
|
+
}.bind(this));
|
|
651
|
+
},
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* @memberOf global
|
|
655
|
+
* @param name
|
|
656
|
+
* @param defaults
|
|
657
|
+
*/
|
|
658
|
+
get: function get(name, defaults) {
|
|
659
|
+
var path = getPath(this, name);
|
|
660
|
+
var result = path.shift();
|
|
661
|
+
var prop = path.pop();
|
|
662
|
+
return hasProp(result, prop) ? result[prop] : defaults;
|
|
663
|
+
},
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* @memberOf global
|
|
667
|
+
* @param {String} name
|
|
668
|
+
* @param value
|
|
669
|
+
* @return
|
|
670
|
+
*/
|
|
671
|
+
set: function set(name, value) {
|
|
672
|
+
var path = getPath(this, name);
|
|
673
|
+
var result = path.shift();
|
|
674
|
+
var prop = path.pop();
|
|
675
|
+
|
|
676
|
+
if (this.getExtend()) {
|
|
677
|
+
if (hasProp(result, prop)) {
|
|
678
|
+
return result[prop];
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
result[prop] = value;
|
|
683
|
+
},
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* @memberOf global
|
|
687
|
+
* @param name
|
|
688
|
+
*/
|
|
689
|
+
call: function call(name) {
|
|
690
|
+
var params = [].slice.call(arguments, 1);
|
|
691
|
+
var path = getPath(this, name);
|
|
692
|
+
var result = path.shift();
|
|
693
|
+
var prop = path.pop();
|
|
694
|
+
|
|
695
|
+
if (isFunction(result[prop])) {
|
|
696
|
+
return result[prop].apply(result, params);
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* @memberOf global
|
|
702
|
+
* @param object
|
|
703
|
+
* @param callback
|
|
704
|
+
*/
|
|
705
|
+
each: function each$1(object, callback) {
|
|
706
|
+
if (isString(object)) {
|
|
707
|
+
object = this.get(object, []);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
each(object, callback);
|
|
711
|
+
},
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* @memberOf global
|
|
715
|
+
* @param {String} layout
|
|
716
|
+
*/
|
|
717
|
+
extend: function extend(layout) {
|
|
718
|
+
this.setExtend(true);
|
|
719
|
+
this.setLayout(layout);
|
|
720
|
+
},
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* @memberOf global
|
|
724
|
+
* @param name
|
|
725
|
+
* @param callback
|
|
726
|
+
* @return {*}
|
|
727
|
+
*/
|
|
728
|
+
block: function block(name, callback) {
|
|
729
|
+
var blocks = this.getBlocks();
|
|
730
|
+
var macro = this.macro(callback);
|
|
731
|
+
|
|
732
|
+
if (this.getExtend()) {
|
|
733
|
+
blocks[name] = macro;
|
|
734
|
+
} else {
|
|
735
|
+
var block = blocks[name];
|
|
736
|
+
|
|
737
|
+
if (block) {
|
|
738
|
+
var parent = function () {
|
|
739
|
+
this.echo(macro());
|
|
740
|
+
}.bind(block.ctx);
|
|
741
|
+
|
|
742
|
+
this.echo(block(parent));
|
|
743
|
+
} else {
|
|
744
|
+
this.echo(macro(noop));
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
},
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* @memberOf global
|
|
751
|
+
* @param {string} path
|
|
752
|
+
* @param {object} [data]
|
|
753
|
+
* @param {boolean} [cx]
|
|
754
|
+
*/
|
|
755
|
+
include: function include(path, data, cx) {
|
|
756
|
+
var context = cx === false ? {} : this.clone(true);
|
|
757
|
+
var params = extend(context, data || {});
|
|
758
|
+
var promise = this.render(path, params);
|
|
759
|
+
this.echo(promise);
|
|
760
|
+
},
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* @memberOf global
|
|
764
|
+
* @param {string} path
|
|
765
|
+
*/
|
|
766
|
+
use: function use(path) {
|
|
767
|
+
var scope = this;
|
|
768
|
+
|
|
769
|
+
var promise = this.require(path);
|
|
770
|
+
|
|
771
|
+
this.echo(promise);
|
|
772
|
+
return {
|
|
773
|
+
as: function as(namespace) {
|
|
774
|
+
promise.then(function (exports) {
|
|
775
|
+
scope.set(namespace, exports);
|
|
776
|
+
});
|
|
777
|
+
return this;
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
},
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* @memberOf global
|
|
784
|
+
* @param {string} path
|
|
785
|
+
*/
|
|
786
|
+
from: function from(path) {
|
|
787
|
+
var scope = this;
|
|
788
|
+
|
|
789
|
+
var promise = this.require(path);
|
|
790
|
+
|
|
791
|
+
this.echo(promise);
|
|
792
|
+
return {
|
|
793
|
+
use: function use() {
|
|
794
|
+
var params = [].slice.call(arguments);
|
|
795
|
+
promise.then(function (exports) {
|
|
796
|
+
params.forEach(function (name) {
|
|
797
|
+
scope.set(name, exports[name]);
|
|
798
|
+
});
|
|
799
|
+
});
|
|
800
|
+
return this;
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
return Scope;
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
function Cache(config) {
|
|
809
|
+
var namespace = config["export"];
|
|
810
|
+
var list = {};
|
|
811
|
+
var cache = {
|
|
812
|
+
preload: function preload() {
|
|
813
|
+
if (isNode() === false) {
|
|
814
|
+
this.load(window[namespace]);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
return this;
|
|
818
|
+
},
|
|
819
|
+
exist: function exist(key) {
|
|
820
|
+
return hasProp(list, key);
|
|
821
|
+
},
|
|
822
|
+
get: function get(key) {
|
|
823
|
+
return list[key];
|
|
824
|
+
},
|
|
825
|
+
remove: function remove(key) {
|
|
826
|
+
delete list[key];
|
|
827
|
+
},
|
|
828
|
+
resolve: function resolve(key) {
|
|
829
|
+
return Promise.resolve(this.get(key));
|
|
830
|
+
},
|
|
831
|
+
set: function set(key, value) {
|
|
832
|
+
list[key] = value;
|
|
833
|
+
return this;
|
|
834
|
+
},
|
|
835
|
+
load: function load(data) {
|
|
836
|
+
extend(list, data);
|
|
837
|
+
return this;
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
return cache.preload();
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function init(options) {
|
|
844
|
+
/**
|
|
845
|
+
* @type {Object}
|
|
846
|
+
*/
|
|
847
|
+
var config = {};
|
|
848
|
+
var _helpers = {};
|
|
849
|
+
|
|
850
|
+
var ext = function ext(path, defaults) {
|
|
851
|
+
var ext = path.split('.').pop();
|
|
852
|
+
|
|
853
|
+
if (ext !== defaults) {
|
|
854
|
+
path = [path, defaults].join('.');
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
return path;
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
var view = {
|
|
861
|
+
element: element,
|
|
862
|
+
output: function output(path, scope) {
|
|
863
|
+
return view.template(path).then(function (template) {
|
|
864
|
+
return template.call(scope, scope, scope.getBuffer(), safeValue);
|
|
865
|
+
});
|
|
866
|
+
},
|
|
867
|
+
render: function render(name, data) {
|
|
868
|
+
var filepath = ext(name, config.extension.template);
|
|
869
|
+
var scope = new view.scope(data);
|
|
870
|
+
return view.output(filepath, scope).then(function (content) {
|
|
871
|
+
if (scope.getExtend()) {
|
|
872
|
+
scope.setExtend(false);
|
|
873
|
+
var layout = scope.getLayout();
|
|
874
|
+
|
|
875
|
+
var _data = scope.clone();
|
|
876
|
+
|
|
877
|
+
return view.render(layout, _data);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return content;
|
|
881
|
+
});
|
|
882
|
+
},
|
|
883
|
+
require: function require(name) {
|
|
884
|
+
var filepath = ext(name, config.extension.module);
|
|
885
|
+
var scope = new view.scope({});
|
|
886
|
+
return view.output(filepath, scope).then(function () {
|
|
887
|
+
return scope.clone(true);
|
|
888
|
+
});
|
|
889
|
+
},
|
|
890
|
+
helpers: function helpers(methods) {
|
|
891
|
+
methods = methods || {};
|
|
892
|
+
extend(_helpers, methods);
|
|
893
|
+
view.scope.helpers(methods);
|
|
894
|
+
},
|
|
895
|
+
configure: function configure(options) {
|
|
896
|
+
config["export"] = typeProp(isString, defaults["export"], options["export"]);
|
|
897
|
+
config.path = typeProp(isString, defaults.path, options.path);
|
|
898
|
+
config.resolver = typeProp(isFunction, defaults.resolver, options.resolver);
|
|
899
|
+
config.extension = extend({}, defaults.extension, options.extension);
|
|
900
|
+
config.token = extend({}, defaults.token, options.token);
|
|
901
|
+
config.vars = extend({}, defaults.vars, options.vars);
|
|
902
|
+
view.scope = Scope(config, _helpers);
|
|
903
|
+
view.compile = Compiler(config);
|
|
904
|
+
view.wrapper = Wrapper(config);
|
|
905
|
+
view.cache = Cache(config);
|
|
906
|
+
view.template = Template(config, view.cache, view.compile);
|
|
907
|
+
return view;
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
/**
|
|
911
|
+
*
|
|
912
|
+
* @param name
|
|
913
|
+
* @param options
|
|
914
|
+
* @param callback
|
|
915
|
+
* @return {*}
|
|
916
|
+
* @private
|
|
917
|
+
*/
|
|
918
|
+
|
|
919
|
+
view.__express = function (name, options, callback) {
|
|
920
|
+
if (isFunction(options)) {
|
|
921
|
+
callback = options;
|
|
922
|
+
options = {};
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
options = options || {};
|
|
926
|
+
var settings = options.settings || {};
|
|
927
|
+
var viewPath = settings['views'];
|
|
928
|
+
var viewOptions = settings['view options'] || {};
|
|
929
|
+
var filename = path__default["default"].relative(viewPath, name);
|
|
930
|
+
viewOptions.path = viewPath;
|
|
931
|
+
view.configure(viewOptions);
|
|
932
|
+
return view.render(filename, options).then(function (content) {
|
|
933
|
+
callback(null, content);
|
|
934
|
+
})["catch"](function (error) {
|
|
935
|
+
callback(error);
|
|
936
|
+
});
|
|
937
|
+
};
|
|
938
|
+
/**
|
|
939
|
+
*
|
|
940
|
+
*/
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
view.configure(options || {});
|
|
944
|
+
/**
|
|
945
|
+
*
|
|
946
|
+
*/
|
|
947
|
+
|
|
948
|
+
view.helpers({
|
|
949
|
+
require: function require(name) {
|
|
950
|
+
return view.require(name, this);
|
|
951
|
+
},
|
|
952
|
+
render: function render(name, data) {
|
|
953
|
+
return view.render(name, data);
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
return view;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
var index = init();
|
|
960
|
+
|
|
961
|
+
module.exports = index;
|