-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
97 lines (85 loc) · 2.43 KB
/
utils.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
'use strict';
const ast = require('node-mus/lib/compile/ast');
const constant = require('node-mus/lib/compile/constant');
const regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g;
module.exports = {
astTreeToString(astTree, process) {
let html = '';
if (!astTree) {
return html;
}
const astNodeToString = ast => {
let html = `${ast._ast.blockStart} ${ast.tag}`;
const expr = ast.text.trim();
html += expr ? ` ${expr}` : '';
html += ' ' + ast._ast.blockEnd;
ast.innerText = this.astTreeToString(ast.children, process);
return html + ast.innerText;
};
astTree.forEach(ast => {
let fragment = '';
switch (ast.type) {
case constant.TYPE_TAG:
fragment += astNodeToString(ast);
break;
case constant.TYPE_TEXT:
fragment += ast.text;
break;
case constant.TYPE_VAR:
fragment += `${ast._ast.variableStart} ${ast.text.trim()} ${ast._ast.variableEnd}`;
break;
default:
break;
}
if (ast.elseifBlock) {
ast.elseifBlock.forEach(_a => {
fragment += astNodeToString(_a);
});
}
if (ast.elseBlock) {
fragment += astNodeToString(ast.elseBlock);
}
if (!ast.isUnary && ast.type === constant.TYPE_TAG) {
fragment += `${ast._ast.blockStart} end${ast.tag} ${ast._ast.blockEnd}`;
}
html += process
? process(fragment, ast)
: fragment;
});
return html;
},
parseTemplate(str, options = {}) {
// parse template by node-mus
/* istanbul ignore next */
return ast(str, Object.assign(options, {
processor: Object.assign({
comment() {},
pagelet() {},
require(el) { el.isUnary = true; },
datalet(el) { el.isUnary = true; },
html() {},
head() {},
body() {},
title() {},
for() {},
uri(el) { el.isUnary = true; },
script() {},
variable() {},
ATF(el) { el.isUnary = true; },
macro() {},
extends(el) { el.isUnary = true; },
block() {},
include(el) { el.isUnary = true; },
import(el) { el.isUnary = true; },
autoescape() {},
verbatim() {},
call() {},
parent(el) { el.isUnary = true; },
spaceless() {},
}, options.processor),
})).root;
},
reStringFormat(str) {
return str.replace(regexEscapeRE, '\\$&');
},
};