-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathashe.js
273 lines (239 loc) · 5.99 KB
/
ashe.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
/**
* Ashe.js is a smart, fast, eval-less javascript templating library.
*/
(function() {
"use strict";
var uid, tokens,
/**
* Recursive parsing method. Parse template string in context of provided object data.
* @param {String} str Template string.
* @param {Object} data Data for template.
*/
process = function(str, data) {
return str.replace(/\{_(\d+?)\}/g, function(a, b) {
var token = tokens[b], repl, i;
if (!token.expr) {
repl = evl(data, tokens[b].buffer);
if (token.modif.length) {
for (i in token.modif) {
var modif = token.modif[i],
params = [],
check = token.modif[i].match(/(\w+)\(([\s\S]+)\)/);
if (check) {
modif = check[1];
params = check[2].split(/\s*,\s*/);
}
params.unshift(repl);
modif = Ashe.modifiers[modif] || window[modif];
if (typeof modif != 'function') {
throw new Error('Ashe: Unknown modifier "' + token.modif[i] + '".');
}
repl = modif.apply(this, params);
}
}
return repl;
}
else {
var block;
switch (token.expr.type) {
case 'if':
var cond = evl(data, token.expr.cond);
block = token.buffer.match(cond
? /\{%\s*if\s+.+?\s*%\}([\s\S]*?)\{%/i
: /\{%\s*else\s*%\}([\s\S]*?)\{%/i
);
return block ? process(block[1], data) : '';
case 'for':
var loopData = evl(data, token.expr.list);
if (typeof loopData == 'undefined') {
if (Ashe.debug) {
throw new Error('Ashe: Undefined list "' + token.expr.list + '".');
}
return '';
}
if (hasElements(loopData)) {
block = token.buffer.match(/\{%\s*for.*?\s*%\}([\s\S]*?)\{%/i);
if (block) {
var key, k,
elem = token.expr.elem,
split = elem.split(/\s*,\s*/),
subStr = '';
if (split.length == 2) {
key = split[0];
elem = split[1];
}
for (k in loopData) {
if (loopData.hasOwnProperty(k)) {
var tmpObj = {};
if (key) tmpObj[key] = k;
tmpObj[elem] = loopData[k];
subStr += process(block[1], tmpObj);
}
}
return subStr;
}
return '';
}
else {
block = token.buffer.match(/\{%\s*else\s*%\}([\s\S]*?)\{%/i);
return block ? process(block[1], loopData) : '';
}
case 'set':
var t = token.expr,
v = t.sval ? evl(data, t.sval) : process(token.buffer.replace(/\{%.*?%\}/g , ''), data);
data[t.svar] = v;
return '';
}
}
});
},
/**
* Replace just markers between {{ and }}.
*/
proccessMarkers = function(str) {
var i = 0;
str = trim(str);
while ((i = str.indexOf('{{', i)) != -1) {
var id = uid++,
end = str.indexOf('}}', i),
buffer = trim(str.slice(i+2, end)).split('|'),
repl = '{_' + id + '}';
tokens[id] = {
buffer: buffer.shift(),
modif: buffer
};
str = replaceWith(str, repl, i, end+2);
i = i + repl.length;
}
return str;
},
/**
* Replace control blocks, loops, conditions.
*/
proccessControls = function(str, i, lookingFor, exprDescr, inline) {
var from = i;
while ((i = str.indexOf('{%', i)) != -1) {
var id = uid++,
end = str.indexOf('%}', i),
expr = str.slice(i+2, end),
repl = '{_' + id + '}';
if (inline || (lookingFor && expr.match(lookingFor))) {
var start = from - 2;
end = i + expr.length + 4;
tokens[id] = {
buffer: trim(str.slice(start, end)),
expr: exprDescr
};
return replaceWith(str, repl, start, end);
}
else {
var m;
// For loop
if (m = expr.match(/\s*for\s+((?:\w+\s*,)?\s*\w+)\s+in\s+(.+?)\s*$/i)) {
str = proccessControls(str, i+2, /\s*endfor\s*/i, {
type: 'for',
elem: m[1],
list: m[2]
});
}
// If statement
else if (m = expr.match(/\s*if\s+(.+)\s*/i)) {
str = proccessControls(str, i+2, /\s*endif\s*/i, {
type: 'if',
cond: trim(m[1])
});
}
// Set expression
else if (m = expr.match(/\s*set\s+(\w+)(?:\s*=\s*(.*)?)?\s*/i)) {
var dat = {
type: 'set',
svar: m[1],
sval: m[2]
};
str = m[2]
? proccessControls(str, i, null, dat, true)
: proccessControls(str, i+2, /\s*endset\s*/i, dat);
}
}
i = i + repl.length;
}
return str;
},
/**
* Need to flush closure vars before next parsing.
*/
reset = function() {
uid = 1;
tokens = {};
},
/**
* Resolve variables from the data scope.
*/
evl = function(data, buffer) {
var parts = ~buffer.indexOf('.') ? buffer.split('.') : [buffer],
i, l = parts.length,
ret = data;
for (i = 0; i < l; i++) {
ret = ret[parts[i]];
if (!ret) return '';
}
return typeof ret == 'function' ? ret.call(data) : ret;
},
/**
* Check if array or object is empty.
* @param {Array|Object}
*/
hasElements = function(obj) {
if (obj.hasOwnProperty('length')) return !!obj.length;
for (var k in obj) {
if (obj.hasOwnProperty(k)) return true;
}
return false;
},
/**
* Trim whitespaces.
*/
trim = function(s) {
return s.replace(/^\s*|\s*$/g, '');
},
/**
* Replace specified part of string.
*/
replaceWith = function(str, replace, start, end) {
return str.substr(0, start) + replace + str.substr(end);
},
/**
* Turn on debug messages about undefined vars and problems of parsing.
*/
__debug = false,
/**
* Modifiers object.
* @type {Object}
*/
__modifiers = {},
/**
* Run analysing and parsing.
* @param {String} tplStr Template string.
* @param {Object} tplData Data passed to the template.
*/
__parse = function(tplStr, tplData) {
return reset(), process(proccessControls(proccessMarkers(tplStr), 0), tplData || {});
},
/**
* Add new modifiers.
*/
__addModifiers = function(obj) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) __modifiers[i] = obj[i];
}
};
/**
* Export to global.
*/
window.Ashe = {
debug: __debug,
parse: __parse,
modifiers: __modifiers,
addModifiers: __addModifiers
};
})();