Skip to content

Commit e1c3517

Browse files
committed
fix tests
1 parent 714f75c commit e1c3517

File tree

6 files changed

+121
-20
lines changed

6 files changed

+121
-20
lines changed

lib/consolidate.js

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,123 @@ exports.liquid = fromStringRenderer('liquid');
187187
* `includeDir` will also become a local.
188188
*/
189189

190+
function _renderTinyliquid(engine, str, options, fn) {
191+
var context = engine.newContext();
192+
var k;
193+
194+
/**
195+
* Note that there's a bug in the library that doesn't allow us to pass
196+
* the locals to newContext(), hence looping through the keys:
197+
*/
198+
199+
if (options.locals){
200+
for (k in options.locals){
201+
context.setLocals(k, options.locals[k]);
202+
}
203+
delete options.locals;
204+
}
205+
206+
if (options.meta){
207+
context.setLocals('page', options.meta);
208+
delete options.meta;
209+
}
210+
211+
/**
212+
* Add any defined filters:
213+
*/
214+
215+
if (options.filters){
216+
for (k in options.filters){
217+
context.setFilter(k, options.filters[k]);
218+
}
219+
delete options.filters;
220+
}
221+
222+
/**
223+
* Set up a callback for the include directory:
224+
*/
225+
226+
var includeDir = options.includeDir || process.cwd();
227+
228+
context.onInclude(function (name, callback) {
229+
var extname = path.extname(name) ? '' : '.liquid';
230+
var filename = path.resolve(includeDir, name + extname);
231+
232+
fs.readFile(filename, {encoding: 'utf8'}, function (err, data){
233+
if (err) return callback(err);
234+
callback(null, engine.parse(data));
235+
});
236+
});
237+
delete options.includeDir;
238+
239+
/**
240+
* The custom tag functions need to have their results pushed back
241+
* through the parser, so set up a shim before calling the provided
242+
* callback:
243+
*/
244+
245+
var compileOptions = {
246+
customTags: {}
247+
};
248+
249+
if (options.customTags){
250+
var tagFunctions = options.customTags;
251+
252+
for (k in options.customTags){
253+
/*Tell jshint there's no problem with having this function in the loop */
254+
/*jshint -W083 */
255+
compileOptions.customTags[k] = function (context, name, body){
256+
var tpl = tagFunctions[name](body.trim());
257+
context.astStack.push(engine.parse(tpl));
258+
};
259+
/*jshint +W083 */
260+
}
261+
delete options.customTags;
262+
}
263+
264+
/**
265+
* Now anything left in `options` becomes a local:
266+
*/
267+
268+
for (k in options){
269+
context.setLocals(k, options[k]);
270+
}
271+
272+
/**
273+
* Finally, execute the template:
274+
*/
275+
276+
var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
277+
tmpl(context, fn);
278+
}
279+
190280
exports.liquid.render = function(str, options, fn){
191281
return promisify(fn, function (fn) {
192-
var Liquid = requires.liquid || (requires.liquid = require('liquid-node'));
193-
var engine = new Liquid.Engine;
282+
var engine = requires.liquid;
283+
var Liquid;
284+
285+
try {
286+
// set up tinyliquid engine
287+
engine = requires.liquid = require('tinyliquid');
288+
289+
// use tinyliquid engine
290+
_renderTinyliquid(engine, str, options, fn);
291+
292+
return;
293+
294+
} catch (err) {
295+
296+
// set up liquid-node engine
297+
try {
298+
Liquid = requires.liquid = require('liquid-node');
299+
engine = new Liquid.Engine;
300+
} catch (err) {
301+
throw err;
302+
}
303+
304+
}
194305

306+
// use liquid-node engine
195307
try {
196308
var locals = options.locals || {};
197309

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

233345
for (k in options.customTags){
234346
engine.registerTag(k, tagFunctions[k]);
235-
/*Tell jshint there's no problem with having this function in the loop */
236-
/*jshint -W083 */
237-
// compileOptions.customTags[k] = function (context, name, body){
238-
// var tpl = tagFunctions[name](body.trim());
239-
// context.astStack.push(engine.parse(tpl));
240-
// };
241-
/*jshint +W083 */
242347
}
243348
delete options.customTags;
244349
}
@@ -265,9 +370,6 @@ exports.liquid.render = function(str, options, fn){
265370
}
266371
});
267372

268-
// var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
269-
// tmpl(context, fn);
270-
271373
} catch (err) {
272374
fn(err);
273375
}

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"jazz": "^0.0.18",
2828
"jqtpl": "^1.1.0",
2929
"just": "^0.1.8",
30+
"liquid-node": "^2.6.1",
3031
"liquor": "^0.0.5",
3132
"lodash": "^4.0.0",
3233
"mocha": "^2.3.4",
@@ -41,9 +42,9 @@
4142
"slm": "^0.5.0",
4243
"swig": "^1.4.1",
4344
"templayed": ">=0.2.3",
44-
"twig": "^0.8.2",
45-
"tinyliquid": "^0.2.22",
45+
"tinyliquid": "^0.2.30",
4646
"toffee": "^0.1.12",
47+
"twig": "^0.8.2",
4748
"underscore": "^1.3.3",
4849
"vash": "^0.8.7",
4950
"walrus": "^0.10.1",
@@ -59,8 +60,6 @@
5960
"test": "mocha"
6061
},
6162
"dependencies": {
62-
"bluebird": "^3.1.1",
63-
"liquid-node": "^2.6.1",
64-
"slm": "^0.5.0"
63+
"bluebird": "^3.1.1"
6564
}
6665
}
File renamed without changes.

test/fixtures/liquid/include.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{% include user %}
22
{% include sub/file %}
3-
{% include "footer.liquid" %}
3+
{% include "footer.html" %}

test/shared/filters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports.test = function(name) {
1313
var str = fs.readFileSync('test/fixtures/' + name + '/filters.' + name).toString();
1414

1515
var locals = { user: user, filters: { toupper: function(object) {
16-
return String(object).toUpperCase();
16+
return object.toUpperCase();
1717
}}};
1818

1919
cons[name].render(str, locals, function(err, html){

test/shared/includes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.test = function(name) {
1616
if (err) return done(err);
1717
try{
1818
if (name === 'liquid') {
19-
html.should.eql('<p>Tobi</p>\n<section></section>\n<footer></footer>');
19+
html.should.eql('<p>Tobi</p><section></section><footer></footer>');
2020
} else {
2121
html.should.eql('<p>Tobi</p>');
2222
}

0 commit comments

Comments
 (0)