Skip to content

Commit

Permalink
Merge pull request #5 from RobLoach/dynamic-filters
Browse files Browse the repository at this point in the history
Allow dynamic registration of filters
  • Loading branch information
thiagodemellobueno committed Jun 12, 2015
2 parents 9da26eb + 6c46200 commit 609094c
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

};
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 11 additions & 0 deletions test/fixtures/dynamic/build/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>

Encrypted: Ncjji&amp;Qitjb&#39;


This is an example.


</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/dynamic/expected/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>

Encrypted: Ncjji&amp;Qitjb&#39;


This is an example.


</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/dynamic/src/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
template: layout.html
title: Hello World!
---

This is an example.

8 changes: 8 additions & 0 deletions test/fixtures/dynamic/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>

Encrypted: {{ title|xorcrypt }}

{{contents}}
</body>
</html>
12 changes: 12 additions & 0 deletions test/fixtures/simple/build/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>

Title: Hello World!
Slug: hello-world


This is an example.


</body>
</html>
12 changes: 12 additions & 0 deletions test/fixtures/simple/expected/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>

Title: Hello World!
Slug: hello-world


This is an example.


</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/simple/src/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
template: layout.html
title: Hello World!
---

This is an example.

9 changes: 9 additions & 0 deletions test/fixtures/simple/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>

Title: {{ title }}
Slug: {{ title|slug }}

{{contents}}
</body>
</html>
46 changes: 46 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

});

0 comments on commit 609094c

Please sign in to comment.