/
renderer.js
212 lines (166 loc) · 4.18 KB
/
renderer.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
var BUFFER_LENGTH = 1024 * 8;
var parser = require('./parser');
exports.render = render;
function render(tokens, context, partials, stream, callback) {
if (!Array.isArray(context)) {
context = [context];
}
return _render(tokens, context, partials, stream, callback);
}
function _render(tokens, context, partials, stream, callback) {
if (tokens[0] !== 'multi') {
throw new Error('Mu - WTF did you give me? I expected mustache tokens.');
}
var i = 1;
function next() {
try {
if (stream.paused) {
stream.once('resumed', function () {
process.nextTick(next);
});
return;
}
var token = tokens[i++];
if (!token) {
return callback ? callback() : true;
}
switch (token[0]) {
case 'static':
stream.emit('data', token[2]);
return next();
case 'mustache':
switch (token[1]) {
case 'utag': // Unescaped Tag
stream.emit('data', s(normalize(context, token[2])));
return next();
case 'etag': // Escaped Tag
stream.emit('data', escape(s(normalize(context, token[2]))));
return next();
case 'section':
var res = normalize(context, token[2], token[3]);
if (res) {
return section(context, token[2], res, token[4], partials, stream, next);
} else {
return next();
}
case 'inverted_section':
var res = normalize(context, token[2], token[3]);
if (!res || res.length === 0) {
return section(context, token[2], true, token[4], partials, stream, next);
} else {
return next();
}
case 'partial':
var partial = partials[token[2]];
// console.log(require('util').inspect(partials));
if (partial) {
return render(partial[0].tokens, context, partials, stream, next);
} else {
return next();
}
}
}
} catch (err) {
stream.emit('error', err);
next();
}
}
next();
}
function s(val) {
return typeof val === 'undefined' ? '' : val.toString();
}
function escape(string) {
return string.replace(/[&<>"]/g, escapeReplace);
}
function normalize(context, name, body) {
var val = walkToFind(context, name);
if (typeof(val) === 'function') {
val = val.call(smashContext(context), body);
}
return val;
}
function walkToFind(context, name) {
var i = context.length;
var names = name.split('.');
var current;
walk:
while (i--) {
current = context[i];
for (var j = 0; j < names.length; j++) {
if (names[j] in current) {
current = current[names[j]];
} else {
continue walk;
}
}
return current;
}
return undefined;
}
// TODO: if Proxy, make more efficient
// TODO: cache?
function smashContext(context) {
var obj = {};
for (var i = 0; i < context.length; i++) {
var level = context[i];
for (var k in level) {
obj[k] = level[k];
}
}
return obj;
}
function section(context, name, val, tokens, partials, stream, callback) {
if (val instanceof Array) {
var i = 0;
(function next() {
var item = val[i++];
if (item) {
context.push(item);
_render(tokens, context, partials, stream, function () {
context.pop();
next();
});
} else {
callback();
}
}());
return;
}
if (typeof val === 'object') {
context.push(val);
_render(tokens, context, partials, stream, function () {
context.pop();
callback();
});
return;
}
if (val) {
return _render(tokens, context, partials, stream, callback);
}
return callback();
}
//
//
//
function findInContext(context, key) {
var i = context.length;
while (i--) {
if (context[i][key]) {
return context[i][key];
}
}
return undefined;
}
//
//
//
function escapeReplace(char) {
switch (char) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '"': return '"';
default: return char;
}
}