From 6cc983ad26a9118ad335e8fd11119bca0057131a Mon Sep 17 00:00:00 2001 From: Ajay Sabhaney Date: Mon, 13 Jan 2014 02:50:29 -0700 Subject: [PATCH 1/4] Added arguments for custom tags and filters to CLI --- bin/swig.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/bin/swig.js b/bin/swig.js index cbdd3bb6..c9c401d5 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': 'File containing custom filters', + 'tags': 'File containing custom tags', '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) { From d53866fdc96433864b88ccd9707431dcbdc62645 Mon Sep 17 00:00:00 2001 From: Ajay Sabhaney Date: Mon, 13 Jan 2014 17:18:13 -0700 Subject: [PATCH 2/4] Added bin tests for compliling & running w/ custom filters & tags --- tests/bin.filters.js | 3 +++ tests/bin.tags.js | 15 +++++++++++++++ tests/bin.test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/bin.filters.js create mode 100644 tests/bin.tags.js diff --git a/tests/bin.filters.js b/tests/bin.filters.js new file mode 100644 index 00000000..268b43b8 --- /dev/null +++ b/tests/bin.filters.js @@ -0,0 +1,3 @@ +exports.benice = function (input) { + return input + ' please!'; +}; \ No newline at end of file diff --git a/tests/bin.tags.js b/tests/bin.tags.js new file mode 100644 index 00000000..7a777328 --- /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 +}; \ No newline at end of file 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(); + }); + }); + }); + }); +}); From 23cd09b52d5182f8525a079ea69d23256f1f93eb Mon Sep 17 00:00:00 2001 From: Ajay Sabhaney Date: Mon, 13 Jan 2014 17:30:46 -0700 Subject: [PATCH 3/4] Updated docs & bin usage --- bin/swig.js | 4 ++-- docs/docs/cli.html | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/swig.js b/bin/swig.js index c9c401d5..ef31c25b 100755 --- a/bin/swig.js +++ b/bin/swig.js @@ -22,8 +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': 'File containing custom filters', - 'tags': 'File containing custom tags', + '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.' 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"] From 7ca7cf5286ad87daba1ca422affe695cfa3acf06 Mon Sep 17 00:00:00 2001 From: Ajay Sabhaney Date: Mon, 13 Jan 2014 17:39:07 -0700 Subject: [PATCH 4/4] added newline characters at eof of new files --- tests/bin.filters.js | 2 +- tests/bin.tags.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/bin.filters.js b/tests/bin.filters.js index 268b43b8..b50d312e 100644 --- a/tests/bin.filters.js +++ b/tests/bin.filters.js @@ -1,3 +1,3 @@ exports.benice = function (input) { return input + ' please!'; -}; \ No newline at end of file +}; diff --git a/tests/bin.tags.js b/tests/bin.tags.js index 7a777328..2a686367 100644 --- a/tests/bin.tags.js +++ b/tests/bin.tags.js @@ -12,4 +12,4 @@ exports.tortilla = { parse: parse, compile: compile, ends: true -}; \ No newline at end of file +};