From e1c35176a052bd26418d4a7ab7baa32577d9f6a2 Mon Sep 17 00:00:00 2001 From: Cameron J Roe Date: Wed, 27 Jan 2016 00:15:50 -0500 Subject: [PATCH] fix tests --- lib/consolidate.js | 126 ++++++++++++++++-- package.json | 9 +- .../liquid/{footer.liquid => footer.html} | 0 test/fixtures/liquid/include.liquid | 2 +- test/shared/filters.js | 2 +- test/shared/includes.js | 2 +- 6 files changed, 121 insertions(+), 20 deletions(-) rename test/fixtures/liquid/{footer.liquid => footer.html} (100%) diff --git a/lib/consolidate.js b/lib/consolidate.js index 50f2988..49fafdd 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -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 || {}; @@ -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; } @@ -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); } diff --git a/package.json b/package.json index a71ac5b..f83573c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -59,8 +60,6 @@ "test": "mocha" }, "dependencies": { - "bluebird": "^3.1.1", - "liquid-node": "^2.6.1", - "slm": "^0.5.0" + "bluebird": "^3.1.1" } } diff --git a/test/fixtures/liquid/footer.liquid b/test/fixtures/liquid/footer.html similarity index 100% rename from test/fixtures/liquid/footer.liquid rename to test/fixtures/liquid/footer.html diff --git a/test/fixtures/liquid/include.liquid b/test/fixtures/liquid/include.liquid index 8db7908..ad766c2 100644 --- a/test/fixtures/liquid/include.liquid +++ b/test/fixtures/liquid/include.liquid @@ -1,3 +1,3 @@ {% include user %} {% include sub/file %} -{% include "footer.liquid" %} \ No newline at end of file +{% include "footer.html" %} \ No newline at end of file diff --git a/test/shared/filters.js b/test/shared/filters.js index 8cb9542..625a227 100644 --- a/test/shared/filters.js +++ b/test/shared/filters.js @@ -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){ diff --git a/test/shared/includes.js b/test/shared/includes.js index d108514..fef5389 100644 --- a/test/shared/includes.js +++ b/test/shared/includes.js @@ -16,7 +16,7 @@ exports.test = function(name) { if (err) return done(err); try{ if (name === 'liquid') { - html.should.eql('

Tobi

\n
\n'); + html.should.eql('

Tobi

'); } else { html.should.eql('

Tobi

'); }