fix existing tags

new tags: for,cycle
This commit is contained in:
harttle
2016-06-17 08:39:23 +08:00
parent 8b9e9f759a
commit 0b7da2abf7
24 changed files with 432 additions and 195 deletions
+17 -31
View File
@@ -1,66 +1,52 @@
const lexical = require('./lexical.js');
const syntax = require('./syntax.js');
const error = require('./error.js');
const Exp = require('./expression.js');
function stringify(val) {
if (typeof val === 'string') return val;
return JSON.stringify(val);
}
function isTruthy(val) {
if (val instanceof Array) return !!val.length;
return !!val;
}
function factory(Filter, Tag) {
function renderTemplates(templates, scope) {
var html = '';
var template;
while (template = templates.shift()) {
templates.some(template => {
if (scope.get('forloop.skip')) return true;
if (template.type === 'tag') {
html += template.render(scope);
if (template.name === 'continue') {
scope.set('forloop.skip', true);
return true;
}
if (template.name === 'break') {
scope.set('forloop.stop', true);
scope.set('forloop.skip', true);
return true;
}
html += template.render(scope, this.register);
} else if (template.type === 'html') {
html += template.value;
} else if (template.type === 'output') {
var val = evalFilter(template.value, scope);
html += stringify(val);
}
}
});
return html;
}
function evalExp(exp, scope) {
if (!scope) error('unable to evalExp: scope undefined');
var operatorREs = lexical.operators;
for (var i = 0; i < operatorREs.length; i++) {
var operatorRE = operatorREs[i];
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
var match = exp.match(expRE);
if (match) {
var l = evalExp(match[1], scope);
var op = syntax.operators[match[2].trim()];
var r = evalExp(match[3], scope);
return op(l, r);
}
}
return evalFilter(exp, scope);
}
function evalFilter(str, scope) {
if (!scope) error('unable to evalFilter: scope undefined');
if (!scope) throw new Error('unable to evalFilter: scope undefined');
var filters = str.split('|');
var val = scope.get(filters.shift());
var val = Exp.evalValue(filters.shift(), scope);
return filters
.map(str => Filter.construct(str))
.reduce((v, filter) => filter.render(v, scope), val);
}
return {
renderTemplates, evalFilter, evalExp
renderTemplates, evalFilter
};
};
factory.isTruthy = isTruthy;
factory.stringify = stringify;
module.exports = factory;