diff --git a/bin/swig.js b/bin/swig.js index cbdd3bb6..ef31c25b 100755 --- a/bin/swig.js +++ b/bin/swig.js @@ -22,6 +22,8 @@ var command, j: 'Variable context as a JSON file.', c: 'Variable context as a CommonJS-style file. Used only if option `j` is not provided.', m: 'Minify compiled functions with uglify-js', + 'filters': 'Custom filters as a CommonJS-style file', + 'tags': 'Custom tags as a CommonJS-style file', 'wrap-start': 'Template wrapper beginning for "compile".', 'wrap-end': 'Template wrapper end for "compile".', 'method-name': 'Method name to set template to and run from.' @@ -98,6 +100,38 @@ if (argv.o !== 'stdout') { }; } +if (argv.filters) { + var cfilters, + k; + + argv.filters = path.resolve(argv.filters); + cfilters = require(argv.filters); + + for (k in cfilters) { + if (cfilters.hasOwnProperty(k)) { + swig.setFilter(k, cfilters[k]); + } + } +} + +if (argv.tags) { + var ctags, + k; + + argv.tags = path.resolve(argv.tags); + ctags = require(argv.tags); + + for (k in ctags) { + if (ctags.hasOwnProperty(k)) { + var parse = ctags[k].parse, + compile = ctags[k].compile, + ends = ctags[k].ends, + block = ctags[k].blockLevel; + swig.setTag(k, parse, compile, ends, block); + } + } +} + switch (command) { case 'compile': fn = function (file, str) { diff --git a/docs/docs/cli.html b/docs/docs/cli.html index 9a7c5c9c..2d57a8cf 100644 --- a/docs/docs/cli.html +++ b/docs/docs/cli.html @@ -27,6 +27,8 @@

Options

-j, --json Variable context as a JSON file. -c, --context Variable context as a CommonJS-style file. Used only if option `j` is not provided. -m, --minify Minify compiled functions with uglify-js +--filters Custom filters as a CommonJS-style file +--tags Custom tags as a CommonJS-style file --wrap-start Template wrapper beginning for "compile". [default: "var tpl = "] --wrap-end Template wrapper end for "compile". [default: ";"] --method-name Method name to set template to and run from. [default: "tpl"] diff --git a/tests/bin.filters.js b/tests/bin.filters.js new file mode 100644 index 00000000..b50d312e --- /dev/null +++ b/tests/bin.filters.js @@ -0,0 +1,3 @@ +exports.benice = function (input) { + return input + ' please!'; +}; diff --git a/tests/bin.tags.js b/tests/bin.tags.js new file mode 100644 index 00000000..2a686367 --- /dev/null +++ b/tests/bin.tags.js @@ -0,0 +1,15 @@ + +function parse(str, line, parser, types) { + return true; +} + +function compile(compiler, args, content) { + return compiler(content) + '\n' + + '_output += " tortilla!"'; +} + +exports.tortilla = { + parse: parse, + compile: compile, + ends: true +}; diff --git a/tests/bin.test.js b/tests/bin.test.js index 53b3d23e..35bef0e3 100644 --- a/tests/bin.test.js +++ b/tests/bin.test.js @@ -99,3 +99,38 @@ describe('bin/swig compile & run from swig', function () { }); }); }); + +describe('bin/swig compile & run with custom extensions', function () { + var tmp = fixPath(__dirname + '/../tmp'); + + it('works with custom filters', function (done) { + var filters = fixPath(__dirname + '/bin.filters.js'), + template = 'I want {{ alpha|benice }}', + p = tmp + '/filter.test.html'; + + fs.writeFile(p, template, function () { + exec('node ' + bin + ' compile ' + p + ' --filters ' + filters + ' -o ' + tmp, function (err, stdout, stderr) { + var locals = fixPath(__dirname + '/bin.locals.json'); + exec('node ' + bin + ' run ' + p + ' -j ' + locals + ' --filters ' + filters, function (err, stdout, stdrr) { + expect(stdout.replace(/\n$/, '')).to.equal('I want Nachos please!'); + done(); + }); + }); + }); + }); + + it('works with custom tags', function (done) { + var tags = fixPath(__dirname + '/bin.tags.js'), + template = '{% tortilla %}flour{% endtortilla %}', + p = tmp + '/tag.test.html'; + + fs.writeFile(p, template, function () { + exec('node ' + bin + ' compile ' + p + ' --tags ' + tags + ' -o ' + tmp, function (err, stdout, stderr) { + exec('node ' + bin + ' run ' + p, function (err, stdout, stdrr) { + expect(stdout.replace(/\n$/, '')).to.equal('flour tortilla!'); + done(); + }); + }); + }); + }); +});