forked from simpleviewinc/goatee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
486 lines (410 loc) · 13.7 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
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
// umd boilerplate for CommonJS and AMD
if (typeof exports === 'object' && typeof define !== 'function') {
var define = function (factory) {
factory(require, exports, module);
};
}
define(function(require, exports, module) {
(function() {
var openChar = "Ͼ";
var closeChar = "Ͽ";
var parenChar = "Ԓ";
// lexes the template to detect opening and closing tags and parens
var lexer = function(html) {
// convert open close to single chars to make algorithm easier
var temp = html.replace(/\{\{/g, openChar).replace(/\}\}/g, closeChar).split("");
var marks = [];
var inTag = false;
var inParen = false;
var inSingle = false;
var inDouble = false;
var openCount = 0;
for(var i = 0; i < temp.length; i++) {
var c = temp[i];
if (inTag) {
// inside tag
if (inParen) {
if (c === openChar) {
temp.splice(i, 1, "{{");
} else if (c === closeChar) {
temp.splice(i, 1, "}}");
} else if (inSingle && c === "'") {
inSingle = false;
} else if (inSingle) {
// inside single do nothing
} else if (inDouble && c === '"') {
inDouble = false;
} else if (inDouble) {
// inside double do nothing
} else if (c === "(") {
openCount++;
} else if (c === ")") {
openCount--;
if (openCount === 0) {
inParen = false;
temp.splice(i, 0, parenChar);
i++;
}
} else if (c === '"') {
inDouble = true;
} else if (c === "'") {
inSingle = true;
}
} else if (c === "(") {
// in tag, not in parent, open char
inParen = true;
openCount++;
temp.splice(i, 0, parenChar);
i++;
} else if (c === closeChar) {
inTag = false;
}
} else if (c === openChar) {
// not in tag, open char
inTag = true;
}
}
return temp.join("");
}
var unlex = function(html) {
return html.replace(/Ͼ/g, "{{").replace(/Ͽ/g, "}}").replace(/Ԓ/g, "");
}
var fill = function(html, data, partials, globalData, plugins) {
if (typeof partials == "undefined") {
partials = {};
}
if (typeof globalData == "undefined") {
globalData = data;
}
if (data === undefined) {
data = {};
}
var lexedHtml = lexer(html);
var context = getTemplateContext(lexedHtml);
var myPartials = {};
for(var i in partials) {
var temp = lexer(partials[i]);
myPartials[i] = {
context : getTemplateContext(temp),
html : temp
};
}
var helpers = new Helpers({ partials : myPartials, plugins : plugins || {} });
return processTags(lexedHtml, context, [ data ], myPartials, {}, globalData, helpers);
};
var getTemplateContext = function(html) {
var currentHTML = html;
var context = {
tags : [],
start : 0,
inner : html,
innerStart : 0,
innerEnd : html.length,
end : html.length
};
var myContext = context;
var previousContext = [];
while(true) {
var matches = currentHTML.match(/Ͼ([\$#!:\/\>\+%]?)(-*?)([~\*@]?)(\w+(Ԓ\([\s\S]*?Ԓ\))?(\.\w+(Ԓ\([\s\S]*?Ԓ\))?)*?)?Ͽ/);
if (matches == null) {
break;
}
if (matches[1] != "/") {
var labelArr = [];
var temp = matches[4];
while(temp !== undefined) {
var termMatch = temp.match(/(\w+)(Ԓ\([\s\S]*?Ԓ\))?(\.|$)/);
if (termMatch === null) {
break;
}
var term = {
label : termMatch[1]
};
if (termMatch[2] !== undefined) {
// extract the contents of a function call eg: foo(bar, baz)
term.argString = termMatch[2].replace(/^Ԓ\(/, "").replace(/Ԓ\)$/, "");
}
temp = temp.replace(termMatch[0], "");
labelArr.push(term);
}
myContext.tags.push({ label : matches[4], labelArr : labelArr, backTrack : matches[2].length, lookup : matches[3], command : matches[1], start : matches.index, end : matches[0].length + matches.index, innerStart : matches[0].length + matches.index, innerEnd : "", tags : [] });
if (["#", ":", "!", "+", "$"].indexOf(matches[1]) > -1) {
previousContext.push(myContext);
myContext = myContext.tags[myContext.tags.length - 1];
}
} else {
myContext.end = matches[0].length + matches.index;
myContext.innerEnd = matches.index;
myContext.inner = html.substring(myContext.innerStart, myContext.innerEnd);
myContext = previousContext[previousContext.length - 1];
previousContext.splice(previousContext.length - 1, 1);
}
var temp = [];
for(var i = 0; i < matches[0].length; i++) {
temp.push("-");
}
currentHTML = currentHTML.replace(matches[0], temp.join(""));
}
return context;
}
var processTags = function(html, context, data, partials, extraData, globalData, helpers) {
var returnArray = [];
var position = context.innerStart;
for(var i = 0; i < context.tags.length; i++) {
returnArray.push(html.substring(position, context.tags[i].start));
position = context.tags[i].end;
if (context.tags[i].command === ">") {
var keyMatch = getKeyMatch(partials, context.tags[i].label);
if (keyMatch !== undefined) {
returnArray.push(processTags(partials[keyMatch].html, partials[keyMatch].context, data, partials, {}, globalData, helpers));
}
continue;
}
if (context.tags[i].command === "$") {
returnArray.push(unlex(context.tags[i].inner));
continue;
}
var myData;
var dataContext = data;
if (context.tags[i].lookup === "*") {
myData = globalData;
} else if (context.tags[i].lookup === "@") {
myData = extraData;
} else if (context.tags[i].lookup === "~") {
myData = helpers;
} else {
dataContext = data.slice();
dataContext.splice(dataContext.length - context.tags[i].backTrack, context.tags[i].backTrack);
myData = dataContext[dataContext.length - 1];
}
var labelArr = context.tags[i].labelArr;
for(var j = 0; j < labelArr.length; j++) {
if (!(myData instanceof Object)) {
// if myData is not an Object (includes {} and []) then we can't dive deeper
// set to undefined because it means we were not able to process all labels
myData = undefined;
break;
}
var keyMatch = getKeyMatch(myData, labelArr[j].label);
if (myData[keyMatch] === undefined) {
// set to undefined because it means we were not able to process all labels
myData = undefined;
break;
}
if (typeof myData[keyMatch] === "function" && labelArr[j].argString !== undefined) {
// if it's a function and we have () then we execute it
var argArray;
if (labelArr[j].argString !== "") {
// in () argString === "", don't eval arguments in that case
argArray = evalArgs(labelArr[j].argString, dataContext[dataContext.length - 1], globalData, extraData, helpers);
if (argArray instanceof Error) {
// invalid arg array, halt processing of this tag
myData = undefined;
break;
}
}
myData = myData[keyMatch].apply(myData, argArray);
} else {
myData = myData[keyMatch];
}
}
if (context.tags[i].command === "" || context.tags[i].command === "%") {
if (typeof myData == "undefined" || myData == null) {
// do nothing
} else if (typeof myData === "string" || typeof myData === "number" || typeof myData === "boolean") {
/*** standard tags ***/
if (context.tags[i].command === "" || typeof myData !== "string") {
returnArray.push(myData)
} else {
returnArray.push(myData.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'));
}
} else if (typeof myData.template != "undefined" && typeof myData.data != "undefined") {
/*** passing a template and data structure ***/
/*** Is array loop over array ***/
if (myData.data instanceof Array) {
for(var j = 0; j < myData.data.length; j++) {
returnArray.push(fill(myData.template, myData.data[j]));
}
} else {
returnArray.push(fill(myData.template, myData.data));
}
}
} else if (context.tags[i].command === "+") {
partials[context.tags[i].label] = {
html : context.tags[i].inner,
context : getTemplateContext(context.tags[i].inner)
}
} else if (context.tags[i].command === "#") {
if (typeof myData != "undefined") {
if (myData instanceof Array) {
var tempExtraData = {
row : 0,
first : false,
last : false,
even : false,
odd : false
};
for(var j = 0; j < myData.length; j++) {
tempExtraData.row++;
tempExtraData.first = tempExtraData.row == 1 ? true : false;
tempExtraData.last = tempExtraData.row == myData.length ? true : false;
tempExtraData.odd = tempExtraData.row % 2 == 1 ? true : false;
tempExtraData.even = !tempExtraData.odd;
tempExtraData.data = myData[j];
var newData = dataContext.slice();
newData.push(myData[j]);
returnArray.push(processTags(html, context.tags[i], newData, partials, tempExtraData, globalData, helpers));
}
} else if (myData instanceof Object && !isEmpty(myData)) {
var newData = dataContext.slice();
newData.push(myData);
returnArray.push(processTags(html, context.tags[i], newData, partials, {}, globalData, helpers));
}
}
} else if (context.tags[i].command === ":") {
if (
typeof myData != "undefined" && (
(typeof myData == "string" && myData != "" && myData != "false")
||
(myData instanceof Array && myData.length > 0)
||
(myData instanceof Object && !isEmpty(myData))
||
(typeof myData == "boolean" && myData != false)
||
(typeof myData == "number")
)
) {
returnArray.push(processTags(html, context.tags[i], dataContext, partials, extraData, globalData, helpers));
}
} else if (context.tags[i].command === "!") {
if (
typeof myData == "undefined"
|| (
(typeof myData == "string" && (myData == "" || myData == "false"))
||
(myData instanceof Array && myData.length == 0)
||
(myData instanceof Object && isEmpty(myData))
||
(typeof myData == "boolean" && myData == false)
)
) {
returnArray.push(processTags(html, context.tags[i], dataContext, partials, extraData, globalData, helpers));
}
}
}
if (position < context.end) {
returnArray.push(html.substring(position, context.innerEnd));
}
return unlex(returnArray.join(""));
};
/*** Cross browser way to test if an object has no keys ***/
var isEmpty = function(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
};
var getKeyMatch = function(obj, key) {
if (obj[key] !== undefined) {
return key;
}
var lcaseKey = key.toLowerCase();
var map = {};
for(var prop in obj) {
var temp = prop.toLowerCase();
if (temp === lcaseKey) {
return prop;
}
}
// key not found
return undefined;
}
// helpers provide all fill() calls with useful function calls to assist in eliminating pre-processing
var Helpers = function(args) {
var self = this;
self.var = {};
self._partials = args.partials;
self.plugins = args.plugins;
}
Helpers.prototype.equal = function(arg1, arg2) {
return arg1 === arg2;
}
Helpers.prototype.contains = function(arg1, arg2) {
if (arg1.indexOf === undefined) {
return false;
}
return arg1.indexOf(arg2) > -1;
}
Helpers.prototype.exec = function(arg1) {
if (arg1 instanceof Function) {
// if it's a function we have to wrap in another try catch to prevent failures
try {
var temp = arg1();
} catch(e) {
warn(e);
return; // return undefined if exec fails
}
} else {
// if it's not a function simply return the result
temp = arg1;
}
return temp;
}
Helpers.prototype.partial = function(name) {
var self = this;
return unlex(self._partials[name].html);
}
Helpers.prototype.log = function() {
var self = this;
console.log.apply(console, arguments);
}
Helpers.prototype.setVar = function(arg1, arg2) {
var self = this;
self.var[arg1] = arg2;
}
var Goatee = function() {
var self = this;
self._plugins = {};
self._locked = false;
}
Goatee.prototype.addPlugin = function(name, plugin) {
var self = this;
if (self._locked) {
throw new Error("Unable to addPlugin to locked goatee instance as it could cause thread safety issues. If you need to add plugins create your own instance of goatee.Goatee()");
}
self._plugins[name] = plugin;
}
Goatee.prototype.fill = function(html, data, partials, globalData, plugins) {
var self = this;
return fill(html, data || {}, partials || {}, globalData || data, self._plugins);
};
Goatee.prototype.lock = function() {
var self = this;
self._locked = true;
}
/*** only reveal public methods ***/
exports.fill = fill;
exports._lexer = lexer;
exports._unlex = unlex;
exports.Goatee = Goatee;
})();
var warn = function(e) {
console.warn(e.message, e.stack);
}
var evalArgs = function(str, data, global, extra, helpers) {
// we var all important variables to negate closure scope preventing eval from escalating out of the sandbox
var window, process, require, setTimeout, setInterval, clearTimeout, clearInterval, __dirname, __filename, module, exports, Buffer, define;
arguments = [];
try {
var temp = eval("[" + str + "]");
} catch (e) {
warn(e);
return e;
}
return temp;
}
});