-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
558 lines (557 loc) · 20.1 KB
/
bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// deno-fmt-ignore-file
// deno-lint-ignore-file
// This code was bundled using `deno bundle` and it's not recommended to edit it manually
const ITEM_PARSING_REGEX = /\{{(.*?)}}/g;
const BLOCK_PARSING_REGEX = /{{\/(?<block_close>.*?)}}|{{#else}}|{{#(?<block_start>.*?) (?<block_value>.*?)}}/gms;
function parseValues(values) {
return values.match(/[a-z_]\w*(?!\w*\s*\()/gi)?.filter((item, index, self)=>{
if (item == "true" || item == "false") return false;
return true;
}) ?? [];
}
function parseString(template) {
const items = [];
let template_left = template;
let m;
let true_index = 0;
const regex = new RegExp(ITEM_PARSING_REGEX, "gms");
while((m = regex.exec(template_left)) != null){
if (!m) break;
const start = m.index;
const end = m.index + m[0].length;
const split = m[1].split(" "), action = split[0], key = split[1];
if (action == ">") {
const item = {
type: "layout",
content: key,
index: start,
index_end: true_index + end
};
const before = template_left.substring(0, item.index);
if (regex.test(before)) {
items.push({
title: "before_block",
content: parseString(before),
type: "list",
index: true_index,
index_end: true_index + before.length
});
} else {
items.push({
title: "before_block",
content: before,
type: "string",
index: true_index,
index_end: true_index + before.length
});
}
item.index = true_index + start;
items.push(item);
true_index = end + true_index;
template_left = template_left.substring(end) || "";
continue;
}
if (m[0].startsWith("{{!--") && m[0].endsWith("--}}")) {
template_left = template_left.replace(m[0], "");
continue;
}
let fn;
try {
fn = new Function("data", "return this." + (key ? key : m[1]));
} catch (error) {
throw new Error(("Error in string: " + m[0] + "\n" + error?.message) ?? "");
}
const item = {
type: "var",
content: m[0],
var: m[1],
fn: fn,
helper: key ? action : undefined,
key: key,
index: start,
index_end: true_index + end
};
const before = template_left.substring(0, item.index);
if (regex.test(before)) {
items.push({
title: "before_block",
content: parseString(before),
type: "list",
index: true_index,
index_end: true_index + before.length
});
} else {
items.push({
title: "before_block",
content: before,
type: "string",
index: true_index,
index_end: true_index + before.length
});
}
item.index = true_index + start;
items.push(item);
true_index = end + true_index;
template_left = template_left.substring(end) || "";
}
items.sort((a, b)=>a.index - b.index);
items.push({
title: "before_block",
content: template_left,
type: "string",
index: true_index,
index_end: true_index + template_left?.length
});
return items;
}
function parseIfBlock(template) {
const blocks = [];
const item_order = [];
let first_if;
let temp_block = [];
function addItem(type, m) {
const start = m.index;
const test = parseValues(m.groups?.block_value ?? "");
const item = {
index: start,
length: m[0].length,
type: type,
condition: m.groups?.block_value,
values: test.map((item)=>{
return {
name: item,
fn: new Function("data", "return this." + item)
};
})
};
item_order.push(item);
}
const open_if = template.matchAll(new RegExp(BLOCK_PARSING_REGEX, "g"));
for (const m of open_if){
if (m.groups?.block_start == "if") {
if (!first_if) {
first_if = m;
addItem("if", m);
} else {
temp_block.push(m);
}
} else if (m.groups?.block_start == "elseif") {
if (temp_block.length == 0) {
addItem("elseif", m);
}
} else if (m.groups?.block_close == "if") {
if (temp_block.length == 0) {
addItem("ifclose", m);
} else {
temp_block.pop();
}
} else if (!m.groups?.block_start && !m.groups?.block_close) {
if (temp_block.length == 0) {
addItem("else", m);
}
}
}
item_order.sort((a, b)=>a.index - b.index);
if (item_order.length == 2 && item_order[0].type == "if" && item_order[1].type == "ifclose") {
let content = template.substring(item_order[0].index + item_order[0].length, item_order[1].index);
const test = parseValues(item_order[0].condition ?? "");
let fn;
try {
fn = new Function(...test, `return ${item_order[0].condition};`);
} catch (error) {
throw new Error(("Error in condition: " + item_order[0].condition + "\n" + error?.message) ?? "");
}
blocks.push({
type: "if",
condition: fn ?? undefined,
content: parse(content),
str_condition: item_order[0].condition,
values: test.map((item)=>{
return {
name: item,
fn: new Function("data", "return this." + item)
};
})
});
return blocks;
}
temp_block = [];
for (const item of item_order){
switch(item.type){
case "if":
if (temp_block.length == 0) {
temp_block.push(item);
}
break;
case "elseif":
if (temp_block.length == 1) {
const lastitem = temp_block.pop();
if (lastitem) {
const content = template.substring(lastitem.index + lastitem.length, item.index).trim();
const test = parseValues(lastitem.condition ?? "");
let fn;
try {
fn = new Function(...test, `return !!(${lastitem.condition})`);
} catch (error) {
throw new Error(("Error in condition: " + lastitem.condition + "\n" + error?.message) ?? "");
}
blocks.push({
type: lastitem.type,
condition: fn ?? undefined,
content: parse(content),
str_condition: lastitem.condition,
values: test.map((item)=>{
return {
name: item,
fn: new Function("data", "return this." + item)
};
})
});
}
}
temp_block.push(item);
break;
case "else":
if (temp_block.length == 1) {
const lastitem = temp_block.pop();
if (lastitem) {
const content = template.substring(lastitem.index + lastitem.length, item.index).trim();
const test = parseValues(lastitem.condition ?? "");
let fn;
try {
fn = new Function(...test, `return !!(${lastitem.condition})`);
} catch (error) {
throw new Error(("Error in condition: " + lastitem.condition + "\n" + error?.message) ?? "");
}
blocks.push({
type: lastitem.type,
condition: lastitem.condition ? fn : undefined,
content: parse(content),
str_condition: lastitem.condition,
values: test.map((item)=>{
return {
name: item,
fn: new Function("data", "return this." + item)
};
})
});
}
temp_block.push(item);
}
break;
case "ifclose":
if (temp_block.length == 1) {
const lastitem = temp_block.pop();
if (lastitem) {
let content = lastitem.type == "if" ? template.substring(lastitem.index + lastitem.length, item.index + item.length) : template.substring(lastitem.index + lastitem.length, item.index);
const test = parseValues(lastitem.condition ?? "");
blocks.push({
type: lastitem.type,
condition: lastitem.condition ? new Function(...test, `return !!(${lastitem.condition})`) : undefined,
content: parse(content),
str_condition: lastitem.condition,
values: test.map((item)=>{
return {
name: item,
fn: new Function("data", "return this." + item)
};
})
});
}
}
break;
default:
break;
}
}
return blocks;
}
function parse(template) {
const blocks = [];
const items = [];
let template_left = template;
const regex = new RegExp(BLOCK_PARSING_REGEX, "gms");
const match = template.matchAll(regex);
let first_block;
let closing_block;
const temp_block = [];
for (const m of match){
const groups = m.groups;
if (groups?.block_start && !first_block) {
first_block = m;
continue;
}
if (groups?.block_start && groups?.block_start !== "elseif") {
temp_block.push(m);
continue;
}
if (groups?.block_close && temp_block.length > 0) {
temp_block.pop();
continue;
}
if (groups?.block_close && first_block && temp_block.length == 0) {
closing_block = m;
continue;
}
}
if (first_block && closing_block) {
switch(first_block.groups?.block_start){
case "if":
{
const content = template.substring(first_block?.index, (closing_block?.index) + closing_block.length + first_block?.length);
blocks.push({
block_start: first_block.groups?.block_start,
block_value: first_block.groups?.block_value,
block_content: parseIfBlock(content),
index: first_block?.index,
index_end: (closing_block?.index) + closing_block.length + first_block?.length
});
break;
}
case "each":
{
const content = template.substring((first_block?.index) + first_block[0].length, closing_block.index);
blocks.push({
block_start: first_block.groups?.block_start,
block_value: first_block.groups?.block_value,
fn: first_block.groups?.block_value ? new Function("data", `return this.${first_block.groups?.block_value}`) : undefined,
block_content: parse(content),
index: first_block?.index,
index_end: closing_block.index + closing_block?.length + first_block[0].length
});
}
}
}
for(let index = 0; index < blocks.length; index++){
const block = blocks[index];
let before = template_left.substring(0, block.index);
items.push({
title: "before_var",
content: parseString(before),
type: "list"
});
items.push({
title: "block",
content: block,
type: block.block_start == "each" ? "each" : "block"
});
template_left = template_left.substring(block.index_end - 1);
if (regex.test(template_left)) {
items.push({
title: "block",
content: parse(template_left.trim()),
type: "item"
});
continue;
}
if (index === blocks.length - 1) {
items.push({
title: "after_var",
content: parseString(template_left.trim()),
type: "list"
});
}
}
if (blocks.length === 0) {
items.push({
title: "content",
content: parseString(template_left),
type: "list"
});
}
const root = {
title: "list",
childs: items,
type: "items"
};
return root;
}
const ESCAPE_REGEX = /[&<>'"]/g;
const helpers = {};
function registerHelper(name, fn) {
helpers[name] = fn;
}
const entity = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"`": "`"
};
function replaceChar(c) {
let result = "";
let text_left = c;
let m;
while(m = ESCAPE_REGEX.exec(text_left)){
result += text_left.slice(0, m.index) + entity[m[0]];
text_left = text_left.slice(m.index + 1);
}
return result + text_left;
}
function escape(text) {
if (!ESCAPE_REGEX.test(text)) return text;
return replaceChar(text);
}
const COMPILE_OPTIONS = {
escape: true
};
const layouts = new Map();
class renderObject {
template;
options;
compiled;
data;
current_data;
render_cache;
constructor(template, options = COMPILE_OPTIONS){
this.template = template;
this.options = COMPILE_OPTIONS;
this.render_cache = {};
this.layouts = (name)=>{
const layout = layouts.get(name);
if (!layout) throw new Error("Layout not found: " + name);
return layout(this.current_data ?? this.data);
};
this.stringCache = (item)=>{
const name = item.var;
return this.render_cache[name] = helpers[item.helper] ? ()=>helpers[item.helper](this.getData(item?.fn)) : ()=>{
const val = name == "this" ? this.current_data || this.data : this.getData(item?.fn);
return this.options.escape && typeof val == "string" ? escape(val) : val;
};
};
this.renderString = (item)=>{
return this.render_cache[item.var]?.() ?? this.stringCache(item)();
};
this.renderBlock = (block)=>{
switch(block.block_start){
case "if":
{
for(let index = 0; index < block.block_content.length; index++){
const item = block.block_content[index];
switch(item.condition){
case undefined:
return this.render(item.content);
default:
try {
const data = item.values.map((v)=>this.getData(v.fn));
if (item.condition.apply(this, data)) {
return this.render(item.content);
}
} catch (error) {
console.error("Error in condition: " + item.str_condition);
}
break;
}
}
return "";
}
default:
throw new Error("Unknown block type: " + block.block_start);
}
};
this.renderForeach = (block)=>{
let result = "";
const old_data = this.current_data;
const isThis = block.block_value == "this";
const value = isThis ? this.data : this.getData(block?.fn);
if (!Array.isArray(value)) throw new Error("each value is not an array");
for(const key in value){
if (isThis) {
this.current_data = value[key];
} else {
this.current_data = Object.assign({}, old_data, typeof value[key] != "object" ? {
[block.block_value.split(".").pop()]: value[key]
} : value[key]);
}
result += this.render(block.block_content);
}
this.current_data = old_data;
return result;
};
this.render = (tree)=>{
let html = "";
switch(tree.type){
case "layout":
html += this.layouts(tree.content);
break;
case "block":
html += this.renderBlock(tree.content);
break;
case "each":
html += this.renderForeach(tree.content);
break;
case "string":
html += tree.content;
break;
case "var":
html += this.renderString(tree);
break;
case "list":
for (const item of tree.content){
html += this.render(item);
}
break;
case "items":
for (const item of tree.childs){
html += this.render(item);
}
break;
case "item":
html += this.render(tree.content);
break;
default:
html += tree.content;
break;
}
return html;
};
this.start = (data)=>{
this.data = data;
return this.render(this.compiled);
};
this.options = options;
this.compiled = typeof template == "string" ? parse(template) : template;
}
getData(fn) {
let data;
if (typeof fn == "function") {
try {
return fn.apply(this.current_data ?? this.data);
} catch (_e) {
return fn.apply(this.data);
}
}
return data;
}
layouts;
stringCache;
renderString;
renderBlock;
renderForeach;
render;
start;
}
function compile(template, options = COMPILE_OPTIONS) {
const compiled = new renderObject(template, options);
return compiled.start;
}
function registerLayout(name, content) {
layouts.set(name, compile(content));
}
const compiled_list = new Map();
function renderTemplate(key, data, template) {
if (compiled_list.has(key)) {
return compiled_list.get(key)(data);
}
const compiled = compile(template);
compiled_list.set(key, compiled);
return compiled(data);
}
registerHelper("JSON", (data)=>{
return JSON.stringify(data);
});
registerHelper("raw", (data)=>{
return data;
});
export { renderTemplate as renderTemplate, compile as compile, registerHelper as registerHelper, registerLayout as registerLayout };