@kosatyi/ejs 0.0.64 → 0.0.66
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 +2 -6
- package/dist/cjs/index.js +387 -246
- package/dist/esm/index.js +415 -323
- package/dist/umd/index.js +387 -246
- package/dist/umd/index.min.js +1 -1
- package/package.json +3 -9
package/dist/esm/index.js
CHANGED
|
@@ -63,7 +63,7 @@ const safeValue = (value, escape) => {
|
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
const instanceOf = (object, instance) => {
|
|
66
|
-
return object instanceof instance
|
|
66
|
+
return Boolean(object instanceof instance)
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
const getPath = (context, name, strict) => {
|
|
@@ -95,8 +95,10 @@ const ext = (path, defaults) => {
|
|
|
95
95
|
return path
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
/**
|
|
99
|
+
* @type {<T extends {}, U, V, W,Y>(T,U,V,W,Y)=> T & U & V & W & Y}
|
|
100
|
+
*/
|
|
101
|
+
const extend = (target, ...args) => {
|
|
100
102
|
return args
|
|
101
103
|
.filter((source) => source)
|
|
102
104
|
.reduce((target, source) => Object.assign(target, source), target)
|
|
@@ -262,79 +264,55 @@ const configSchema = (config, options) => {
|
|
|
262
264
|
config.rmWhitespace,
|
|
263
265
|
options.rmWhitespace
|
|
264
266
|
),
|
|
265
|
-
cache: typeProp(isBoolean, defaults.
|
|
267
|
+
cache: typeProp(isBoolean, defaults.cache, config.cache, options.cache),
|
|
266
268
|
token: extend({}, defaults.token, config.token, options.token),
|
|
267
269
|
vars: extend({}, defaults.vars, config.vars, options.vars),
|
|
268
270
|
});
|
|
269
271
|
};
|
|
270
272
|
|
|
271
|
-
const
|
|
272
|
-
template = [path, template].join('/');
|
|
273
|
-
template = template.replace(/\/\//g, '/');
|
|
274
|
-
return template
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const httpRequest = (path, template) => {
|
|
278
|
-
return fetch(resolvePath(path, template)).then((response) =>
|
|
279
|
-
response.text()
|
|
280
|
-
)
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const fileSystem = (path, template) => {
|
|
284
|
-
return new Promise((resolve, reject) => {
|
|
285
|
-
fs.readFile(resolvePath(path, template), (error, data) => {
|
|
286
|
-
if (error) {
|
|
287
|
-
reject(error);
|
|
288
|
-
} else {
|
|
289
|
-
resolve(data.toString());
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
})
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
const fileResolver = (resolver) => {
|
|
296
|
-
return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
function Template(config, cache, compiler) {
|
|
300
|
-
if (instanceOf(this, Template) === false)
|
|
301
|
-
return new Template(config, cache, compiler)
|
|
302
|
-
|
|
303
|
-
const template = {};
|
|
273
|
+
const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
|
|
304
274
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
275
|
+
function Cache(config) {
|
|
276
|
+
if (instanceOf(this, Cache) === false) return new Cache()
|
|
277
|
+
const cache = {
|
|
278
|
+
enabled: true,
|
|
279
|
+
list: {},
|
|
308
280
|
};
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
281
|
+
this.configure = function (config) {
|
|
282
|
+
cache.enabled = config.cache;
|
|
283
|
+
if (isNode() === false) {
|
|
284
|
+
this.load(global[config.export]);
|
|
285
|
+
}
|
|
312
286
|
};
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
287
|
+
this.clear = function () {
|
|
288
|
+
cache.list = {};
|
|
289
|
+
};
|
|
290
|
+
this.load = function (data) {
|
|
291
|
+
if (cache.enabled) {
|
|
292
|
+
extend(cache.list, data || {});
|
|
319
293
|
}
|
|
294
|
+
return this
|
|
320
295
|
};
|
|
321
|
-
this.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
296
|
+
this.get = function (key) {
|
|
297
|
+
if (cache.enabled) {
|
|
298
|
+
return cache.list[key]
|
|
299
|
+
}
|
|
325
300
|
};
|
|
326
|
-
this.
|
|
327
|
-
if (cache.
|
|
328
|
-
|
|
301
|
+
this.set = function (key, value) {
|
|
302
|
+
if (cache.enabled) {
|
|
303
|
+
cache.list[key] = value;
|
|
329
304
|
}
|
|
330
|
-
return
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
305
|
+
return this
|
|
306
|
+
};
|
|
307
|
+
this.resolve = function (key) {
|
|
308
|
+
return Promise.resolve(this.get(key))
|
|
309
|
+
};
|
|
310
|
+
this.remove = function (key) {
|
|
311
|
+
delete cache.list[key];
|
|
312
|
+
};
|
|
313
|
+
this.exist = function (key) {
|
|
314
|
+
return hasProp(cache.list, key)
|
|
336
315
|
};
|
|
337
|
-
this.configure(config);
|
|
338
316
|
}
|
|
339
317
|
|
|
340
318
|
const tagList = [
|
|
@@ -457,54 +435,80 @@ function Compiler(config) {
|
|
|
457
435
|
this.configure(config);
|
|
458
436
|
}
|
|
459
437
|
|
|
460
|
-
const
|
|
438
|
+
const resolvePath = (path, template) => {
|
|
439
|
+
template = [path, template].join('/');
|
|
440
|
+
template = template.replace(/\/\//g, '/');
|
|
441
|
+
return template
|
|
442
|
+
};
|
|
461
443
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
444
|
+
const httpRequest = (path, template) => {
|
|
445
|
+
return fetch(resolvePath(path, template)).then((response) =>
|
|
446
|
+
response.text()
|
|
447
|
+
)
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const fileSystem = (path, template) => {
|
|
451
|
+
return new Promise((resolve, reject) => {
|
|
452
|
+
fs.readFile(resolvePath(path, template), (error, data) => {
|
|
453
|
+
if (error) {
|
|
454
|
+
reject(error);
|
|
455
|
+
} else {
|
|
456
|
+
resolve(data.toString());
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
})
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const fileResolver = (resolver) => {
|
|
463
|
+
return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
function Template(config, cache, compiler) {
|
|
467
|
+
if (instanceOf(this, Template) === false)
|
|
468
|
+
return new Template(config, cache, compiler)
|
|
469
|
+
if (instanceOf(cache, Cache) === false)
|
|
470
|
+
throw new TypeError('cache is not instance of Cache')
|
|
471
|
+
if (instanceOf(compiler, Compiler) === false)
|
|
472
|
+
throw new TypeError('compiler is not instance of Compiler')
|
|
473
|
+
const template = {};
|
|
474
|
+
const result = function (template, content) {
|
|
475
|
+
cache.set(template, content);
|
|
476
|
+
return content
|
|
467
477
|
};
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
cache.enabled = config.cache;
|
|
471
|
-
if (isNode() === false) {
|
|
472
|
-
this.load(global[config.export]);
|
|
473
|
-
}
|
|
478
|
+
const resolve = function (path) {
|
|
479
|
+
return template.resolver(template.path, path)
|
|
474
480
|
};
|
|
475
|
-
|
|
476
|
-
if (
|
|
477
|
-
|
|
481
|
+
const compile = function (content, template) {
|
|
482
|
+
if (isFunction(content)) {
|
|
483
|
+
return content
|
|
484
|
+
} else {
|
|
485
|
+
return compiler.compile(content, template)
|
|
478
486
|
}
|
|
479
|
-
return this
|
|
480
487
|
};
|
|
481
|
-
this.
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
488
|
+
this.configure = function (config) {
|
|
489
|
+
template.path = config.path;
|
|
490
|
+
template.cache = config.cache;
|
|
491
|
+
template.resolver = fileResolver(config.resolver);
|
|
485
492
|
};
|
|
486
|
-
this.
|
|
487
|
-
if (cache.
|
|
488
|
-
cache.
|
|
493
|
+
this.get = function (template) {
|
|
494
|
+
if (cache.exist(template)) {
|
|
495
|
+
return cache.resolve(template)
|
|
489
496
|
}
|
|
490
|
-
return
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
delete cache.list[key];
|
|
497
|
-
};
|
|
498
|
-
this.exist = function (key) {
|
|
499
|
-
return hasProp(cache.list, key)
|
|
497
|
+
return result(
|
|
498
|
+
template,
|
|
499
|
+
resolve(template).then((content) =>
|
|
500
|
+
result(template, compile(content, template))
|
|
501
|
+
)
|
|
502
|
+
)
|
|
500
503
|
};
|
|
504
|
+
this.configure(config);
|
|
501
505
|
}
|
|
502
506
|
|
|
503
507
|
function resolve(list) {
|
|
504
508
|
return Promise.all(list || []).then((list) => list.join(''))
|
|
505
509
|
}
|
|
506
510
|
|
|
507
|
-
|
|
511
|
+
function createBuffer() {
|
|
508
512
|
let store = [],
|
|
509
513
|
array = [];
|
|
510
514
|
function buffer(value) {
|
|
@@ -529,11 +533,10 @@ const createBuffer = function () {
|
|
|
529
533
|
return resolve(array)
|
|
530
534
|
};
|
|
531
535
|
return buffer
|
|
532
|
-
}
|
|
536
|
+
}
|
|
533
537
|
|
|
534
538
|
function Context(config) {
|
|
535
539
|
if (instanceOf(this, Context) === false) return new Context(config)
|
|
536
|
-
|
|
537
540
|
this.configure = function (config, methods) {
|
|
538
541
|
const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT } = config.vars;
|
|
539
542
|
|
|
@@ -543,6 +546,7 @@ function Context(config) {
|
|
|
543
546
|
|
|
544
547
|
this.helpers = function (methods) {
|
|
545
548
|
extend(Scope.prototype, methods || {});
|
|
549
|
+
console.log('helpers', Scope.prototype);
|
|
546
550
|
};
|
|
547
551
|
|
|
548
552
|
function Scope(data) {
|
|
@@ -550,234 +554,331 @@ function Context(config) {
|
|
|
550
554
|
this[MACRO] = {};
|
|
551
555
|
extend(this, data || {});
|
|
552
556
|
}
|
|
553
|
-
Scope.prototype = extend({}, methods || {});
|
|
554
|
-
Scope.method = Scope.define = function (
|
|
555
|
-
name,
|
|
556
|
-
value,
|
|
557
|
-
writable,
|
|
558
|
-
configurable,
|
|
559
|
-
enumerable
|
|
560
|
-
) {
|
|
561
|
-
Object.defineProperty(Scope.prototype, name, {
|
|
562
|
-
value: value,
|
|
563
|
-
writable: writable || false,
|
|
564
|
-
configurable: configurable || false,
|
|
565
|
-
enumerable: enumerable || false,
|
|
566
|
-
});
|
|
567
|
-
};
|
|
568
|
-
|
|
569
|
-
Scope.define(BUFFER, createBuffer());
|
|
570
|
-
Scope.define(BLOCKS, {}, true);
|
|
571
|
-
Scope.define(MACRO, {}, true);
|
|
572
|
-
Scope.define(LAYOUT, false, true);
|
|
573
|
-
Scope.define(EXTEND, false, true);
|
|
574
|
-
|
|
575
|
-
Scope.method('getMacro', function () {
|
|
576
|
-
return this[MACRO]
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
Scope.method('getBuffer', function () {
|
|
580
|
-
return this[BUFFER]
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
Scope.method('getComponent', function () {
|
|
584
|
-
const context = this;
|
|
585
|
-
if (COMPONENT in context) {
|
|
586
|
-
return function () {
|
|
587
|
-
return context[COMPONENT].apply(context, arguments)
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
return function () {
|
|
591
|
-
console.log('%s function not defined', COMPONENT);
|
|
592
|
-
}
|
|
593
|
-
});
|
|
594
|
-
|
|
595
|
-
Scope.method('getBlocks', function () {
|
|
596
|
-
return this[BLOCKS]
|
|
597
|
-
});
|
|
598
|
-
|
|
599
|
-
Scope.method('setExtend', function (value) {
|
|
600
|
-
this[EXTEND] = value;
|
|
601
|
-
});
|
|
602
|
-
|
|
603
|
-
Scope.method('getExtend', function () {
|
|
604
|
-
return this[EXTEND]
|
|
605
|
-
});
|
|
606
557
|
|
|
607
|
-
Scope.
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
Scope.method('macro', function (name, callback) {
|
|
664
|
-
const list = this.getMacro();
|
|
665
|
-
const macro = this.fn(callback);
|
|
666
|
-
const context = this;
|
|
667
|
-
list[name] = function () {
|
|
668
|
-
return context.echo(macro.apply(undefined, arguments))
|
|
669
|
-
};
|
|
670
|
-
});
|
|
671
|
-
|
|
672
|
-
Scope.method('call', function (name) {
|
|
673
|
-
const list = this.getMacro();
|
|
674
|
-
const macro = list[name];
|
|
675
|
-
const params = [].slice.call(arguments, 1);
|
|
676
|
-
if (isFunction(macro)) {
|
|
677
|
-
return macro.apply(macro, params)
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
Scope.method('block', function (name, callback) {
|
|
682
|
-
const blocks = this.getBlocks();
|
|
683
|
-
blocks[name] = blocks[name] || [];
|
|
684
|
-
blocks[name].push(this.fn(callback));
|
|
685
|
-
if (this.getExtend()) return
|
|
686
|
-
const list = Object.assign([], blocks[name]);
|
|
687
|
-
const current = function () {
|
|
688
|
-
return list.shift()
|
|
689
|
-
};
|
|
690
|
-
const next = () => {
|
|
691
|
-
const parent = current();
|
|
692
|
-
if (parent) {
|
|
693
|
-
return () => {
|
|
694
|
-
this.echo(parent(next()));
|
|
558
|
+
Scope.prototype = extend({}, methods || {});
|
|
559
|
+
Object.defineProperties(Scope.prototype, {
|
|
560
|
+
[BUFFER]: {
|
|
561
|
+
value: createBuffer(),
|
|
562
|
+
writable: true,
|
|
563
|
+
configurable: false,
|
|
564
|
+
enumerable: false,
|
|
565
|
+
},
|
|
566
|
+
[BLOCKS]: {
|
|
567
|
+
value: {},
|
|
568
|
+
writable: true,
|
|
569
|
+
configurable: false,
|
|
570
|
+
enumerable: false,
|
|
571
|
+
},
|
|
572
|
+
[MACRO]: {
|
|
573
|
+
value: {},
|
|
574
|
+
writable: true,
|
|
575
|
+
configurable: false,
|
|
576
|
+
enumerable: false,
|
|
577
|
+
},
|
|
578
|
+
[LAYOUT]: {
|
|
579
|
+
value: false,
|
|
580
|
+
writable: true,
|
|
581
|
+
configurable: false,
|
|
582
|
+
enumerable: false,
|
|
583
|
+
},
|
|
584
|
+
[EXTEND]: {
|
|
585
|
+
value: false,
|
|
586
|
+
writable: true,
|
|
587
|
+
configurable: false,
|
|
588
|
+
enumerable: false,
|
|
589
|
+
},
|
|
590
|
+
getMacro: {
|
|
591
|
+
value() {
|
|
592
|
+
return this[MACRO]
|
|
593
|
+
},
|
|
594
|
+
writable: false,
|
|
595
|
+
configurable: false,
|
|
596
|
+
enumerable: false,
|
|
597
|
+
},
|
|
598
|
+
getBuffer: {
|
|
599
|
+
value() {
|
|
600
|
+
return this[BUFFER]
|
|
601
|
+
},
|
|
602
|
+
writable: false,
|
|
603
|
+
configurable: false,
|
|
604
|
+
enumerable: false,
|
|
605
|
+
},
|
|
606
|
+
getComponent: {
|
|
607
|
+
value() {
|
|
608
|
+
const context = this;
|
|
609
|
+
if (COMPONENT in context) {
|
|
610
|
+
return function () {
|
|
611
|
+
return context[COMPONENT].apply(context, arguments)
|
|
612
|
+
}
|
|
695
613
|
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
614
|
+
return function () {
|
|
615
|
+
console.log('%s function not defined', COMPONENT);
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
writable: false,
|
|
619
|
+
configurable: false,
|
|
620
|
+
enumerable: false,
|
|
621
|
+
},
|
|
622
|
+
getBlocks: {
|
|
623
|
+
value() {
|
|
624
|
+
return this[BLOCKS]
|
|
625
|
+
},
|
|
626
|
+
writable: false,
|
|
627
|
+
configurable: false,
|
|
628
|
+
enumerable: false,
|
|
629
|
+
},
|
|
630
|
+
setExtend: {
|
|
631
|
+
value(value) {
|
|
632
|
+
this[EXTEND] = value;
|
|
633
|
+
},
|
|
634
|
+
writable: false,
|
|
635
|
+
configurable: false,
|
|
636
|
+
enumerable: false,
|
|
637
|
+
},
|
|
638
|
+
getExtend: {
|
|
639
|
+
value() {
|
|
640
|
+
return this[EXTEND]
|
|
641
|
+
},
|
|
642
|
+
writable: false,
|
|
643
|
+
configurable: false,
|
|
644
|
+
enumerable: false,
|
|
645
|
+
},
|
|
646
|
+
setLayout: {
|
|
647
|
+
value(layout) {
|
|
648
|
+
this[LAYOUT] = layout;
|
|
649
|
+
},
|
|
650
|
+
writable: false,
|
|
651
|
+
configurable: false,
|
|
652
|
+
enumerable: false,
|
|
653
|
+
},
|
|
654
|
+
getLayout: {
|
|
655
|
+
value() {
|
|
656
|
+
return this[LAYOUT]
|
|
657
|
+
},
|
|
658
|
+
writable: false,
|
|
659
|
+
configurable: false,
|
|
660
|
+
enumerable: false,
|
|
661
|
+
},
|
|
662
|
+
clone: {
|
|
663
|
+
value(exclude_blocks) {
|
|
664
|
+
const filter = [LAYOUT, EXTEND, BUFFER];
|
|
665
|
+
if (exclude_blocks === true) {
|
|
666
|
+
filter.push(BLOCKS);
|
|
667
|
+
}
|
|
668
|
+
return omit(this, filter)
|
|
669
|
+
},
|
|
670
|
+
writable: false,
|
|
671
|
+
configurable: false,
|
|
672
|
+
enumerable: false,
|
|
673
|
+
},
|
|
674
|
+
extend: {
|
|
675
|
+
value(layout) {
|
|
676
|
+
this.setExtend(true);
|
|
677
|
+
this.setLayout(layout);
|
|
678
|
+
},
|
|
679
|
+
writable: false,
|
|
680
|
+
configurable: false,
|
|
681
|
+
enumerable: false,
|
|
682
|
+
},
|
|
683
|
+
echo: {
|
|
684
|
+
value(layout) {
|
|
685
|
+
const buffer = this.getBuffer();
|
|
686
|
+
const params = [].slice.call(arguments);
|
|
687
|
+
params.forEach(buffer);
|
|
688
|
+
},
|
|
689
|
+
writable: false,
|
|
690
|
+
configurable: false,
|
|
691
|
+
enumerable: false,
|
|
692
|
+
},
|
|
693
|
+
fn: {
|
|
694
|
+
value(callback) {
|
|
695
|
+
const buffer = this.getBuffer();
|
|
696
|
+
const context = this;
|
|
697
|
+
return function () {
|
|
698
|
+
buffer.backup();
|
|
699
|
+
if (isFunction(callback)) {
|
|
700
|
+
callback.apply(context, arguments);
|
|
701
|
+
}
|
|
702
|
+
return buffer.restore()
|
|
703
|
+
}
|
|
704
|
+
},
|
|
705
|
+
writable: false,
|
|
706
|
+
configurable: false,
|
|
707
|
+
enumerable: false,
|
|
708
|
+
},
|
|
709
|
+
get: {
|
|
710
|
+
value(name, defaults) {
|
|
711
|
+
const path = getPath(this, name, true);
|
|
712
|
+
const result = path.shift();
|
|
713
|
+
const prop = path.pop();
|
|
714
|
+
return hasProp(result, prop) ? result[prop] : defaults
|
|
715
|
+
},
|
|
716
|
+
writable: false,
|
|
717
|
+
configurable: false,
|
|
718
|
+
enumerable: false,
|
|
719
|
+
},
|
|
720
|
+
set: {
|
|
721
|
+
value(name, value) {
|
|
722
|
+
const path = getPath(this, name, false);
|
|
723
|
+
const result = path.shift();
|
|
724
|
+
const prop = path.pop();
|
|
725
|
+
if (this.getExtend() && hasProp(result, prop)) {
|
|
726
|
+
return result[prop]
|
|
727
|
+
}
|
|
728
|
+
return (result[prop] = value)
|
|
729
|
+
},
|
|
730
|
+
writable: false,
|
|
731
|
+
configurable: false,
|
|
732
|
+
enumerable: false,
|
|
733
|
+
},
|
|
734
|
+
macro: {
|
|
735
|
+
value(name, callback) {
|
|
736
|
+
const list = this.getMacro();
|
|
737
|
+
const macro = this.fn(callback);
|
|
738
|
+
const context = this;
|
|
739
|
+
list[name] = function () {
|
|
740
|
+
return context.echo(macro.apply(undefined, arguments))
|
|
741
|
+
};
|
|
742
|
+
},
|
|
743
|
+
writable: false,
|
|
744
|
+
configurable: false,
|
|
745
|
+
enumerable: false,
|
|
746
|
+
},
|
|
747
|
+
call: {
|
|
748
|
+
value(name) {
|
|
749
|
+
const list = this.getMacro();
|
|
750
|
+
const macro = list[name];
|
|
751
|
+
const params = [].slice.call(arguments, 1);
|
|
752
|
+
if (isFunction(macro)) {
|
|
753
|
+
return macro.apply(macro, params)
|
|
754
|
+
}
|
|
755
|
+
},
|
|
756
|
+
writable: false,
|
|
757
|
+
configurable: false,
|
|
758
|
+
enumerable: false,
|
|
759
|
+
},
|
|
760
|
+
block: {
|
|
761
|
+
value(name, callback) {
|
|
762
|
+
const blocks = this.getBlocks();
|
|
763
|
+
blocks[name] = blocks[name] || [];
|
|
764
|
+
blocks[name].push(this.fn(callback));
|
|
765
|
+
if (this.getExtend()) return
|
|
766
|
+
const list = Object.assign([], blocks[name]);
|
|
767
|
+
const current = function () {
|
|
768
|
+
return list.shift()
|
|
769
|
+
};
|
|
770
|
+
const next = () => {
|
|
771
|
+
const parent = current();
|
|
772
|
+
if (parent) {
|
|
773
|
+
return () => {
|
|
774
|
+
this.echo(parent(next()));
|
|
775
|
+
}
|
|
776
|
+
} else {
|
|
777
|
+
return noop
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
this.echo(current()(next()));
|
|
781
|
+
},
|
|
782
|
+
writable: false,
|
|
783
|
+
configurable: false,
|
|
784
|
+
enumerable: false,
|
|
785
|
+
},
|
|
786
|
+
include: {
|
|
787
|
+
value(path, data, cx) {
|
|
788
|
+
const context = cx === false ? {} : this.clone(true);
|
|
789
|
+
const params = extend(context, data || {});
|
|
790
|
+
const promise = this.render(path, params);
|
|
791
|
+
this.echo(promise);
|
|
792
|
+
},
|
|
793
|
+
writable: false,
|
|
794
|
+
configurable: false,
|
|
795
|
+
enumerable: false,
|
|
796
|
+
},
|
|
797
|
+
use: {
|
|
798
|
+
value(path, namespace) {
|
|
799
|
+
const promise = this.require(path);
|
|
800
|
+
this.echo(
|
|
801
|
+
resolve$1(
|
|
802
|
+
promise,
|
|
803
|
+
function (exports) {
|
|
804
|
+
const list = this.getMacro();
|
|
805
|
+
each(exports, function (macro, name) {
|
|
806
|
+
list[[namespace, name].join('.')] = macro;
|
|
807
|
+
});
|
|
808
|
+
},
|
|
809
|
+
this
|
|
810
|
+
)
|
|
811
|
+
);
|
|
812
|
+
},
|
|
813
|
+
writable: false,
|
|
814
|
+
configurable: false,
|
|
815
|
+
enumerable: false,
|
|
816
|
+
},
|
|
817
|
+
async: {
|
|
818
|
+
value(promise, callback) {
|
|
819
|
+
this.echo(
|
|
820
|
+
resolve$1(
|
|
821
|
+
promise,
|
|
822
|
+
function (data) {
|
|
823
|
+
return this.fn(callback)(data)
|
|
824
|
+
},
|
|
825
|
+
this
|
|
826
|
+
)
|
|
827
|
+
);
|
|
828
|
+
},
|
|
829
|
+
writable: false,
|
|
830
|
+
configurable: false,
|
|
831
|
+
enumerable: false,
|
|
832
|
+
},
|
|
833
|
+
each: {
|
|
834
|
+
value: function (object, callback) {
|
|
835
|
+
if (isString(object)) {
|
|
836
|
+
object = this.get(object, []);
|
|
837
|
+
}
|
|
838
|
+
each(object, callback);
|
|
839
|
+
},
|
|
840
|
+
writable: false,
|
|
841
|
+
configurable: false,
|
|
842
|
+
enumerable: false,
|
|
843
|
+
},
|
|
844
|
+
element: {
|
|
845
|
+
value(tag, attr, content) {
|
|
846
|
+
return element(tag, attr, content)
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
el: {
|
|
850
|
+
value(tag, attr, content) {
|
|
851
|
+
if (isFunction(content)) {
|
|
852
|
+
content = this.fn(content)();
|
|
853
|
+
}
|
|
854
|
+
this.echo(
|
|
855
|
+
resolve$1(
|
|
856
|
+
content,
|
|
857
|
+
function (content) {
|
|
858
|
+
return this.element(tag, attr, content)
|
|
859
|
+
},
|
|
860
|
+
this
|
|
861
|
+
)
|
|
862
|
+
);
|
|
863
|
+
},
|
|
864
|
+
writable: false,
|
|
865
|
+
configurable: false,
|
|
866
|
+
enumerable: false,
|
|
867
|
+
},
|
|
762
868
|
});
|
|
763
869
|
};
|
|
764
|
-
|
|
765
870
|
this.configure(config);
|
|
766
871
|
}
|
|
767
872
|
|
|
768
873
|
function EJS(options) {
|
|
769
874
|
if (instanceOf(this, EJS) === false) return new EJS(options)
|
|
770
|
-
|
|
771
875
|
const scope = {};
|
|
772
876
|
const config = {};
|
|
773
|
-
|
|
774
877
|
configSchema(config, options || {});
|
|
775
|
-
|
|
776
878
|
const context = new Context(config);
|
|
777
879
|
const compiler = new Compiler(config);
|
|
778
880
|
const cache = new Cache();
|
|
779
881
|
const template = new Template(config, cache, compiler);
|
|
780
|
-
|
|
781
882
|
const output = function (path, scope) {
|
|
782
883
|
return template.get(path).then(function (callback) {
|
|
783
884
|
return callback.call(
|
|
@@ -789,7 +890,6 @@ function EJS(options) {
|
|
|
789
890
|
)
|
|
790
891
|
})
|
|
791
892
|
};
|
|
792
|
-
|
|
793
893
|
const require = function (name) {
|
|
794
894
|
const filepath = ext(name, config.extension);
|
|
795
895
|
const scope = context.create({});
|
|
@@ -797,7 +897,6 @@ function EJS(options) {
|
|
|
797
897
|
return scope.getMacro()
|
|
798
898
|
})
|
|
799
899
|
};
|
|
800
|
-
|
|
801
900
|
const render = function (name, data) {
|
|
802
901
|
const filepath = ext(name, config.extension);
|
|
803
902
|
const scope = context.create(data);
|
|
@@ -811,7 +910,6 @@ function EJS(options) {
|
|
|
811
910
|
return content
|
|
812
911
|
})
|
|
813
912
|
};
|
|
814
|
-
|
|
815
913
|
this.configure = function (options) {
|
|
816
914
|
options = options || {};
|
|
817
915
|
configSchema(config, options);
|
|
@@ -821,32 +919,26 @@ function EJS(options) {
|
|
|
821
919
|
template.configure(config);
|
|
822
920
|
return config
|
|
823
921
|
};
|
|
824
|
-
|
|
825
922
|
this.render = function (name, data) {
|
|
826
923
|
return render(name, data)
|
|
827
924
|
};
|
|
828
|
-
|
|
829
925
|
this.helpers = function (methods) {
|
|
830
|
-
context.helpers(
|
|
926
|
+
context.helpers(extend(scope, methods));
|
|
831
927
|
};
|
|
832
|
-
|
|
833
928
|
this.preload = function (list) {
|
|
834
929
|
return cache.load(list || {})
|
|
835
930
|
};
|
|
836
|
-
|
|
837
931
|
this.create = function (options) {
|
|
838
932
|
return new EJS(options)
|
|
839
933
|
};
|
|
840
|
-
|
|
841
934
|
this.compile = function (content, path) {
|
|
842
935
|
return compiler.compile(content, path)
|
|
843
936
|
};
|
|
844
|
-
|
|
845
937
|
this.context = function (data) {
|
|
846
938
|
return context.create(data)
|
|
847
939
|
};
|
|
848
|
-
|
|
849
940
|
this.helpers({ require, render });
|
|
941
|
+
return this
|
|
850
942
|
}
|
|
851
943
|
|
|
852
944
|
const ejs = new EJS();
|
|
@@ -872,7 +964,7 @@ function __express(name, options, callback) {
|
|
|
872
964
|
viewOptions.path = viewPath;
|
|
873
965
|
viewOptions.cache = viewCache;
|
|
874
966
|
configure(viewOptions);
|
|
875
|
-
return
|
|
967
|
+
return render(filename, options)
|
|
876
968
|
.then((content) => {
|
|
877
969
|
callback(null, content);
|
|
878
970
|
})
|