templatizer
Advanced tools
Comparing version 0.2.8 to 0.2.9
@@ -14,5 +14,11 @@ (function () { | ||
// 404.jade compiled template | ||
exports["404"] = function tmpl_404(locals) { | ||
exports["404"] = function tmpl_404() { | ||
return '<div class="page-404">404!</div>'; | ||
}; | ||
// 404withVars.jade compiled template | ||
exports["404withVars"] = function tmpl_404withVars(locals) { | ||
var buf = []; | ||
buf.push('<div class="page-404">404!</div>'); | ||
var locals_ = locals || {}, content = locals_.content; | ||
buf.push('<div class="page-404">' + jade.escape((jade.interp = content || '404') == null ? '' : jade.interp) + '!</div>'); | ||
return buf.join(''); | ||
@@ -41,7 +47,7 @@ }; | ||
var locals_ = locals || {}, users = locals_.users; | ||
buf.push("<ul>"); | ||
buf.push('<ul>'); | ||
var i = 0; | ||
(function() { | ||
(function () { | ||
var $$obj = users; | ||
if ("number" == typeof $$obj.length) { | ||
if ('number' == typeof $$obj.length) { | ||
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) { | ||
@@ -61,5 +67,5 @@ var user = $$obj[$index]; | ||
} | ||
}).call(this); | ||
buf.push("</ul>"); | ||
return buf.join(""); | ||
}.call(this)); | ||
buf.push('</ul>'); | ||
return buf.join(''); | ||
}; | ||
@@ -117,7 +123,7 @@ | ||
var locals_ = locals || {}, users = locals_.users; | ||
buf.push("<ul>"); | ||
buf.push('<ul>'); | ||
var i = 0; | ||
(function() { | ||
(function () { | ||
var $$obj = users; | ||
if ("number" == typeof $$obj.length) { | ||
if ('number' == typeof $$obj.length) { | ||
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) { | ||
@@ -137,5 +143,5 @@ var user = $$obj[$index]; | ||
} | ||
}).call(this); | ||
buf.push("</ul>"); | ||
return buf.join(""); | ||
}.call(this)); | ||
buf.push('</ul>'); | ||
return buf.join(''); | ||
}; | ||
@@ -142,0 +148,0 @@ |
@@ -95,7 +95,9 @@ var esprima = require('esprima'); | ||
// Add namespaced callee as the argument | ||
statement.expression.arguments = [{ | ||
type: 'CallExpression', | ||
callee: JSON.parse(newCallee), | ||
arguments: oldArgs | ||
}]; | ||
statement.expression.arguments = [ | ||
{ | ||
type: 'CallExpression', | ||
callee: JSON.parse(newCallee), | ||
arguments: oldArgs | ||
} | ||
]; | ||
} | ||
@@ -112,2 +114,50 @@ | ||
module.exports.simplifyTemplate = function (func) { | ||
var ast = esprima.parse(func); | ||
var funcRoot = ast.body[0].body.body; | ||
if (funcRoot.length === 3) { | ||
//determine if there are only the buf declaration, the push of one string and then the return of the buf.join | ||
var simple = false; | ||
var simpleString = ''; | ||
try { | ||
/* check for buf declare */ | ||
if (funcRoot[0].type === "VariableDeclaration" && funcRoot[0].declarations[0].id.name === "buf" && | ||
(funcRoot[0].declarations[0].init.elements instanceof Array && funcRoot[0].declarations[0].init.elements.length === 0)) { | ||
/* check for single string push */ | ||
if (funcRoot[1].type === "ExpressionStatement" && funcRoot[1].expression.callee.object.name === "buf" && | ||
funcRoot[1].expression.arguments.length === 1 && funcRoot[1].expression.arguments[0].type === "Literal") { | ||
/* save the simple string */ | ||
simpleString = funcRoot[1].expression.arguments[0].value; | ||
/* check for buf join */ | ||
if (funcRoot[2].type === "ReturnStatement" && funcRoot[2].argument.callee.object.name === "buf" && | ||
funcRoot[2].argument.callee.property.name === "join" && funcRoot[2].argument.arguments.length === 1 && funcRoot[2].argument.arguments[0].value === '') { | ||
simple = true; | ||
} | ||
} | ||
} | ||
} | ||
catch (e) { | ||
simple = false; | ||
} | ||
if (simple) { | ||
//replace the funcRoot with a simple return; | ||
var simpleRoot = [{ | ||
type: 'ReturnStatement', | ||
argument: { | ||
type: 'Literal', | ||
value: simpleString | ||
} | ||
}]; | ||
ast.body[0].body.body = simpleRoot; | ||
//remove function parameter | ||
ast.body[0].params = []; | ||
} | ||
} | ||
return escodegen.generate(ast); | ||
} | ||
module.exports.getMixins = function (options) { | ||
@@ -114,0 +164,0 @@ var ast = esprima.parse(options.template); |
{ | ||
"name": "templatizer", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -32,3 +32,4 @@ "contributors": [ | ||
"benchmark": "node benchmark/speedtest.js", | ||
"builddemo": "node benchmark/build-demo.js" | ||
"builddemo": "node benchmark/build-demo.js", | ||
"test": "npm run builddemo && open ./test/index.html" | ||
}, | ||
@@ -35,0 +36,0 @@ "keywords": [ |
@@ -91,2 +91,6 @@ # templatizer.js | ||
## Changelog | ||
- v0.2.9 [diff](https://github.com/henrikjoreteg/templatizer/compare/v0.2.8...v0.2.9) - Adding path normalize to avoid issues if passing in paths like `/thing/../otherfolder`. | ||
## License | ||
@@ -101,2 +105,2 @@ | ||
If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg) | ||
If you think this is cool, you should follow me on twitter: [@HenrikJoreteg](http://twitter.com/henrikjoreteg) |
@@ -37,8 +37,11 @@ var jade = require('jade'); | ||
templateDirectories = _.map(templateDirectories, function (templateDirectory) { | ||
return templateDirectory.replace(pathSepRegExp, pathSep); | ||
}); | ||
templateDirectories.forEach(function (templateDirectory) { | ||
var contents = walkdir.sync(templateDirectory); | ||
templateDirectory = templateDirectory.replace(pathSepRegExp, pathSep); | ||
contents.forEach(function (file) { | ||
var item = file.replace(templateDirectory, '').slice(1); | ||
var item = file.replace(path.normalize(templateDirectory), '').slice(1); | ||
if (path.extname(item) === '' && path.basename(item).charAt(0) !== '.') { | ||
@@ -51,3 +54,3 @@ if (folders.indexOf(item) === -1) folders.push(item); | ||
} | ||
_readTemplates.push(item); | ||
@@ -88,2 +91,3 @@ templates.push(templateDirectory + pathSep + item); | ||
}).toString()); | ||
template = jadeAst.renameFunc(template, dirString); | ||
@@ -101,2 +105,4 @@ | ||
template = jadeAst.simplifyTemplate(template); | ||
output += [ | ||
@@ -103,0 +109,0 @@ '', |
@@ -14,5 +14,11 @@ (function () { | ||
// 404.jade compiled template | ||
exports["404"] = function tmpl_404(locals) { | ||
exports["404"] = function tmpl_404() { | ||
return '<div class="page-404">404!</div>'; | ||
}; | ||
// 404withVars.jade compiled template | ||
exports["404withVars"] = function tmpl_404withVars(locals) { | ||
var buf = []; | ||
buf.push('<div class="page-404">404!</div>'); | ||
var locals_ = locals || {}, content = locals_.content; | ||
buf.push('<div class="page-404">' + jade.escape((jade.interp = content || '404') == null ? '' : jade.interp) + '!</div>'); | ||
return buf.join(''); | ||
@@ -41,7 +47,7 @@ }; | ||
var locals_ = locals || {}, users = locals_.users; | ||
buf.push("<ul>"); | ||
buf.push('<ul>'); | ||
var i = 0; | ||
(function() { | ||
(function () { | ||
var $$obj = users; | ||
if ("number" == typeof $$obj.length) { | ||
if ('number' == typeof $$obj.length) { | ||
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) { | ||
@@ -61,5 +67,5 @@ var user = $$obj[$index]; | ||
} | ||
}).call(this); | ||
buf.push("</ul>"); | ||
return buf.join(""); | ||
}.call(this)); | ||
buf.push('</ul>'); | ||
return buf.join(''); | ||
}; | ||
@@ -117,7 +123,7 @@ | ||
var locals_ = locals || {}, users = locals_.users; | ||
buf.push("<ul>"); | ||
buf.push('<ul>'); | ||
var i = 0; | ||
(function() { | ||
(function () { | ||
var $$obj = users; | ||
if ("number" == typeof $$obj.length) { | ||
if ('number' == typeof $$obj.length) { | ||
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) { | ||
@@ -137,5 +143,5 @@ var user = $$obj[$index]; | ||
} | ||
}).call(this); | ||
buf.push("</ul>"); | ||
return buf.join(""); | ||
}.call(this)); | ||
buf.push('</ul>'); | ||
return buf.join(''); | ||
}; | ||
@@ -197,13 +203,9 @@ | ||
// otherfolder/othertweet2.jade compiled template | ||
exports["otherfolder"]["othertweet2"] = function tmpl_otherfolder_othertweet2(locals) { | ||
var buf = []; | ||
buf.push('<p>test</p>'); | ||
return buf.join(''); | ||
exports["otherfolder"]["othertweet2"] = function tmpl_otherfolder_othertweet2() { | ||
return '<p>test</p>'; | ||
}; | ||
// test.jade compiled template | ||
exports["test"] = function tmpl_test(locals) { | ||
var buf = []; | ||
buf.push('<p>test</p>'); | ||
return buf.join(''); | ||
exports["test"] = function tmpl_test() { | ||
return '<p>test</p>'; | ||
}; | ||
@@ -210,0 +212,0 @@ |
@@ -14,5 +14,11 @@ (function () { | ||
// 404.jade compiled template | ||
exports["404"] = function tmpl_404(locals) { | ||
exports["404"] = function tmpl_404() { | ||
return '<div class="page-404">404!</div>'; | ||
}; | ||
// 404withVars.jade compiled template | ||
exports["404withVars"] = function tmpl_404withVars(locals) { | ||
var buf = []; | ||
buf.push('<div class="page-404">404!</div>'); | ||
var locals_ = locals || {}, content = locals_.content; | ||
buf.push('<div class="page-404">' + jade.escape((jade.interp = content || '404') == null ? '' : jade.interp) + '!</div>'); | ||
return buf.join(''); | ||
@@ -19,0 +25,0 @@ }; |
@@ -84,2 +84,10 @@ /* globals test, ok, templatizer, templatizer_unaltered, templatizer_multiple_dirs */ | ||
ok(typeof nestedMixin.user_li === 'function'); | ||
}); | ||
test("Test that simplified templates have the same content: Issue #31", function () { | ||
var regular = templatizer['404withVars'], | ||
simple = templatizer['404']; | ||
ok(regular() === simple()); | ||
ok(regular({content: 'test'}) !== simple()); | ||
}); |
137599
32
3712
105