From 6c46200db3f86c53040d49b3ed3587a01e5773e5 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Fri, 12 Jun 2015 14:46:18 -0400 Subject: [PATCH] Allow dynamic registration of filters --- README.md | 25 +++++++++++ lib/index.js | 17 ++++++++ package.json | 10 ++++- test/fixtures/dynamic/build/example.html | 11 +++++ test/fixtures/dynamic/expected/example.html | 11 +++++ test/fixtures/dynamic/src/example.html | 7 ++++ test/fixtures/dynamic/templates/layout.html | 8 ++++ test/fixtures/simple/build/example.html | 12 ++++++ test/fixtures/simple/expected/example.html | 12 ++++++ test/fixtures/simple/src/example.html | 7 ++++ test/fixtures/simple/templates/layout.html | 9 ++++ test/index.js | 46 +++++++++++++++++++++ 12 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/dynamic/build/example.html create mode 100644 test/fixtures/dynamic/expected/example.html create mode 100644 test/fixtures/dynamic/src/example.html create mode 100644 test/fixtures/dynamic/templates/layout.html create mode 100644 test/fixtures/simple/build/example.html create mode 100644 test/fixtures/simple/expected/example.html create mode 100644 test/fixtures/simple/src/example.html create mode 100644 test/fixtures/simple/templates/layout.html create mode 100644 test/index.js diff --git a/README.md b/README.md index 47e2c9a..ef143ac 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,31 @@ metalsmith .use(templates('swig')); ``` +### Filters + +Add your own filters dynamically with either a function, or a require() string. + +```js +var swigHelpers = require('metalsmith-swig-helpers'); +var templates = require('metalsmith-templates'); +metalsmith +.use(swig-helpers({ + filters: { + // Append a ! at the end of the given content. + // {{ title|exclamation }} + "exclamation": function(content) { + return content + "!" + }, + + // Encryption filter + // {{ title|xorcrypt }} + "xorcrypt": "xor-crypt" // Does a require() on "xor-crypt" + } +})) +.use(templates('swig')); +``` + + ## License MIT diff --git a/lib/index.js b/lib/index.js index 1cb5517..d2c1b55 100644 --- a/lib/index.js +++ b/lib/index.js @@ -65,6 +65,23 @@ module.exports = function plugin( data ) { return input + "?" + Date.now(); }); + // Dynamic Filters + // Take an array of functions or strings. + for (var name in data.filters || {}) { + var filter = null; + switch (typeof data.filters[name]) { + case "string": + filter = require(data.filters[name]); + break; + case "function": + default: + filter = data.filters[name]; + break; + } + swig.setFilter(name, filter); + } + + done(); }; diff --git a/package.json b/package.json index 7427f98..bb28e7a 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,14 @@ "homepage": "https://github.com/madeofpeople/metalsmith-swigh-helpers", "_from": "metalsmith-swig-helpers@", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha --reporter spec" + }, + "devDependencies": { + "assert-dir-equal": "^1.0.1", + "metalsmith": "^1.7.0", + "metalsmith-templates": "^0.7.0", + "mocha": "^2.2.5", + "rimraf": "^2.4.0", + "xor-crypt": "0.0.2" } } diff --git a/test/fixtures/dynamic/build/example.html b/test/fixtures/dynamic/build/example.html new file mode 100644 index 0000000..7cb8985 --- /dev/null +++ b/test/fixtures/dynamic/build/example.html @@ -0,0 +1,11 @@ + + + +Encrypted: Ncjji&Qitjb' + + +This is an example. + + + + diff --git a/test/fixtures/dynamic/expected/example.html b/test/fixtures/dynamic/expected/example.html new file mode 100644 index 0000000..7cb8985 --- /dev/null +++ b/test/fixtures/dynamic/expected/example.html @@ -0,0 +1,11 @@ + + + +Encrypted: Ncjji&Qitjb' + + +This is an example. + + + + diff --git a/test/fixtures/dynamic/src/example.html b/test/fixtures/dynamic/src/example.html new file mode 100644 index 0000000..06c4b48 --- /dev/null +++ b/test/fixtures/dynamic/src/example.html @@ -0,0 +1,7 @@ +--- +template: layout.html +title: Hello World! +--- + +This is an example. + diff --git a/test/fixtures/dynamic/templates/layout.html b/test/fixtures/dynamic/templates/layout.html new file mode 100644 index 0000000..7dcf7a1 --- /dev/null +++ b/test/fixtures/dynamic/templates/layout.html @@ -0,0 +1,8 @@ + + + +Encrypted: {{ title|xorcrypt }} + +{{contents}} + + diff --git a/test/fixtures/simple/build/example.html b/test/fixtures/simple/build/example.html new file mode 100644 index 0000000..6b97c10 --- /dev/null +++ b/test/fixtures/simple/build/example.html @@ -0,0 +1,12 @@ + + + +Title: Hello World! +Slug: hello-world + + +This is an example. + + + + diff --git a/test/fixtures/simple/expected/example.html b/test/fixtures/simple/expected/example.html new file mode 100644 index 0000000..6b97c10 --- /dev/null +++ b/test/fixtures/simple/expected/example.html @@ -0,0 +1,12 @@ + + + +Title: Hello World! +Slug: hello-world + + +This is an example. + + + + diff --git a/test/fixtures/simple/src/example.html b/test/fixtures/simple/src/example.html new file mode 100644 index 0000000..06c4b48 --- /dev/null +++ b/test/fixtures/simple/src/example.html @@ -0,0 +1,7 @@ +--- +template: layout.html +title: Hello World! +--- + +This is an example. + diff --git a/test/fixtures/simple/templates/layout.html b/test/fixtures/simple/templates/layout.html new file mode 100644 index 0000000..4586f75 --- /dev/null +++ b/test/fixtures/simple/templates/layout.html @@ -0,0 +1,9 @@ + + + +Title: {{ title }} +Slug: {{ title|slug }} + +{{contents}} + + diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..39fc7fb --- /dev/null +++ b/test/index.js @@ -0,0 +1,46 @@ + +var equal = require('assert-dir-equal'); +var swigHelpers = require('..'); +var templates = require('metalsmith-templates'); +var Metalsmith = require('metalsmith'); +var rm = require('rimraf').sync; + +describe('metalsmith-ignore', function(){ + + it('should register swig helpers', function(done){ + rm('test/fixtures/simple/build'); + var m = Metalsmith('test/fixtures/simple') + .use(swigHelpers({})) + .use(templates({ + engine: 'swig', + directory: 'templates' + })); + + m.build(function(err){ + if (err) return done(err); + equal('test/fixtures/simple/build', 'test/fixtures/simple/expected'); + done(); + }); + }); + + it('should register dynamic swig helpers', function(done){ + rm('test/fixtures/dynamic/build'); + var m = Metalsmith('test/fixtures/dynamic') + .use(swigHelpers({ + filters: { + "xorcrypt": "xor-crypt" + } + })) + .use(templates({ + engine: 'swig', + directory: 'templates' + })); + + m.build(function(err){ + if (err) return done(err); + equal('test/fixtures/dynamic/build', 'test/fixtures/dynamic/expected'); + done(); + }); + }); + +});