Comparing version 0.1.1 to 0.1.3
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
console.log('jsmartier version 0.1.1'); | ||
console.log('jsmartier version 0.1.3'); | ||
// modules | ||
@@ -18,3 +18,4 @@ var FS = require('fs'); | ||
suffix : '%}', | ||
monitorInterval : 4000 | ||
monitorInterval : 4000, | ||
legacy : false | ||
}; | ||
@@ -120,3 +121,3 @@ | ||
update : function(content) { | ||
this.render = parse(content, this.path, defaults.prefix, defaults.suffix, getTemplate); | ||
this.render = parse(content, this.path, defaults.prefix, defaults.suffix, defaults.legacy); | ||
// console.log(new Date() + ': ' + this.path + " updated."); | ||
@@ -123,0 +124,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"main": "jsmartier.js", | ||
"version": "0.1.1", | ||
"version": "0.1.3", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
@@ -70,3 +70,9 @@ var stringify = JSON.stringify; | ||
assign : function(vars, config, args) { | ||
vars[args['var']] = JSON.parse(args.value); | ||
var ret; | ||
try { | ||
ret = new Function("return " + args.value)(); | ||
} catch (e) { | ||
ret = args.value; | ||
} | ||
vars[args['var']] = ret; | ||
return ''; | ||
@@ -78,7 +84,29 @@ } | ||
exports.parse = function(text, path, prefix, suffix) { | ||
exports.parse = function(text, path, prefix, suffix, legacy) { | ||
ERR_PATH = path; | ||
return new Function('$', parse(text, path, prefix, suffix))(plugins); | ||
var script = parse(text, path, prefix, suffix); | ||
if (legacy) { | ||
script = script.replace(/vars(?:\.\w+){2,}/g, function(m) { | ||
var idx = m.indexOf('.', 6), buf = m.substr(0, idx); | ||
while ((idx = m.indexOf('.', idx + 1)) != -1) { | ||
buf += '&&' + m.substr(0, idx); | ||
} | ||
return '(' + buf + '||"")'; | ||
}); | ||
} | ||
console.log('function anonymous($){' + script + '}'); | ||
try { | ||
return new Function('$', script)(plugins); | ||
} catch (e) { | ||
return templateError(path, e); | ||
} | ||
}; | ||
function templateError(path, e) { | ||
return function() { | ||
return '<Template error: ' + path + '\n' + e.message + '\n>'; | ||
}; | ||
} | ||
var WRONG_MACRO = 'Wrong macro: '; | ||
@@ -106,3 +134,3 @@ | ||
idx2 = text.indexOf(suffix, start); | ||
if (suffix === -1) { | ||
if (idx2 === -1) { | ||
throw text.substr(idx, 20) + ": unterminated macro"; | ||
@@ -114,3 +142,3 @@ } | ||
break; | ||
content = content.substr(0, idx3) + transform(content.substring(idx3 + prefix.length, idx2), stack); | ||
content = content.substr(0, idx3) + '"' + transform(content.substring(idx3 + prefix.length, idx2), stack) + '+"'; | ||
start = idx2 + suffix.length; | ||
@@ -128,4 +156,3 @@ } | ||
buf += ';};'; | ||
buf = buf.replace(/"\+"/g, '').replace(/\+""\+/g, '+'); | ||
// console.log('function anonymous($){' + buf + '}'); | ||
buf = buf.replace(/"\+"|/g, '').replace(/\+""\+/g, '+').replace(/ ""\+/g, ' '); | ||
return buf; | ||
@@ -178,4 +205,3 @@ | ||
stack.push('foreach'); | ||
return '+$.forEach(vars,config,{' + args.join().replace(/"from":"\$(.*?)"/, '"from":vars.$1') | ||
+ '},function(vars){return ""'; | ||
return '+$.forEach(vars,config,{' + args.join() + '},function(vars){return ""'; | ||
case "/foreach": | ||
@@ -196,2 +222,3 @@ if (stack.pop() !== 'foreach') { | ||
function getArgs(content) { | ||
// console.log('getArgs', content); | ||
return content.split(/\s+/).map(function(str) { | ||
@@ -202,8 +229,10 @@ if (!str) | ||
var val = str.substr(idx + 1); | ||
try { | ||
new Function('return ' + val)(); | ||
} catch (e) { | ||
if (/^\$\w+/.test(val)) { | ||
val = 'vars.' + val.substr(1).replace(/#(\w+)#/g, 'config.$1').replace(/->/g, '.'); | ||
} else if (/^#\w+#$/.test(val)) { | ||
val = 'config.' + val.substr(1, val.length - 2); | ||
} else if (!/^".*"$|^'.*'$/.test(val)) { | ||
val = stringify(val); | ||
} | ||
// console.log(str.substr(0, idx), val); | ||
return '"' + str.substr(0, idx) + '":' + val; | ||
@@ -210,0 +239,0 @@ }); |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
10695
4
398
1
1