From 62a01acef7d0f3603417dbc3c3bc66256204e6b8 Mon Sep 17 00:00:00 2001 From: nebrelbug Date: Tue, 2 Oct 2018 20:22:16 -0600 Subject: [PATCH] Added Squirrelly as a template engine, tests passing (except for Hamlet), added Squirrelly to ReadMe --- Readme.md | 1 + lib/consolidate.js | 27 ++++++++++++++++++++ package.json | 7 ++--- test/consolidate.js | 3 +++ test/fixtures/squirrelly/helpers.squirrelly | 2 ++ test/fixtures/squirrelly/partials.squirrelly | 1 + test/fixtures/squirrelly/user.squirrelly | 1 + test/shared/helpers.js | 18 +++++++++++++ 8 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/squirrelly/helpers.squirrelly create mode 100644 test/fixtures/squirrelly/partials.squirrelly create mode 100644 test/fixtures/squirrelly/user.squirrelly diff --git a/Readme.md b/Readme.md index d618e04..e4b6db7 100644 --- a/Readme.md +++ b/Readme.md @@ -39,6 +39,7 @@ - [ractive](https://github.com/Rich-Harris/Ractive) - [react](https://github.com/facebook/react) - [slm](https://github.com/slm-lang/slm) + - [squirrelly](https://github.com/nebrelbug/squirrelly) [(website)](https://squirrelly.js.org) - [swig (maintained fork)](https://github.com/node-swig/swig-templates) - [swig (unmaintained)](https://github.com/paularmstrong/swig) - [teacup](https://github.com/goodeggs/teacup) diff --git a/lib/consolidate.js b/lib/consolidate.js index a198484..2d9c06c 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -1678,6 +1678,33 @@ exports.teacup.render = function(str, options, cb) { }); }; +/** + * Squirrelly support. + */ + +exports.squirrelly = fromStringRenderer('squirrelly'); + +/** + * Squirrelly string support. + */ + +exports.squirrelly.render = function(str, options, cb) { + return promisify(cb, function(cb) { + var engine = requires.squirrelly || (requires.squirrelly = require('squirrelly')); + try { + for (var partial in options.partials) { + engine.definePartial(partial, options.partials[partial]); + } + for (var helper in options.helpers) { + engine.defineHelper(helper, options.helpers[helper]); + } + var tmpl = cache(options) || cache(options, engine.Compile(str, options)); + cb(null, tmpl(options, engine)); + } catch (err) { + cb(err); + } + }); +}; /** * expose the instance of the engine */ diff --git a/package.json b/package.json index dcf3346..ade2628 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,9 @@ "react-dom": "^15.3.2", "should": "*", "slm": "^0.5.0", - "swig-templates": "^2.0.2", + "squirrelly": "^5.0.1", "swig": "^1.4.1", + "swig-templates": "^2.0.2", "teacup": "^2.0.0", "templayed": ">=0.2.3", "tinyliquid": "^0.2.30", @@ -78,9 +79,9 @@ "twig": "^0.10.0", "underscore": "^1.3.3", "vash": "^0.12.2", + "velocityjs": "^0.8.2", "walrus": "^0.10.1", - "whiskers": "^0.4.0", - "velocityjs": "^0.8.2" + "whiskers": "^0.4.0" }, "keywords": [ "engine", diff --git a/test/consolidate.js b/test/consolidate.js index 375bacc..eb668d9 100644 --- a/test/consolidate.js +++ b/test/consolidate.js @@ -68,3 +68,6 @@ require('./shared').test('marko'); require('./shared').test('bracket'); require('./shared').test('teacup'); require('./shared').test('velocityjs'); +require('./shared').test('squirrelly'); +require('./shared/partials').test('squirrelly'); +require('./shared/helpers').test('squirrelly'); diff --git a/test/fixtures/squirrelly/helpers.squirrelly b/test/fixtures/squirrelly/helpers.squirrelly new file mode 100644 index 0000000..94a91a6 --- /dev/null +++ b/test/fixtures/squirrelly/helpers.squirrelly @@ -0,0 +1,2 @@ +{{myhelper(options.user.name)}} +{{/myhelper}} \ No newline at end of file diff --git a/test/fixtures/squirrelly/partials.squirrelly b/test/fixtures/squirrelly/partials.squirrelly new file mode 100644 index 0000000..322719a --- /dev/null +++ b/test/fixtures/squirrelly/partials.squirrelly @@ -0,0 +1 @@ +{{include(partial)/}} \ No newline at end of file diff --git a/test/fixtures/squirrelly/user.squirrelly b/test/fixtures/squirrelly/user.squirrelly new file mode 100644 index 0000000..f5b9962 --- /dev/null +++ b/test/fixtures/squirrelly/user.squirrelly @@ -0,0 +1 @@ +

{{user.name}}

\ No newline at end of file diff --git a/test/shared/helpers.js b/test/shared/helpers.js index ddde9e9..5d5f23b 100644 --- a/test/shared/helpers.js +++ b/test/shared/helpers.js @@ -1,6 +1,7 @@ var cons = require('../../'); var handlebars = require('handlebars'); +var Sqrl = require('squirrelly'); var fs = require('fs'); var readFile = fs.readFile; var readFileSync = fs.readFileSync; @@ -34,6 +35,23 @@ exports.test = function(name) { done(); }); }); + } else if (name === 'squirrelly') { + user = { name: 'Tobi' }; + + // Use case: return safe HTML that won’t be escaped in the final render. + it('should support helpers', function(done) { + var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); + Sqrl.defineHelper('myhelper', function(args, content, blocks) { + return args[0].slice(1, -1); + }); + var options = { user: user }; + + cons[name].render(str, options, function(err, html) { + if (err) return done(err); + html.should.equal('strong>Tobi