janja 0.0.0 → 1.1.0
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/index.cjs.js +106 -172
- package/dist/index.d.ts +14 -23
- package/dist/index.esm.js +106 -172
- package/dist/index.js +106 -172
- package/package.json +3 -23
- package/LICENSE +0 -21
- package/README.md +0 -39
package/dist/index.js
CHANGED
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
let cursor = 0;
|
|
19
19
|
let caretLinesCount = 0;
|
|
20
20
|
lines.forEach((line, index) => {
|
|
21
|
-
|
|
21
|
+
if (!line) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
output.push(`${`${index + 1}: `.padStart(indentWidth, ' ')}${line}`);
|
|
22
25
|
ranges.forEach((tag) => {
|
|
23
26
|
if (tag.start < cursor) {
|
|
24
27
|
ranges.splice(ranges.indexOf(tag), 1);
|
|
@@ -112,15 +115,16 @@ ${output
|
|
|
112
115
|
});
|
|
113
116
|
}
|
|
114
117
|
in() {
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
this.context = `${this.context}_${this.index++}`;
|
|
119
|
+
this.contexts.push(this.context);
|
|
120
|
+
return this.context;
|
|
117
121
|
}
|
|
118
122
|
out() {
|
|
119
123
|
if (this.context === CONTEXT) {
|
|
120
124
|
return;
|
|
121
125
|
}
|
|
122
126
|
this.contexts.pop();
|
|
123
|
-
this.context = this.contexts
|
|
127
|
+
this.context = this.contexts.at(-1) ?? CONTEXT;
|
|
124
128
|
}
|
|
125
129
|
expect(name) {
|
|
126
130
|
this.expected.push(name);
|
|
@@ -285,32 +289,32 @@ ${output
|
|
|
285
289
|
value: options
|
|
286
290
|
});
|
|
287
291
|
}
|
|
288
|
-
async compile(
|
|
292
|
+
async compile(tag, filepath) {
|
|
289
293
|
const ctx = new Context(this.options);
|
|
290
294
|
const out = new OutScript(this.options);
|
|
291
295
|
const sourcemap = new SourceMap(this.options);
|
|
292
296
|
out.start();
|
|
293
|
-
while (
|
|
294
|
-
const compilers = (this.options.compilers[
|
|
297
|
+
while (tag) {
|
|
298
|
+
const compilers = (this.options.compilers[tag.name] ?? []);
|
|
295
299
|
for (const compiler of compilers) {
|
|
296
300
|
try {
|
|
297
301
|
const r = await compiler.compile({
|
|
298
|
-
|
|
302
|
+
tag,
|
|
299
303
|
ctx,
|
|
300
304
|
out,
|
|
301
305
|
});
|
|
302
306
|
if (r !== false) {
|
|
303
307
|
if (r) {
|
|
304
|
-
sourcemap.addMapping(
|
|
308
|
+
sourcemap.addMapping(tag, r);
|
|
305
309
|
}
|
|
306
310
|
break;
|
|
307
311
|
}
|
|
308
312
|
}
|
|
309
313
|
catch (error) {
|
|
310
|
-
throw new CompileError(error.message,
|
|
314
|
+
throw new CompileError(error.message, tag, filepath);
|
|
311
315
|
}
|
|
312
316
|
}
|
|
313
|
-
|
|
317
|
+
tag = tag.next;
|
|
314
318
|
}
|
|
315
319
|
try {
|
|
316
320
|
ctx.validate();
|
|
@@ -327,66 +331,6 @@ ${output
|
|
|
327
331
|
}
|
|
328
332
|
}
|
|
329
333
|
|
|
330
|
-
const hasOwn = Object.prototype.hasOwnProperty;
|
|
331
|
-
function translate(translations = {}, key, params = {}) {
|
|
332
|
-
return (hasOwn.call(translations, key) ? translations[key] : key).replace(/\{(.+?)\}/g, (_, name) => (hasOwn.call(params, name) ? params[name] : _));
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const monthNames = [
|
|
336
|
-
'January',
|
|
337
|
-
'February',
|
|
338
|
-
'March',
|
|
339
|
-
'April',
|
|
340
|
-
'May',
|
|
341
|
-
'June',
|
|
342
|
-
'July',
|
|
343
|
-
'August',
|
|
344
|
-
'September',
|
|
345
|
-
'October',
|
|
346
|
-
'November',
|
|
347
|
-
'December',
|
|
348
|
-
];
|
|
349
|
-
const shortMonthNames = [
|
|
350
|
-
'Jan',
|
|
351
|
-
'Feb',
|
|
352
|
-
'Mar',
|
|
353
|
-
'Apr',
|
|
354
|
-
'May',
|
|
355
|
-
'Jun',
|
|
356
|
-
'Jul',
|
|
357
|
-
'Aug',
|
|
358
|
-
'Sep',
|
|
359
|
-
'Oct',
|
|
360
|
-
'Nov',
|
|
361
|
-
'Dec',
|
|
362
|
-
];
|
|
363
|
-
const dayNames = [
|
|
364
|
-
'Sunday',
|
|
365
|
-
'Monday',
|
|
366
|
-
'Tuesday',
|
|
367
|
-
'Wednesday',
|
|
368
|
-
'Thursday',
|
|
369
|
-
'Friday',
|
|
370
|
-
'Saturday',
|
|
371
|
-
];
|
|
372
|
-
const shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
373
|
-
const o = {
|
|
374
|
-
y: d => `${d.getUTCFullYear()}`,
|
|
375
|
-
M: (d, len) => `${d.getUTCMonth() + 1}`.padStart(len, '0'),
|
|
376
|
-
N: (d, len, translations) => translate(translations, (len > 1 ? monthNames : shortMonthNames)[d.getUTCMonth()]),
|
|
377
|
-
d: (d, len) => `${d.getUTCDate()}`.padStart(len, '0'),
|
|
378
|
-
h: (d, len) => `${d.getUTCHours()}`.padStart(len, '0'),
|
|
379
|
-
m: (d, len) => `${d.getUTCMinutes()}`.padStart(len, '0'),
|
|
380
|
-
s: (d, len) => `${d.getUTCSeconds()}`.padStart(len, '0'),
|
|
381
|
-
D: (d, len, translations) => translate(translations, (len > 1 ? dayNames : shortDayNames)[d.getUTCDay()]),
|
|
382
|
-
};
|
|
383
|
-
function formatDatetime(translations = {}, value = 0, format) {
|
|
384
|
-
const d = new Date(value);
|
|
385
|
-
return format.replace(/y+|M+|N+|d+|h+|m+|s+|D+/g, (str) => {
|
|
386
|
-
return o[str[0]](d, str.length, translations);
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
|
|
390
334
|
class Safe extends String {
|
|
391
335
|
constructor(value) {
|
|
392
336
|
super(`${value}`);
|
|
@@ -417,8 +361,8 @@ ${output
|
|
|
417
361
|
function compact(value = []) {
|
|
418
362
|
return value.filter(v => v != null);
|
|
419
363
|
}
|
|
420
|
-
function
|
|
421
|
-
return
|
|
364
|
+
function div(value = 0, divisor = 1) {
|
|
365
|
+
return value / divisor;
|
|
422
366
|
}
|
|
423
367
|
function entries(value = {}) {
|
|
424
368
|
return Object.entries(value);
|
|
@@ -471,8 +415,8 @@ ${output
|
|
|
471
415
|
function min(value = 0, ...values) {
|
|
472
416
|
return Math.min(value, ...values);
|
|
473
417
|
}
|
|
474
|
-
function
|
|
475
|
-
return value
|
|
418
|
+
function mul(value = 0, multiplier = 1) {
|
|
419
|
+
return value * multiplier;
|
|
476
420
|
}
|
|
477
421
|
function odd(value = 0) {
|
|
478
422
|
return +value % 2 === 1;
|
|
@@ -504,15 +448,12 @@ ${output
|
|
|
504
448
|
function split(value = '', separator = '') {
|
|
505
449
|
return value.split(separator);
|
|
506
450
|
}
|
|
451
|
+
function sub(value = 0, subtrahend = 0) {
|
|
452
|
+
return value - subtrahend;
|
|
453
|
+
}
|
|
507
454
|
function sum(value = []) {
|
|
508
455
|
return value.reduce((a, b) => a + b, 0);
|
|
509
456
|
}
|
|
510
|
-
function t(value, params) {
|
|
511
|
-
return translate(this.translations, value, params);
|
|
512
|
-
}
|
|
513
|
-
function time(value = 0, format = 'hh:mm') {
|
|
514
|
-
return formatDatetime(this.translations, value, format);
|
|
515
|
-
}
|
|
516
457
|
function trim(value = '') {
|
|
517
458
|
return value.trim();
|
|
518
459
|
}
|
|
@@ -544,7 +485,7 @@ ${output
|
|
|
544
485
|
capitalize: capitalize,
|
|
545
486
|
ceil: ceil,
|
|
546
487
|
compact: compact,
|
|
547
|
-
|
|
488
|
+
div: div,
|
|
548
489
|
entries: entries,
|
|
549
490
|
escape: escape,
|
|
550
491
|
even: even,
|
|
@@ -561,7 +502,7 @@ ${output
|
|
|
561
502
|
map: map,
|
|
562
503
|
max: max,
|
|
563
504
|
min: min,
|
|
564
|
-
|
|
505
|
+
mul: mul,
|
|
565
506
|
odd: odd,
|
|
566
507
|
omit: omit,
|
|
567
508
|
pick: pick,
|
|
@@ -572,9 +513,8 @@ ${output
|
|
|
572
513
|
slice: slice,
|
|
573
514
|
sort: sort,
|
|
574
515
|
split: split,
|
|
516
|
+
sub: sub,
|
|
575
517
|
sum: sum,
|
|
576
|
-
t: t,
|
|
577
|
-
time: time,
|
|
578
518
|
trim: trim,
|
|
579
519
|
truncate: truncate,
|
|
580
520
|
unique: unique,
|
|
@@ -587,6 +527,7 @@ ${output
|
|
|
587
527
|
const expTokenOperators = {
|
|
588
528
|
AND: '&&',
|
|
589
529
|
OR: '||',
|
|
530
|
+
IS: '',
|
|
590
531
|
NOT: '!',
|
|
591
532
|
EQ: '===',
|
|
592
533
|
NE: '!==',
|
|
@@ -609,11 +550,9 @@ ${output
|
|
|
609
550
|
COMMA: ',',
|
|
610
551
|
DOT: '.',
|
|
611
552
|
ID: '',
|
|
612
|
-
|
|
613
|
-
NUM: '',
|
|
553
|
+
LIT: '',
|
|
614
554
|
IF: '',
|
|
615
555
|
ELSE: '',
|
|
616
|
-
BOOL: '',
|
|
617
556
|
};
|
|
618
557
|
|
|
619
558
|
class ExpCompiler {
|
|
@@ -642,18 +581,16 @@ ${output
|
|
|
642
581
|
return this.compileNot(expression);
|
|
643
582
|
case 'ID':
|
|
644
583
|
return this.compileId(expression);
|
|
645
|
-
case '
|
|
646
|
-
return this.
|
|
647
|
-
case 'NUM':
|
|
648
|
-
return this.compileNum(expression);
|
|
649
|
-
case 'BOOL':
|
|
650
|
-
return this.compileBool(expression);
|
|
584
|
+
case 'LIT':
|
|
585
|
+
return this.compileLit(expression);
|
|
651
586
|
case 'PIPE':
|
|
652
587
|
return this.compilePipe(expression);
|
|
653
588
|
case 'OF':
|
|
654
589
|
return this.compileOf(expression);
|
|
655
590
|
case 'SET':
|
|
656
591
|
return this.compileSet(expression);
|
|
592
|
+
case 'IS':
|
|
593
|
+
return this.compileIs(expression);
|
|
657
594
|
case 'AND':
|
|
658
595
|
case 'OR':
|
|
659
596
|
case 'EQ':
|
|
@@ -689,14 +626,8 @@ ${output
|
|
|
689
626
|
}
|
|
690
627
|
return s;
|
|
691
628
|
}
|
|
692
|
-
|
|
693
|
-
return JSON.stringify(value);
|
|
694
|
-
}
|
|
695
|
-
compileNum({ value }) {
|
|
696
|
-
return value.toString();
|
|
697
|
-
}
|
|
698
|
-
compileBool({ value }) {
|
|
699
|
-
return value.toString();
|
|
629
|
+
compileLit({ value }) {
|
|
630
|
+
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
700
631
|
}
|
|
701
632
|
compileSeq({ elements }) {
|
|
702
633
|
return elements.map(element => this.compileExp(element)).toString();
|
|
@@ -723,6 +654,9 @@ ${output
|
|
|
723
654
|
// destructuring
|
|
724
655
|
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
725
656
|
}
|
|
657
|
+
compileIs({ left, right }) {
|
|
658
|
+
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
659
|
+
}
|
|
726
660
|
compileBinary({ type, left, right }) {
|
|
727
661
|
const ret = `(${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)})`;
|
|
728
662
|
return type === 'NI' ? `(!${ret})` : ret;
|
|
@@ -741,6 +675,7 @@ ${output
|
|
|
741
675
|
RP: 20,
|
|
742
676
|
OR: 25,
|
|
743
677
|
AND: 30,
|
|
678
|
+
IS: 40,
|
|
744
679
|
EQ: 40,
|
|
745
680
|
NE: 40,
|
|
746
681
|
GT: 40,
|
|
@@ -759,15 +694,14 @@ ${output
|
|
|
759
694
|
PIPE: 80,
|
|
760
695
|
DOT: 90,
|
|
761
696
|
ID: 90,
|
|
762
|
-
|
|
763
|
-
NUM: 90,
|
|
764
|
-
BOOL: 90,
|
|
697
|
+
LIT: 90,
|
|
765
698
|
};
|
|
766
699
|
|
|
767
700
|
const expTokenTypes = {
|
|
768
701
|
'and': 'AND',
|
|
769
702
|
'or': 'OR',
|
|
770
703
|
'not': 'NOT',
|
|
704
|
+
'is': 'IS',
|
|
771
705
|
'eq': 'EQ',
|
|
772
706
|
'ne': 'NE',
|
|
773
707
|
'gt': 'GT',
|
|
@@ -784,8 +718,10 @@ ${output
|
|
|
784
718
|
'%': 'MOD',
|
|
785
719
|
'if': 'IF',
|
|
786
720
|
'else': 'ELSE',
|
|
787
|
-
'true': '
|
|
788
|
-
'false': '
|
|
721
|
+
'true': 'LIT',
|
|
722
|
+
'false': 'LIT',
|
|
723
|
+
'null': 'LIT',
|
|
724
|
+
'undefined': 'LIT',
|
|
789
725
|
'|': 'PIPE',
|
|
790
726
|
'=': 'SET',
|
|
791
727
|
'(': 'LP',
|
|
@@ -899,7 +835,7 @@ ${output
|
|
|
899
835
|
// skip closing quote char
|
|
900
836
|
this.cursor++;
|
|
901
837
|
this.tokens.push({
|
|
902
|
-
type: '
|
|
838
|
+
type: 'LIT',
|
|
903
839
|
value,
|
|
904
840
|
start,
|
|
905
841
|
end: this.cursor,
|
|
@@ -925,7 +861,7 @@ ${output
|
|
|
925
861
|
this.cursor++;
|
|
926
862
|
}
|
|
927
863
|
this.tokens.push({
|
|
928
|
-
type: '
|
|
864
|
+
type: 'LIT',
|
|
929
865
|
value: Number.parseFloat(value),
|
|
930
866
|
start,
|
|
931
867
|
end: this.cursor,
|
|
@@ -947,7 +883,17 @@ ${output
|
|
|
947
883
|
const type = value in expTokenTypes ? expTokenTypes[value] : 'ID';
|
|
948
884
|
this.tokens.push({
|
|
949
885
|
type,
|
|
950
|
-
value: type === '
|
|
886
|
+
value: type === 'LIT'
|
|
887
|
+
? value === 'true'
|
|
888
|
+
? true
|
|
889
|
+
: value === 'false'
|
|
890
|
+
? false
|
|
891
|
+
: value === 'null'
|
|
892
|
+
? null
|
|
893
|
+
: value === 'undefined'
|
|
894
|
+
? undefined
|
|
895
|
+
: value
|
|
896
|
+
: value,
|
|
951
897
|
start,
|
|
952
898
|
end: this.cursor,
|
|
953
899
|
raw: value,
|
|
@@ -1101,27 +1047,10 @@ ${output
|
|
|
1101
1047
|
}
|
|
1102
1048
|
continue;
|
|
1103
1049
|
}
|
|
1104
|
-
if (token.type === '
|
|
1050
|
+
if (token.type === 'LIT') {
|
|
1105
1051
|
left = {
|
|
1106
1052
|
...token,
|
|
1107
|
-
type: '
|
|
1108
|
-
value: token.value,
|
|
1109
|
-
};
|
|
1110
|
-
continue;
|
|
1111
|
-
}
|
|
1112
|
-
if (token.type === 'NUM') {
|
|
1113
|
-
left = {
|
|
1114
|
-
...token,
|
|
1115
|
-
type: 'NUM',
|
|
1116
|
-
value: token.value,
|
|
1117
|
-
};
|
|
1118
|
-
continue;
|
|
1119
|
-
}
|
|
1120
|
-
if (token.type === 'BOOL') {
|
|
1121
|
-
left = {
|
|
1122
|
-
...token,
|
|
1123
|
-
type: 'BOOL',
|
|
1124
|
-
value: token.value,
|
|
1053
|
+
type: 'LIT',
|
|
1125
1054
|
};
|
|
1126
1055
|
continue;
|
|
1127
1056
|
}
|
|
@@ -1159,6 +1088,7 @@ ${output
|
|
|
1159
1088
|
}
|
|
1160
1089
|
if (token.type === 'AND'
|
|
1161
1090
|
|| token.type === 'OR'
|
|
1091
|
+
|| token.type === 'IS'
|
|
1162
1092
|
|| token.type === 'EQ'
|
|
1163
1093
|
|| token.type === 'NE'
|
|
1164
1094
|
|| token.type === 'GT'
|
|
@@ -1337,7 +1267,7 @@ ${output
|
|
|
1337
1267
|
*/
|
|
1338
1268
|
const tag$7 = {
|
|
1339
1269
|
names: [CALL, ENDCALL],
|
|
1340
|
-
async compile({
|
|
1270
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1341
1271
|
if (name === CALL) {
|
|
1342
1272
|
if (value?.type !== 'ID') {
|
|
1343
1273
|
throw new Error(`"${CALL}" tag must specify a "macro" name`);
|
|
@@ -1363,7 +1293,7 @@ ${output
|
|
|
1363
1293
|
*/
|
|
1364
1294
|
const tag$6 = {
|
|
1365
1295
|
names: [COMMENT, '#', ENDCOMMENT],
|
|
1366
|
-
async compile({
|
|
1296
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1367
1297
|
// inline comment
|
|
1368
1298
|
if (name === '#') {
|
|
1369
1299
|
if (!value.value || out.options.stripComments) {
|
|
@@ -1392,7 +1322,7 @@ ${output
|
|
|
1392
1322
|
*/
|
|
1393
1323
|
const tag$5 = {
|
|
1394
1324
|
names: [EXPRESSION],
|
|
1395
|
-
compile({
|
|
1325
|
+
compile({ tag: { value }, ctx: { context }, out }) {
|
|
1396
1326
|
return out.pushVar(compiler.compile(value, context, FILTERS));
|
|
1397
1327
|
},
|
|
1398
1328
|
};
|
|
@@ -1407,15 +1337,16 @@ ${output
|
|
|
1407
1337
|
*/
|
|
1408
1338
|
const tag$4 = {
|
|
1409
1339
|
names: [FOR, BREAK, CONTINUE, ENDFOR],
|
|
1410
|
-
async compile({
|
|
1340
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1411
1341
|
if (name === FOR) {
|
|
1412
1342
|
if (!value) {
|
|
1413
1343
|
throw new Error(`"${FOR}" tag must have expression`);
|
|
1414
1344
|
}
|
|
1415
1345
|
ctx.expect(ENDFOR);
|
|
1416
1346
|
const { context } = ctx;
|
|
1417
|
-
|
|
1418
|
-
|
|
1347
|
+
const { type, elements } = value.left;
|
|
1348
|
+
if (type === 'SEQ') {
|
|
1349
|
+
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, elements.map(({ value }) => value).join(','), `};`);
|
|
1419
1350
|
}
|
|
1420
1351
|
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.value},`, `};`);
|
|
1421
1352
|
}
|
|
@@ -1450,7 +1381,7 @@ ${output
|
|
|
1450
1381
|
*/
|
|
1451
1382
|
const tag$3 = {
|
|
1452
1383
|
names: [IF, ELIF, ELSE, ENDIF],
|
|
1453
|
-
async compile({
|
|
1384
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1454
1385
|
if (name === IF) {
|
|
1455
1386
|
if (!value) {
|
|
1456
1387
|
throw new Error(`"${IF}" tag must have expression`);
|
|
@@ -1487,25 +1418,29 @@ ${output
|
|
|
1487
1418
|
const MACRO = 'macro';
|
|
1488
1419
|
const CALLER = 'caller';
|
|
1489
1420
|
const ENDMACRO = 'endmacro';
|
|
1421
|
+
const CALLER_PN = '_c';
|
|
1490
1422
|
/**
|
|
1491
1423
|
* @example {{ macro my_macro = (x, y) }}...{{ caller }}{{ endmacro }}{{ my_macro("foo", 1) }}
|
|
1492
1424
|
*/
|
|
1493
1425
|
const tag$2 = {
|
|
1494
1426
|
names: [MACRO, CALLER, ENDMACRO],
|
|
1495
|
-
async compile({
|
|
1427
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1496
1428
|
if (name === MACRO) {
|
|
1497
1429
|
if (value?.type !== 'SET') {
|
|
1498
1430
|
throw new Error(`"${MACRO}" tag must have "SET" expression`);
|
|
1499
1431
|
}
|
|
1500
1432
|
ctx.expect(ENDMACRO);
|
|
1501
1433
|
const { context } = ctx;
|
|
1502
|
-
|
|
1434
|
+
const { elements } = value.right;
|
|
1435
|
+
return out.pushLine(`${compiler.compile(value, context, FILTERS)}=>async(${CALLER_PN})=>{`, `const ${ctx.in()}={`, `...${context},`, elements.map(el => el.type === 'SET'
|
|
1436
|
+
? el.left.value
|
|
1437
|
+
: el.value).join(','), `};`);
|
|
1503
1438
|
}
|
|
1504
1439
|
if (name === CALLER) {
|
|
1505
1440
|
if (!ctx.matchAny(ENDMACRO)) {
|
|
1506
1441
|
throw new Error(`"${CALLER}" tag must inside a "${MACRO}" tag`);
|
|
1507
1442
|
}
|
|
1508
|
-
return out.pushLine(`await
|
|
1443
|
+
return out.pushLine(`await ${CALLER_PN}?.();`);
|
|
1509
1444
|
}
|
|
1510
1445
|
if (name === ENDMACRO) {
|
|
1511
1446
|
if (!ctx.consume(ENDMACRO)) {
|
|
@@ -1523,7 +1458,7 @@ ${output
|
|
|
1523
1458
|
*/
|
|
1524
1459
|
const tag$1 = {
|
|
1525
1460
|
names: [RAW],
|
|
1526
|
-
async compile({
|
|
1461
|
+
async compile({ tag: { raw, previous, next }, out }) {
|
|
1527
1462
|
return out.pushStr(raw, {
|
|
1528
1463
|
trimStart: previous?.stripAfter,
|
|
1529
1464
|
trimEnd: next?.stripBefore,
|
|
@@ -1539,7 +1474,7 @@ ${output
|
|
|
1539
1474
|
*/
|
|
1540
1475
|
const tag = {
|
|
1541
1476
|
names: [SET, ENDSET],
|
|
1542
|
-
async compile({
|
|
1477
|
+
async compile({ tag: { name, value }, ctx, out }) {
|
|
1543
1478
|
if (name === SET) {
|
|
1544
1479
|
if (!value) {
|
|
1545
1480
|
throw new Error(`"${SET}" tag must have expression`);
|
|
@@ -1647,30 +1582,29 @@ ${output
|
|
|
1647
1582
|
let match;
|
|
1648
1583
|
while ((match = tagRe.exec(template))) {
|
|
1649
1584
|
if (match.index > this.index) {
|
|
1650
|
-
this.
|
|
1585
|
+
this.addRaw(this.index, match.index);
|
|
1651
1586
|
}
|
|
1652
|
-
await this.
|
|
1587
|
+
await this.addTag(match);
|
|
1653
1588
|
this.index = match.index + match[0].length;
|
|
1654
1589
|
}
|
|
1655
1590
|
if (this.index < template.length) {
|
|
1656
|
-
this.
|
|
1591
|
+
this.addRaw(this.index, template.length);
|
|
1657
1592
|
}
|
|
1658
1593
|
this.cursor = this.first;
|
|
1659
1594
|
this.rebuildBlocks();
|
|
1660
1595
|
return this.cursor;
|
|
1661
1596
|
}
|
|
1662
|
-
|
|
1663
|
-
const raw = this.template.slice(start, end);
|
|
1597
|
+
addRaw(start, end) {
|
|
1664
1598
|
this.push({
|
|
1665
1599
|
name: RAW$1,
|
|
1666
|
-
raw,
|
|
1600
|
+
raw: this.template.slice(start, end),
|
|
1667
1601
|
previous: null,
|
|
1668
1602
|
next: null,
|
|
1669
1603
|
start,
|
|
1670
1604
|
end,
|
|
1671
1605
|
});
|
|
1672
1606
|
}
|
|
1673
|
-
async
|
|
1607
|
+
async addTag(match) {
|
|
1674
1608
|
const start = match.index;
|
|
1675
1609
|
const end = start + match[0].length;
|
|
1676
1610
|
const base = {
|
|
@@ -1687,7 +1621,7 @@ ${output
|
|
|
1687
1621
|
...base,
|
|
1688
1622
|
name: match[2],
|
|
1689
1623
|
value: {
|
|
1690
|
-
type: '
|
|
1624
|
+
type: 'LIT',
|
|
1691
1625
|
value: unescapeTag(match[3]),
|
|
1692
1626
|
},
|
|
1693
1627
|
});
|
|
@@ -1718,7 +1652,7 @@ ${output
|
|
|
1718
1652
|
}
|
|
1719
1653
|
async layout(value) {
|
|
1720
1654
|
const exp = parser.parse(value);
|
|
1721
|
-
if (exp?.type !== '
|
|
1655
|
+
if (exp?.type !== 'LIT' || typeof exp.value !== 'string' || !exp.value) {
|
|
1722
1656
|
throw new ParseError(`"layout" tag must have a file path`, {
|
|
1723
1657
|
source: this.template,
|
|
1724
1658
|
range: {
|
|
@@ -1731,7 +1665,7 @@ ${output
|
|
|
1731
1665
|
}
|
|
1732
1666
|
async include(value) {
|
|
1733
1667
|
const exp = parser.parse(value.replace(/\?$/, ''));
|
|
1734
|
-
if (exp?.type !== '
|
|
1668
|
+
if (exp?.type !== 'LIT' || typeof exp.value !== 'string' || !exp.value) {
|
|
1735
1669
|
throw new ParseError(`"include" tag must have a file path`, {
|
|
1736
1670
|
source: this.template,
|
|
1737
1671
|
range: {
|
|
@@ -1762,9 +1696,9 @@ ${output
|
|
|
1762
1696
|
cursor = cursor.next;
|
|
1763
1697
|
}
|
|
1764
1698
|
}
|
|
1765
|
-
push(
|
|
1766
|
-
if (
|
|
1767
|
-
const { type, value } =
|
|
1699
|
+
push(tag) {
|
|
1700
|
+
if (tag.name === BLOCK) {
|
|
1701
|
+
const { type, value } = tag.value;
|
|
1768
1702
|
if (type !== 'ID') {
|
|
1769
1703
|
throw new ParseError(`"${BLOCK}" tag must have a title`, {
|
|
1770
1704
|
source: this.template,
|
|
@@ -1777,31 +1711,31 @@ ${output
|
|
|
1777
1711
|
this.group = value;
|
|
1778
1712
|
// child blocks
|
|
1779
1713
|
if (this.blocks[value]) {
|
|
1780
|
-
this.blocks[value].push([
|
|
1714
|
+
this.blocks[value].push([tag]);
|
|
1781
1715
|
return;
|
|
1782
1716
|
}
|
|
1783
1717
|
// parent block
|
|
1784
|
-
this.blocks[value] = [[
|
|
1718
|
+
this.blocks[value] = [[tag]];
|
|
1785
1719
|
}
|
|
1786
1720
|
else if (this.group) {
|
|
1787
1721
|
const chunks = this.blocks[this.group];
|
|
1788
|
-
if (
|
|
1722
|
+
if (tag.name === ENDBLOCK) {
|
|
1789
1723
|
this.group = '';
|
|
1790
1724
|
}
|
|
1791
|
-
chunks.at(-1).push(
|
|
1725
|
+
chunks.at(-1).push(tag);
|
|
1792
1726
|
// child blocks
|
|
1793
1727
|
if (chunks.length > 1) {
|
|
1794
1728
|
return;
|
|
1795
1729
|
}
|
|
1796
1730
|
}
|
|
1797
1731
|
if (this.cursor) {
|
|
1798
|
-
|
|
1799
|
-
this.cursor.next =
|
|
1732
|
+
tag.previous = this.cursor;
|
|
1733
|
+
this.cursor.next = tag;
|
|
1800
1734
|
}
|
|
1801
1735
|
else {
|
|
1802
|
-
this.first =
|
|
1736
|
+
this.first = tag;
|
|
1803
1737
|
}
|
|
1804
|
-
this.cursor =
|
|
1738
|
+
this.cursor = tag;
|
|
1805
1739
|
}
|
|
1806
1740
|
/**
|
|
1807
1741
|
* @note Block tags are hoisted to top level
|
|
@@ -1839,29 +1773,29 @@ ${output
|
|
|
1839
1773
|
return [];
|
|
1840
1774
|
}
|
|
1841
1775
|
const chunk = [];
|
|
1842
|
-
child.forEach((
|
|
1843
|
-
if (
|
|
1776
|
+
child.forEach((tag) => {
|
|
1777
|
+
if (tag.name === SUPER) {
|
|
1844
1778
|
this.rebuildBlockBody(parents.pop(), parents).forEach(t => chunk.push(t));
|
|
1845
1779
|
}
|
|
1846
1780
|
else {
|
|
1847
|
-
chunk.push(
|
|
1781
|
+
chunk.push(tag);
|
|
1848
1782
|
}
|
|
1849
1783
|
});
|
|
1850
1784
|
return chunk;
|
|
1851
1785
|
}
|
|
1852
|
-
rebuildBlockChain(
|
|
1853
|
-
for (let i = 0; i <
|
|
1854
|
-
|
|
1855
|
-
|
|
1786
|
+
rebuildBlockChain(tags, previous, next) {
|
|
1787
|
+
for (let i = 0; i < tags.length; i++) {
|
|
1788
|
+
tags[i].previous = tags[i - 1] || previous;
|
|
1789
|
+
tags[i].next = tags[i + 1] || next;
|
|
1856
1790
|
}
|
|
1857
1791
|
if (previous) {
|
|
1858
|
-
[previous.next] =
|
|
1792
|
+
[previous.next] = tags;
|
|
1859
1793
|
}
|
|
1860
1794
|
if (next) {
|
|
1861
|
-
next.previous =
|
|
1795
|
+
next.previous = tags[tags.length - 1];
|
|
1862
1796
|
}
|
|
1863
1797
|
if (!this.first) {
|
|
1864
|
-
[this.first] =
|
|
1798
|
+
[this.first] = tags;
|
|
1865
1799
|
this.cursor = this.first;
|
|
1866
1800
|
}
|
|
1867
1801
|
}
|