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