adding `master` template option and tests #21

Closed
wants to merge 3 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+85 −19
Diff settings

Always

Just for now

View
@@ -1,7 +1,7 @@
var consolidate = require('consolidate');
var debug = require('debug')('metalsmith-templates');
-var each = require('async').each;
+var async = require('async');
var extend = require('extend');
var join = require('path').join;
var match = require('multimatch');
@@ -41,50 +41,49 @@ function plugin(opts){
var pattern = opts.pattern;
var inPlace = opts.inPlace;
var def = opts.default;
+ var master = opts.master;
var params = omit(opts, settings);
return function(files, metalsmith, done){
var metadata = metalsmith.metadata();
function check(file){
var data = files[file];
- var tmpl = data.template || def;
if (pattern && !match(file, pattern)[0]) return false;
- if (!inPlace && !tmpl) return false;
+ if (!inPlace && !data.template && !def && !master) return false;
return true;
}
- Object.keys(files).forEach(function(file){
- if (!check(file)) return;
- debug('stringifying file: %s', file);
- var data = files[file];
- data.contents = data.contents.toString();
- });
-
- each(Object.keys(files), convert, done);
+ async.each(Object.keys(files), convert, done);
function convert(file, done){
if (!check(file)) return done();
debug('converting file: %s', file);
var data = files[file];
+ data.contents = data.contents.toString();
var clone = extend({}, params, metadata, data);
var str;
var render;
+ var templates = [];
if (inPlace) {
- str = clone.contents;
+ templates.push(clone.contents);
render = consolidate[engine].render;
} else {
- str = metalsmith.join(dir, data.template || def);
+ if (data.template || def) templates.push(metalsmith.join(dir, data.template || def));
+ if (master) templates.push(metalsmith.join(dir, master));
render = consolidate[engine];
}
- render(str, clone, function(err, str){
- if (err) return done(err);
- data.contents = new Buffer(str);
- debug('converted file: %s', file);
- done();
- });
+ async.eachSeries(templates,function(tmplname,done){
+ render(tmplname, clone, function(err, tmplname){
+ if (err) return done(err);
+ data.contents = clone.contents = (new Buffer(tmplname)).toString();
+ debug('converted file %s using template %s', file, tmplname);
+ done();
+ });
+ },done);
+
}
};
}
@@ -0,0 +1,8 @@
+### MASTER ###
+I use a default template
+##############
+---DEFAULT---
+I use a default template
+BODY
+-------------
+##############
@@ -0,0 +1 @@
+I shouldn't be templated
@@ -0,0 +1,8 @@
+### MASTER ###
+I use a specific template
+##############
+--TEMPLATE---
+I use a specific template
+BODY
+-------------
+##############
@@ -0,0 +1,8 @@
+### MASTER ###
+I use a default template
+##############
+---DEFAULT---
+I use a default template
+BODY
+-------------
+##############
@@ -0,0 +1 @@
+I shouldn't be templated
@@ -0,0 +1,8 @@
+### MASTER ###
+I use a specific template
+##############
+--TEMPLATE---
+I use a specific template
+BODY
+-------------
+##############
@@ -0,0 +1,4 @@
+---
+title: I use a default template
+---
+BODY
@@ -0,0 +1 @@
+I shouldn't be templated
@@ -0,0 +1,5 @@
+---
+title: I use a specific template
+template: template.html
+---
+BODY
@@ -0,0 +1,4 @@
+---DEFAULT---
+{{title}}
+{{contents}}
+-------------
@@ -0,0 +1,5 @@
+### MASTER ###
+{{title}}
+##############
+{{contents}}
+##############
@@ -0,0 +1,4 @@
+--TEMPLATE---
+{{title}}
+{{contents}}
+-------------
View
@@ -85,4 +85,14 @@ describe('metalsmith-templates', function(){
done();
});
});
+
+ it('should apply master template', function(done){
+ Metalsmith('test/fixtures/master')
+ .use(templates({ engine: 'swig', master: 'master.html', default: 'default.html', pattern: '*.md' }))
+ .build(function(err){
+ if (err) return done(err);
+ equal('test/fixtures/master/expected', 'test/fixtures/master/build');
+ done();
+ });
+ });
});