-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (187 loc) · 5.93 KB
/
index.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
var Stream = require('stream');
var configureEngine = require('./lib/configure-engine');
var getTemplate = require('./lib/get-template');
var checkNodeType = require('./lib/check-node-type');
var areChildren = checkNodeType.areChildren;
var isChild = checkNodeType.isChild;
var isRaw = checkNodeType.isRaw;
var fillMold = require('./lib/fill-mold');
var normalizeCondition = require('./lib/normalize-condition');
var indentHtml = require('./lib/indent-html');
function renderTemplate(template, fillers, options) {
var html = '';
options = configureEngine(template, options);
if(options.doctype) html += options.doctype;
// Consider raws as template filename
if(isRaw(template)) {
template = getTemplate(template.toString(), 'views', options);
template = JSON.parse(template);
}
if(areChildren(template)) template = {'in' : template};
if(options.wrap !== false) {
options.wrapped = template;
if(!options.wrap) options.wrap = options.defaultWrap;
var wrap = getTemplate(options.wrap, 'wraps', options);
wrap = JSON.parse(wrap);
// Start recursive nodes inspection from wrap
html += inspectNode(wrap, fillers, options);
} else {
// Start recursive nodes inspection from template
html += inspectNode(template, fillers, options);
}
// Delete first EOL when pretty is enabled and doctype is disabled
if(options.pretty && !options.doctype) html = html.substr(1);
if(options.out === 'buffer') return new Buffer(html);
if(options.out === 'stream') {
var stream = new Stream.Readable();
stream.push(html, 'utf8');
stream.push(null);
return stream;
}
return html;
}
function inspectNode(node, fillers, options) {
// Node local configuration
if(node.options) options = configureEngine(node, options);
// HTML tag
if(node.tag) return buildTag(node, fillers, options);
// API key
if(node.api) return inspectApi(node, fillers, options);
// Node tree
if(node.in) return inspectNodeTree(node, fillers, options);
return '';
}
function inspectApi(node, fillers, options) {
switch(node.api) {
case 'if' :
if(node.in.constructor !== Array) node.in = [node.in];
for(var i = 0, e = node.in.length; i < e; i++) {
if(node.in[i].default) {
return inspectNodeTree(node.in[i], fillers, options);
}
var condition = normalizeCondition(node.in[i].condition, options.alias);
if(eval(condition)) {
return inspectNodeTree(node.in[i], fillers, options);
}
}
return '';
break;
case 'switch' :
var expression = normalizeCondition(node.expression, options.alias);
if(node.in.constructor !== Array) node.in = [node.in];
for(var i = 0, e = node.in.length; i < e; i++) {
if(node.in[i].default) {
return inspectNodeTree(node.in[i], fillers, options);
}
var expressionValue = eval(expression);
var caseValue = node.in[i].case;
if(caseValue === String) handleRaw(caseValue, fillers, {'alias' : options.alias});
if(expressionValue === caseValue) {
return inspectNodeTree(node.in[i], fillers, options);
}
}
return '';
break;
case 'each' :
var html = '';
var array = fillers[node.array];
if(!node.item) node.item = 'item';
array.forEach(function(item) {
fillers[node.item] = item;
html += inspectNodeTree(node, fillers, options);
});
return html;
break;
case 'repeat' :
var html = '';
if(!node.current) node.current = 'current';
if(node.number.constructor === String) {
node.number = handleRaw(node.number, fillers, {
'alias' : options.alias
});
}
for(var i = 1, e = node.number; i <= e; i++) {
fillers[node.current] = i;
html += inspectNodeTree(node, fillers, options);
}
return html;
break;
case 'inlay' :
// Prevent infinite inclusion
if(options.inlayed.indexOf(node.name) !== -1) return '';
// Create non-reference options object for descendant nodes
var subOptions = {};
Object.keys(options).forEach(function(key) {
subOptions[key] = options[key];
});
var inlayed = [];
inlayed.concat(options.inlayed);
inlayed.push(node.name);
subOptions.inlayed = inlayed;
var template = getTemplate(node.name, 'inlays', options);
// Handle raw inclusions
if(node.raw) {
// Remove last EOL
var lastEOL = /\n$/;
template = template.replace(lastEOL, '');
return handleRaw(template, fillers, options);
}
template = JSON.parse(template);
return inspectNode(template, fillers, subOptions);
break;
case 'wrap' :
if(options.wrapped) {
var wrapped = options.wrapped;
options.wrapped = null;
return inspectNode(wrapped, fillers, options);
}
break;
}
return '';
}
function inspectNodeTree(node, fillers, options) {
var html = '';
if(!node.in) return html;
if(areChildren(node.in)) {
node.in.forEach(function(child) {
if(isRaw(child)) return html += handleRaw(child, fillers, options);
html += inspectNode(child, fillers, options);
});
return html;
}
if(isChild(node.in)) return html += inspectNode(node.in, fillers, options);
if(isRaw(node.in)) return html += handleRaw(node.in, fillers, options);
return html;
}
function handleRaw(raw, fillers, options) {
var html = '';
if(options.pretty) html += indentHtml(options);
return html += fillMold(raw, fillers, options.alias);
}
function buildTag(node, fillers, options) {
var html = '';
if(options.pretty) html += indentHtml(options);
// Build start tag
html += '<' + node.tag;
// Append attributes
if(node.attributes) {
for(var attribute in node.attributes) {
html += ' ' + attribute + '="' + node.attributes[attribute] + '"';
}
}
// Close start tag
html += '>';
// Handle childless tag
if(options.voids.indexOf(node.tag) != -1 || !node.in) return html;
if(options.pretty) options.depth++;
// Handle tag children
html += inspectNodeTree(node, fillers, options);
if(options.pretty) {
options.depth--;
html += indentHtml(options);
}
// Build end tag
html += '</' + node.tag + '>';
return html;
}
module.exports = renderTemplate;