velocityjs
Advanced tools
Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
@@ -37,3 +37,3 @@ ##Velocity - Template Engine [![Build Status](https://secure.travis-ci.org/shepherdwind/velocity.js.png)](https://travis-ci.org/shepherdwind/velocity.js) | ||
```bash | ||
$ cake --build | ||
$ make parse | ||
``` | ||
@@ -151,3 +151,3 @@ | ||
``` | ||
#foreach($num as [1..5]) | ||
#foreach($num in [1..5]) | ||
index => $foreach.index | ||
@@ -154,0 +154,0 @@ count => $foreach.count |
module.exports = function(Velocity, utils, BLOCK_TYPES){ | ||
function getUnique(arrs){ | ||
var objs = {}; | ||
utils.forEach(arrs, function(arr){ | ||
objs[arr] = 1; | ||
}); | ||
return utils.keys(objs); | ||
} | ||
utils.mixin(Velocity.prototype, { | ||
@@ -86,3 +93,4 @@ getBlock: function(block) { | ||
ast : ast, | ||
context: {} | ||
context: {}, | ||
objectKeys: [] | ||
}; | ||
@@ -96,2 +104,7 @@ | ||
this._render(asts); | ||
if (local.objectKeys.length) { | ||
var vmText = this.getRefText(local.real); | ||
var vm = this._callMacro('objects', vmText, getUnique(local.objectKeys)); | ||
this.setRef(local.real, vm); | ||
} | ||
@@ -98,0 +111,0 @@ this.conditions.pop(); |
@@ -0,10 +1,17 @@ | ||
var Parser = require('../../parse/index'); | ||
var Compile = require('../../compile/index'); | ||
var fs = require('fs'); | ||
var macros = Parser.parse(fs.readFileSync(__dirname + '/macros.vm').toString()); | ||
function getVmText(macro, vmText, third){ | ||
var vm = new Compile(macros); | ||
return vm.render({ | ||
macro: macro, | ||
vmText: vmText, | ||
third: third | ||
}); | ||
} | ||
module.exports = function(Velocity, utils, BLOCK_TYPES){ | ||
// 基本数据结构 | ||
var TYPE_STRING = 'variable'; | ||
var TYPE_ARRAY = 'foreach:normal'; | ||
var TYPE_MAP_ALL = 'foreach:entrySet'; | ||
var TYPE_MAP_KEYS = 'foreach:keySet'; | ||
var TYPE_METHOD = 'method'; | ||
function getPath(ast){ | ||
@@ -43,2 +50,11 @@ | ||
if (astType.foreach === true && astType.type === 'method') { | ||
var isMaps = true; | ||
} | ||
this.setRef(ast, this.getLeaf(astType), isMaps); | ||
}, | ||
setRef: function(ast, text, isMaps){ | ||
var paths = getPath(ast); | ||
@@ -50,5 +66,4 @@ var last = paths.pop(); | ||
if (astType.foreach === true && astType.type === 'method') { | ||
if (last === 'entrySet' || last === 'keySet') last = paths.pop(); | ||
} | ||
if (isMaps && (last === 'entrySet' || last === 'keySet')) | ||
last = paths.pop(); | ||
@@ -60,3 +75,3 @@ utils.forEach(paths, function(path){ | ||
leafs.push(astType); | ||
leafs.push(text); | ||
context[last] = '{@' + len + '@}'; | ||
@@ -71,4 +86,3 @@ }, | ||
var tpl = '"{@' + i + '@}"'; | ||
var str = this.getLeaf(leaf); | ||
context = context.replace(tpl, str); | ||
context = context.replace(tpl, leaf); | ||
}, this); | ||
@@ -84,3 +98,7 @@ | ||
if (!leaf.foreach) { | ||
ret = this._callMacro('jsonifyGetString', this.getRefText(real)); | ||
if (leaf.type === 'method') { | ||
ret = this.getMethodCall(leaf); | ||
} else { | ||
ret = this._callMacro('string', this.getRefText(real)); | ||
} | ||
} else { | ||
@@ -90,4 +108,3 @@ if (leaf.foreach === true && real.from) { | ||
} else { | ||
ret = '"Function(){}"'; | ||
//ret = this.getMethodCall(leaf); | ||
ret = this.getMethodInEachCall(leaf); | ||
} | ||
@@ -99,42 +116,29 @@ } | ||
getMethodCall: function(leaf){ | ||
var args, returnVal; | ||
var ret = { | ||
__isMethod: true | ||
}; | ||
returnVal = this._callMacro('jsonifyGetString', this.getRefText(leaf.real)); | ||
if (leaf.foreach) { | ||
var real = leaf.real; | ||
var len = real.path && real.path.length; | ||
var last = len ? real.path[len - 1] : real; | ||
args = utils.map(last.args, function(ast){ | ||
return this._callMacro('jsonifyGetString', this.getRefText(ast)); | ||
}, this); | ||
returnVal = this._creatEach(leaf.foreach, args, returnVal); | ||
} | ||
ret.args = args; | ||
ret.returnVal = returnVal; | ||
return JSON.stringify(ret, false, 2); | ||
getMethodInEachCall: function(leaf){ | ||
return '"function(){}"'; | ||
}, | ||
_creatEach: function(foreach, args, value){ | ||
var tpl = '#foreach(${list} in {$lists}) {' + | ||
' "{$args}" : "{$value}" #if($foreach.hasNext) , #end '+ | ||
'} #end'; | ||
var getText = this.getRefText; | ||
tpl = tpl.replace('{$lists}', getText(foreach.from)); | ||
tpl = tpl.replace('{list}', foreach.to); | ||
tpl = tpl.replace('{$args}', args); | ||
tpl = tpl.replace('{$value}', value); | ||
return tpl; | ||
getMethodCall: function(leaf){ | ||
return '"function(){}"'; | ||
/* | ||
* var ast = leaf.real; | ||
* var paths = getPath(ast); | ||
* var len = ast.path.length; | ||
* var last = ast.path[len - 1]; | ||
* var args = last.args; | ||
* | ||
* var argText = []; | ||
* utils.forEach(args, function(arg){ | ||
* argText.push('"' + this.getRefText(arg) + '"'); | ||
* }, this); | ||
* argText = argText.join(', '); | ||
* | ||
* var retText = this.getRefText(ast); | ||
* | ||
* //console.log(getVmText('method', argText, retText)); | ||
* return getVmText('method', argText, retText); | ||
*/ | ||
}, | ||
_callMacro: function(macro, args){ | ||
args = utils.isArray(args) ? args.join(' ') : args; | ||
return '#' + macro + '(' + args + ')'; | ||
}, | ||
_callMacro: getVmText, | ||
@@ -145,11 +149,14 @@ getEachVTL: function(leaf){ | ||
var last = paths.pop(); | ||
var macro = 'jsonifyGetList'; | ||
var macro = 'lists'; | ||
var vmText = this.getRefText(real.from); | ||
if (leaf.type === 'method' && last === 'entrySet') { | ||
macro = 'jsonifyGetEntryMap'; | ||
macro = 'maps'; | ||
vmText = vmText.replace(/\.entrySet\(\)$/, ''); | ||
} else if (leaf.type === 'method' && last === 'keySet') { | ||
macro = 'jsonifyGetKeyMap'; | ||
macro = 'maps'; | ||
vmText = vmText.replace(/\.keySet\(\)$/, ''); | ||
} | ||
return this._callMacro(macro, this.getRefText(real.from)); | ||
return this._callMacro(macro, vmText); | ||
} | ||
@@ -156,0 +163,0 @@ |
@@ -23,4 +23,10 @@ module.exports = function(Velocity, utils){ | ||
if (ast.id == local.ast.to) { | ||
ret.real = local.ast; | ||
ret.foreach = true; | ||
if (ast.path) { | ||
local.objectKeys.push(ast.path[0].id); | ||
ret.ignore = true; | ||
} else { | ||
ret.real = local.ast; | ||
ret.foreach = true; | ||
} | ||
} | ||
@@ -27,0 +33,0 @@ |
@@ -63,21 +63,45 @@ module.exports = function(Helper, utils){ | ||
utils.forEach(ref.args, function(arg){ | ||
args.push(getLiteral(arg)); | ||
}); | ||
if (arg.type === 'string') { | ||
ret += ref.id + '(' + args.join(',') + ')'; | ||
var sign = arg.isEval? '"': "'"; | ||
var text = sign + arg.value + sign; | ||
args.push(text); | ||
return ret; | ||
} else { | ||
} | ||
args.push(getRefText(arg)); | ||
function getLiteral(ast){ | ||
var ret = ''; | ||
switch(ast.type) { | ||
case 'string': { | ||
var sign = ast.isEval? '"': "'"; | ||
ret = sign + ast.value + sign; | ||
break; | ||
} | ||
}); | ||
case 'integer': | ||
case 'bool' : { | ||
ret = ast.value; | ||
break; | ||
} | ||
ret += ref.id + '(' + args.join(',') + ')'; | ||
case 'array': { | ||
ret = '['; | ||
var len = ast.value.length - 1; | ||
utils.forEach(ast.value, function(arg, i){ | ||
ret += getLiteral(arg); | ||
if (i !== len) ret += ', '; | ||
}); | ||
ret += ']'; | ||
break; | ||
} | ||
default: | ||
ret = getRefText(ast) | ||
} | ||
return ret; | ||
} | ||
@@ -84,0 +108,0 @@ |
@@ -11,9 +11,11 @@ /** | ||
var jsonify = new Jsonify(Parser.parse(vm)); | ||
return jsonify.context; | ||
return jsonify.toVTL(); | ||
} | ||
function getJson(obj){ | ||
return JSON.stringify(obj, false, 2); | ||
} | ||
it('simple references', function(){ | ||
var vm = '$foo.bar'; | ||
var context = getContext(vm).strings; | ||
assert.deepEqual(context, { foo: {bar: '$foo.bar'} }); | ||
assert.equal(getContext(vm), getJson({foo: {bar: '#jsonifyGetString($foo.bar)'} })); | ||
}); | ||
@@ -23,5 +25,3 @@ | ||
var vm = '$foo.bar($user.name)'; | ||
var context = getContext(vm).methods; | ||
assert.equal(context.foo.bar[0][0], "$user.name"); | ||
assert.equal(context.foo.bar[0][1], "$foo.bar($user.name)"); | ||
assert.equal(getContext(vm), getJson({foo: {bar: 'Function(){}'} })); | ||
}); | ||
@@ -31,6 +31,5 @@ | ||
var vm = '$foo.bar($user.name).bar'; | ||
var context = getContext(vm).methods; | ||
assert.equal(context.foo.bar, undefined); | ||
//console.log(getContext(vm)); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
402005
3349
5