Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron J Roe committed Jan 27, 2016
1 parent 714f75c commit e1c3517
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 20 deletions.
126 changes: 114 additions & 12 deletions lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,123 @@ exports.liquid = fromStringRenderer('liquid');
* `includeDir` will also become a local.
*/

function _renderTinyliquid(engine, str, options, fn) {
var context = engine.newContext();
var k;

/**
* Note that there's a bug in the library that doesn't allow us to pass
* the locals to newContext(), hence looping through the keys:
*/

if (options.locals){
for (k in options.locals){
context.setLocals(k, options.locals[k]);
}
delete options.locals;
}

if (options.meta){
context.setLocals('page', options.meta);
delete options.meta;
}

/**
* Add any defined filters:
*/

if (options.filters){
for (k in options.filters){
context.setFilter(k, options.filters[k]);
}
delete options.filters;
}

/**
* Set up a callback for the include directory:
*/

var includeDir = options.includeDir || process.cwd();

context.onInclude(function (name, callback) {
var extname = path.extname(name) ? '' : '.liquid';
var filename = path.resolve(includeDir, name + extname);

fs.readFile(filename, {encoding: 'utf8'}, function (err, data){
if (err) return callback(err);
callback(null, engine.parse(data));
});
});
delete options.includeDir;

/**
* The custom tag functions need to have their results pushed back
* through the parser, so set up a shim before calling the provided
* callback:
*/

var compileOptions = {
customTags: {}
};

if (options.customTags){
var tagFunctions = options.customTags;

for (k in options.customTags){
/*Tell jshint there's no problem with having this function in the loop */
/*jshint -W083 */
compileOptions.customTags[k] = function (context, name, body){
var tpl = tagFunctions[name](body.trim());
context.astStack.push(engine.parse(tpl));
};
/*jshint +W083 */
}
delete options.customTags;
}

/**
* Now anything left in `options` becomes a local:
*/

for (k in options){
context.setLocals(k, options[k]);
}

/**
* Finally, execute the template:
*/

var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
tmpl(context, fn);
}

exports.liquid.render = function(str, options, fn){
return promisify(fn, function (fn) {
var Liquid = requires.liquid || (requires.liquid = require('liquid-node'));
var engine = new Liquid.Engine;
var engine = requires.liquid;
var Liquid;

try {
// set up tinyliquid engine
engine = requires.liquid = require('tinyliquid');

// use tinyliquid engine
_renderTinyliquid(engine, str, options, fn);

return;

} catch (err) {

// set up liquid-node engine
try {
Liquid = requires.liquid = require('liquid-node');
engine = new Liquid.Engine;
} catch (err) {
throw err;
}

}

// use liquid-node engine
try {
var locals = options.locals || {};

Expand Down Expand Up @@ -232,13 +344,6 @@ exports.liquid.render = function(str, options, fn){

for (k in options.customTags){
engine.registerTag(k, tagFunctions[k]);
/*Tell jshint there's no problem with having this function in the loop */
/*jshint -W083 */
// compileOptions.customTags[k] = function (context, name, body){
// var tpl = tagFunctions[name](body.trim());
// context.astStack.push(engine.parse(tpl));
// };
/*jshint +W083 */
}
delete options.customTags;
}
Expand All @@ -265,9 +370,6 @@ exports.liquid.render = function(str, options, fn){
}
});

// var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
// tmpl(context, fn);

} catch (err) {
fn(err);
}
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"jazz": "^0.0.18",
"jqtpl": "^1.1.0",
"just": "^0.1.8",
"liquid-node": "^2.6.1",
"liquor": "^0.0.5",
"lodash": "^4.0.0",
"mocha": "^2.3.4",
Expand All @@ -41,9 +42,9 @@
"slm": "^0.5.0",
"swig": "^1.4.1",
"templayed": ">=0.2.3",
"twig": "^0.8.2",
"tinyliquid": "^0.2.22",
"tinyliquid": "^0.2.30",
"toffee": "^0.1.12",
"twig": "^0.8.2",
"underscore": "^1.3.3",
"vash": "^0.8.7",
"walrus": "^0.10.1",
Expand All @@ -59,8 +60,6 @@
"test": "mocha"
},
"dependencies": {
"bluebird": "^3.1.1",
"liquid-node": "^2.6.1",
"slm": "^0.5.0"
"bluebird": "^3.1.1"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures/liquid/include.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% include user %}
{% include sub/file %}
{% include "footer.liquid" %}
{% include "footer.html" %}
2 changes: 1 addition & 1 deletion test/shared/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.test = function(name) {
var str = fs.readFileSync('test/fixtures/' + name + '/filters.' + name).toString();

var locals = { user: user, filters: { toupper: function(object) {
return String(object).toUpperCase();
return object.toUpperCase();
}}};

cons[name].render(str, locals, function(err, html){
Expand Down
2 changes: 1 addition & 1 deletion test/shared/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.test = function(name) {
if (err) return done(err);
try{
if (name === 'liquid') {
html.should.eql('<p>Tobi</p>\n<section></section>\n<footer></footer>');
html.should.eql('<p>Tobi</p><section></section><footer></footer>');
} else {
html.should.eql('<p>Tobi</p>');
}
Expand Down

0 comments on commit e1c3517

Please sign in to comment.