├── .gitignore ├── History.md ├── Makefile ├── Readme.md ├── lib └── index.js ├── package.json └── test ├── fixtures ├── basic │ ├── build │ │ └── index.html │ ├── expected │ │ └── index.html │ ├── src │ │ └── index.html │ └── templates │ │ └── layout.html ├── binary │ ├── build │ │ ├── avatar.png │ │ └── image.png │ ├── expected │ │ ├── avatar.png │ │ └── image.png │ └── src │ │ ├── avatar.png │ │ └── image.png ├── default │ ├── build │ │ ├── default.md │ │ └── other.md │ ├── expected │ │ ├── default.md │ │ └── other.md │ ├── src │ │ ├── default.md │ │ └── other.md │ └── templates │ │ ├── default.html │ │ └── other.html ├── directory │ ├── build │ │ └── index.html │ ├── expected │ │ └── index.html │ ├── layouts │ │ └── layout.html │ └── src │ │ └── index.html ├── in-place │ ├── build │ │ └── index.html │ ├── expected │ │ └── index.html │ └── src │ │ └── index.html ├── metadata │ ├── build │ │ ├── one.html │ │ └── two.html │ ├── expected │ │ ├── one.html │ │ └── two.html │ ├── src │ │ ├── one.html │ │ └── two.html │ └── templates │ │ └── layout.html ├── partials │ ├── build │ │ └── index.html │ ├── expected │ │ └── index.html │ ├── src │ │ └── index.html │ └── templates │ │ ├── layout.html │ │ └── partials │ │ └── nav.html └── pattern │ ├── build │ ├── index.html │ └── index.md │ ├── expected │ ├── index.html │ └── index.md │ ├── src │ ├── index.html │ └── index.md │ └── templates │ └── layout.html └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.7.0 - March 29, 2015 3 | ---------------------- 4 | * ignore binary files 5 | 6 | 0.6.1 - February 28, 2015 7 | ------------------------- 8 | * fix for using partials more than once 9 | 10 | 0.6.0 - October 3, 2014 11 | ----------------------- 12 | * fix to use `path` for metalsmith `1.0.0` 13 | 14 | 0.5.2 - July 9, 2014 15 | -------------------- 16 | * fix breaking binary files 17 | 18 | 0.5.1 - June 11, 2014 19 | --------------------- 20 | * fix race condition with stringify file contents 21 | 22 | 0.5.0 - April 29, 2014 23 | ---------------------- 24 | * pass in options to consolidate.js 25 | 26 | 0.4.0 - April 2, 2014 27 | --------------------- 28 | * add `default` option 29 | 30 | 0.3.0 - March 10, 2014 31 | ---------------------- 32 | * add `inPlace` option 33 | * change `pattern` option to just filter 34 | 35 | 0.2.1 - March 10, 2014 36 | ---------------------- 37 | * fix bug in matching pattern logic 38 | 39 | 0.2.0 - March 8, 2014 40 | --------------------- 41 | * add rendering files in place with a `pattern` 42 | 43 | 0.1.0 - March 5, 2014 44 | --------------------- 45 | * add `string` engine convenience 46 | 47 | 0.0.6 - February 7, 2014 48 | ------------------------ 49 | * fix `directory` option bug 50 | 51 | 0.0.5 - February 7, 2014 52 | ------------------------ 53 | * stringify `contents` on the original file data 54 | 55 | 0.0.4 - February 6, 2014 56 | ------------------------ 57 | * switch to `extend` from `defaults` to avoid cloning 58 | * add debug statements 59 | 60 | 0.0.3 - February 6, 2014 61 | ------------------------ 62 | * fix to use buffers 63 | 64 | 0.0.2 - February 5, 2014 65 | ------------------------ 66 | * mix in metadata 67 | 68 | 0.0.1 - February 4, 2014 69 | ------------------------ 70 | :sparkles: -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | mocha=node_modules/.bin/mocha --reporter spec 3 | 4 | node_modules: package.json 5 | @npm install 6 | 7 | test: node_modules 8 | @$(mocha) 9 | 10 | test-debug: node_modules 11 | @$(mocha) debug 12 | 13 | .PHONY: test 14 | .PHONY: test-debug -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # metalsmith-templates 3 | 4 | _Note: this plugin is deprecated, and was split into two simpler plugins:_ 5 | 6 | - _[metalsmith-layouts](https://github.com/superwolff/metalsmith-layouts) for rendering templates inside nested layouts._ 7 | - _[metalsmith-in-place](https://github.com/superwolff/metalsmith-in-place) for render an individual file through a templating engine._ 8 | 9 | _Check out those to repositories!_ 10 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 2 | var consolidate = require('consolidate'); 3 | var debug = require('debug')('metalsmith-templates'); 4 | var each = require('async').each; 5 | var extend = require('extend'); 6 | var match = require('multimatch'); 7 | var omit = require('lodash.omit'); 8 | var utf8 = require('is-utf8'); 9 | 10 | /** 11 | * Expose `plugin`. 12 | */ 13 | 14 | module.exports = plugin; 15 | 16 | /** 17 | * Settings. 18 | */ 19 | 20 | var settings = ['engine', 'directory', 'pattern', 'inPlace', 'default']; 21 | 22 | /** 23 | * Metalsmith plugin to run files through any template in a template `dir`. 24 | * 25 | * @param {String or Object} options 26 | * @property {String} default (optional) 27 | * @property {String} directory (optional) 28 | * @property {String} engine 29 | * @property {String} inPlace (optional) 30 | * @property {String} pattern (optional) 31 | * @return {Function} 32 | */ 33 | 34 | function plugin(opts){ 35 | opts = opts || {}; 36 | if ('string' == typeof opts) opts = { engine: opts }; 37 | if (!opts.engine) throw new Error('"engine" option required'); 38 | 39 | var engine = opts.engine; 40 | var dir = opts.directory || 'templates'; 41 | var pattern = opts.pattern; 42 | var inPlace = opts.inPlace; 43 | var def = opts.default; 44 | var params = omit(opts, settings); 45 | 46 | if (!consolidate[engine]) { 47 | throw new Error('Unknown template engine: "' + engine + '"'); 48 | } 49 | 50 | return function(files, metalsmith, done){ 51 | var metadata = metalsmith.metadata(); 52 | var matches = {}; 53 | 54 | function check(file){ 55 | var data = files[file]; 56 | var tmpl = data.template || def; 57 | if (!utf8(data.contents)) return false; 58 | if (pattern && !match(file, pattern)[0]) return false; 59 | if (!inPlace && !tmpl) return false; 60 | return true; 61 | } 62 | 63 | Object.keys(files).forEach(function(file){ 64 | if (!check(file)) return; 65 | debug('stringifying file: %s', file); 66 | var data = files[file]; 67 | data.contents = data.contents.toString(); 68 | matches[file] = data; 69 | }); 70 | 71 | each(Object.keys(matches), convert, done); 72 | 73 | function convert(file, done){ 74 | debug('converting file: %s', file); 75 | var data = files[file]; 76 | var clonedParams = extend(true, {}, params); 77 | var clone = extend({}, clonedParams, metadata, data); 78 | var str; 79 | var render; 80 | 81 | if (inPlace) { 82 | str = clone.contents; 83 | render = consolidate[engine].render; 84 | } else { 85 | str = metalsmith.path(dir, data.template || def); 86 | render = consolidate[engine]; 87 | } 88 | 89 | render(str, clone, function(err, str){ 90 | if (err) return done(err); 91 | data.contents = new Buffer(str); 92 | debug('converted file: %s', file); 93 | done(); 94 | }); 95 | } 96 | }; 97 | } 98 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "metalsmith-templates", 3 | "description": "A metalsmith plugin to render files with templates.", 4 | "repository": "git://github.com/segmentio/metalsmith-templates.git", 5 | "version": "0.7.0", 6 | "license": "MIT", 7 | "main": "lib/index.js", 8 | "scripts": { 9 | "test": "make test" 10 | }, 11 | "dependencies": { 12 | "async": "~0.2.10", 13 | "consolidate": "^0.10.0", 14 | "debug": "~0.7.4", 15 | "extend": "~1.2.1", 16 | "is-utf8": "^0.2.0", 17 | "lodash.omit": "~2.4.1", 18 | "multimatch": "^0.1.0" 19 | }, 20 | "devDependencies": { 21 | "assert-dir-equal": "0.0.1", 22 | "handlebars": "^3.0.1", 23 | "metalsmith": "^1.0.1", 24 | "mocha": "1.x", 25 | "swig": "~1.3.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/fixtures/basic/build/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | body 5 | 6 | -------------------------------------------------------------------------------- /test/fixtures/basic/expected/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | body 5 | 6 | -------------------------------------------------------------------------------- /test/fixtures/basic/src/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | template: layout.html 3 | --- 4 | 5 | body -------------------------------------------------------------------------------- /test/fixtures/basic/templates/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{contents}} 4 | 5 | -------------------------------------------------------------------------------- /test/fixtures/binary/build/avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/build/avatar.png -------------------------------------------------------------------------------- /test/fixtures/binary/build/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/build/image.png -------------------------------------------------------------------------------- /test/fixtures/binary/expected/avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/expected/avatar.png -------------------------------------------------------------------------------- /test/fixtures/binary/expected/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/expected/image.png -------------------------------------------------------------------------------- /test/fixtures/binary/src/avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/src/avatar.png -------------------------------------------------------------------------------- /test/fixtures/binary/src/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/segment-boneyard/metalsmith-templates/fdb24036bafdff1503a0dfdc4a5962fbce4c867e/test/fixtures/binary/src/image.png -------------------------------------------------------------------------------- /test/fixtures/default/build/default.md: -------------------------------------------------------------------------------- 1 | Title 2 | 3 | Body -------------------------------------------------------------------------------- /test/fixtures/default/build/other.md: -------------------------------------------------------------------------------- 1 | 2 | Body -------------------------------------------------------------------------------- /test/fixtures/default/expected/default.md: -------------------------------------------------------------------------------- 1 | Rendered 2 | 3 | Body -------------------------------------------------------------------------------- /test/fixtures/default/expected/other.md: -------------------------------------------------------------------------------- 1 | 2 | Body -------------------------------------------------------------------------------- /test/fixtures/default/src/default.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Title 3 | --- 4 | 5 | Body -------------------------------------------------------------------------------- /test/fixtures/default/src/other.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Title 3 | template: other.html 4 | --- 5 | 6 | Body -------------------------------------------------------------------------------- /test/fixtures/default/templates/default.html: -------------------------------------------------------------------------------- 1 | {{title}} 2 | {{contents}} -------------------------------------------------------------------------------- /test/fixtures/default/templates/other.html: -------------------------------------------------------------------------------- 1 | {{contents}} -------------------------------------------------------------------------------- /test/fixtures/directory/build/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | body 5 | 6 | -------------------------------------------------------------------------------- /test/fixtures/directory/expected/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | body 5 | 6 | -------------------------------------------------------------------------------- /test/fixtures/directory/layouts/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{contents}} 4 | 5 | -------------------------------------------------------------------------------- /test/fixtures/directory/src/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | template: layout.html 3 | --- 4 | 5 | body -------------------------------------------------------------------------------- /test/fixtures/in-place/build/index.html: -------------------------------------------------------------------------------- 1 | 2 | First Last -------------------------------------------------------------------------------- /test/fixtures/in-place/expected/index.html: -------------------------------------------------------------------------------- 1 | 2 | First Last -------------------------------------------------------------------------------- /test/fixtures/in-place/src/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | name: First Last 3 | --- 4 | 5 | {{name}} -------------------------------------------------------------------------------- /test/fixtures/metadata/build/one.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |Nav
2 |Content
3 | 4 | -------------------------------------------------------------------------------- /test/fixtures/partials/expected/index.html: -------------------------------------------------------------------------------- 1 |Nav
2 |Content
3 | 4 | -------------------------------------------------------------------------------- /test/fixtures/partials/src/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | template: layout.html 3 | --- 4 |Content
5 | -------------------------------------------------------------------------------- /test/fixtures/partials/templates/layout.html: -------------------------------------------------------------------------------- 1 | {{>nav}} 2 | {{{contents}}} 3 | -------------------------------------------------------------------------------- /test/fixtures/partials/templates/partials/nav.html: -------------------------------------------------------------------------------- 1 |Nav
2 | -------------------------------------------------------------------------------- /test/fixtures/pattern/build/index.html: -------------------------------------------------------------------------------- 1 | 2 | Not matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/build/index.md: -------------------------------------------------------------------------------- 1 | Matched 2 | 3 | Matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/expected/index.html: -------------------------------------------------------------------------------- 1 | 2 | Not matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/expected/index.md: -------------------------------------------------------------------------------- 1 | Matched 2 | 3 | Matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/src/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | title: Not matched 3 | template: layout.html 4 | --- 5 | 6 | Not matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/src/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Matched 3 | template: layout.html 4 | --- 5 | 6 | Matched body -------------------------------------------------------------------------------- /test/fixtures/pattern/templates/layout.html: -------------------------------------------------------------------------------- 1 | {{title}} 2 | {{contents}} -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | var assert = require('assert'); 3 | var equal = require('assert-dir-equal'); 4 | var Metalsmith = require('metalsmith'); 5 | var templates = require('..'); 6 | 7 | describe('metalsmith-templates', function(){ 8 | it('should render a basic template', function(done){ 9 | Metalsmith('test/fixtures/basic') 10 | .use(templates({ engine: 'swig' })) 11 | .build(function(err){ 12 | if (err) return done(err); 13 | equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); 14 | done(); 15 | }); 16 | }); 17 | 18 | it('should accept an engine string', function(done){ 19 | Metalsmith('test/fixtures/basic') 20 | .use(templates('swig')) 21 | .build(function(err){ 22 | if (err) return done(err); 23 | equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); 24 | done(); 25 | }); 26 | }); 27 | 28 | it('should accept an inPlace option', function(done){ 29 | Metalsmith('test/fixtures/in-place') 30 | .use(templates({ engine: 'swig', inPlace: true })) 31 | .build(function(err){ 32 | if (err) return done(err); 33 | equal('test/fixtures/in-place/expected', 'test/fixtures/in-place/build'); 34 | done(); 35 | }); 36 | }); 37 | 38 | it('should accept a pattern to match', function(done){ 39 | Metalsmith('test/fixtures/pattern') 40 | .use(templates({ engine: 'swig', pattern: '*.md' })) 41 | .build(function(err){ 42 | if (err) return done(err); 43 | equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build'); 44 | done(); 45 | }); 46 | }); 47 | 48 | it('should accept a default template', function(done){ 49 | Metalsmith('test/fixtures/default') 50 | .use(templates({ engine: 'swig', pattern: '*.md', default: 'default.html' })) 51 | .build(function(err){ 52 | if (err) return done(err); 53 | equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build'); 54 | done(); 55 | }); 56 | }); 57 | 58 | it('should accept a different templates directory', function(done){ 59 | Metalsmith('test/fixtures/directory') 60 | .use(templates({ engine: 'swig', directory: 'layouts' })) 61 | .build(function(err){ 62 | if (err) return done(err); 63 | equal('test/fixtures/directory/expected', 'test/fixtures/directory/build'); 64 | done(); 65 | }); 66 | }); 67 | 68 | it('should mix in global metadata', function(done){ 69 | Metalsmith('test/fixtures/metadata') 70 | .metadata({ title: 'Global Title' }) 71 | .use(templates({ engine: 'swig' })) 72 | .build(function(err){ 73 | if (err) return done(err); 74 | equal('test/fixtures/metadata/expected', 'test/fixtures/metadata/build'); 75 | done(); 76 | }); 77 | }); 78 | 79 | it('should preserve binary files', function(done){ 80 | Metalsmith('test/fixtures/binary') 81 | .use(templates({ engine: 'handlebars', pattern: '**', inPlace: true })) 82 | .build(function(err){ 83 | if (err) return done(err); 84 | equal('test/fixtures/binary/expected', 'test/fixtures/binary/build'); 85 | done(); 86 | }); 87 | }); 88 | 89 | it('should be capable of processing partials multiple times', function(done){ 90 | var instance = Metalsmith('test/fixtures/partials') 91 | .use(templates({ 92 | engine: 'handlebars', 93 | partials: { nav: 'partials/nav'} 94 | })); 95 | 96 | instance.build(function(err){ 97 | if (err) return done(err); 98 | instance.build(function(err){ 99 | if (err) return done(err); 100 | equal('test/fixtures/partials/expected', 'test/fixtures/partials/build'); 101 | done(); 102 | }); 103 | }); 104 | }); 105 | }); 106 | --------------------------------------------------------------------------------