Skip to content

Commit 149228f

Browse files
committed
Merge branch 'feature/liquid-node' of https://github.com/cameronroe/consolidate.js into cameronroe-feature/liquid-node
2 parents 213a1e2 + 76af43c commit 149228f

File tree

3 files changed

+135
-42
lines changed

3 files changed

+135
-42
lines changed

lib/consolidate.js

Lines changed: 132 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ function readPartials(path, options, fn) {
121121
if (index === keys.length) return fn(null);
122122
var key = keys[index];
123123
var partialPath = partials[key];
124-
124+
125125
var file;
126126
if (isAbsolute(partialPath)) {
127127
if (extname(partialPath) !== '') {
128128
file = partialPath;
129129
} else {
130130
file = join(partialPath + extname(path));
131-
}
131+
}
132132
} else {
133133
file = join(dirname(path), partialPath + extname(path));
134134
}
@@ -202,38 +202,137 @@ exports.liquid = fromStringRenderer('liquid');
202202
* `includeDir` will also become a local.
203203
*/
204204

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

212-
/**
213-
* Note that there's a bug in the library that doesn't allow us to pass
214-
* the locals to newContext(), hence looping through the keys:
215-
*/
304+
// use tinyliquid engine
305+
_renderTinyliquid(engine, str, options, fn);
216306

217-
if (options.locals){
218-
for (k in options.locals){
219-
context.setLocals(k, options.locals[k]);
220-
}
221-
delete options.locals;
307+
return;
308+
309+
} catch (err) {
310+
311+
// set up liquid-node engine
312+
try {
313+
Liquid = requires.liquid = require('liquid-node');
314+
engine = new Liquid.Engine;
315+
} catch (err) {
316+
throw err;
222317
}
223318

319+
}
320+
321+
// use liquid-node engine
322+
try {
323+
var locals = options.locals || {};
324+
224325
if (options.meta){
225-
context.setLocals('page', options.meta);
326+
locals.pages = options.meta;
226327
delete options.meta;
227328
}
228329

229330
/**
230331
* Add any defined filters:
231332
*/
232333

233-
if (options.filters){
234-
for (k in options.filters){
235-
context.setFilter(k, options.filters[k]);
236-
}
334+
if (options.filters) {
335+
engine.registerFilters(options.filters);
237336
delete options.filters;
238337
}
239338

@@ -242,16 +341,7 @@ exports.liquid.render = function(str, options, fn){
242341
*/
243342

244343
var includeDir = options.includeDir || process.cwd();
245-
246-
context.onInclude(function (name, callback) {
247-
var extname = path.extname(name) ? '' : '.liquid';
248-
var filename = path.resolve(includeDir, name + extname);
249-
250-
fs.readFile(filename, {encoding: 'utf8'}, function (err, data){
251-
if (err) return callback(err);
252-
callback(null, engine.parse(data));
253-
});
254-
});
344+
engine.fileSystem = new Liquid.LocalFileSystem(includeDir, 'liquid');
255345
delete options.includeDir;
256346

257347
/**
@@ -268,13 +358,7 @@ exports.liquid.render = function(str, options, fn){
268358
var tagFunctions = options.customTags;
269359

270360
for (k in options.customTags){
271-
/*Tell jshint there's no problem with having this function in the loop */
272-
/*jshint -W083 */
273-
compileOptions.customTags[k] = function (context, name, body){
274-
var tpl = tagFunctions[name](body.trim());
275-
context.astStack.push(engine.parse(tpl));
276-
};
277-
/*jshint +W083 */
361+
engine.registerTag(k, tagFunctions[k]);
278362
}
279363
delete options.customTags;
280364
}
@@ -283,16 +367,24 @@ exports.liquid.render = function(str, options, fn){
283367
* Now anything left in `options` becomes a local:
284368
*/
285369

286-
for (k in options){
287-
context.setLocals(k, options[k]);
370+
for (var k in options){
371+
locals[k] = options[k];
288372
}
289373

290374
/**
291375
* Finally, execute the template:
292376
*/
293377

294-
var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
295-
tmpl(context, fn);
378+
return engine
379+
.parseAndRender(str, locals)
380+
.nodeify(function (err, result) {
381+
if (err) {
382+
throw new Error(err);
383+
} else {
384+
return fn(null, result);
385+
}
386+
});
387+
296388
} catch (err) {
297389
fn(err);
298390
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"jazz": "^0.0.18",
4141
"jqtpl": "~1.1.0",
4242
"just": "^0.1.8",
43+
"liquid-node": "^2.6.1",
4344
"liquor": "^0.0.5",
4445
"lodash": "^4.0.0",
4546
"marko": "^3.12.0",
@@ -58,7 +59,7 @@
5859
"swig": "^1.4.1",
5960
"teacup": "^2.0.0",
6061
"templayed": ">=0.2.3",
61-
"tinyliquid": "^0.2.22",
62+
"tinyliquid": "^0.2.30",
6263
"toffee": "^0.1.12",
6364
"twig": "^0.10.0",
6465
"underscore": "^1.3.3",

test/shared/includes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var should = require('should');
66
exports.test = function(name) {
77
var user = { name: 'Tobi' };
88

9-
describe(name, function(){
9+
describe(name, function() {
1010

1111
it('should support includes', function(done) {
1212
var str = fs.readFileSync('test/fixtures/' + name + '/include.' + name).toString();

0 commit comments

Comments
 (0)