Merge pull request #74 from superwolff/bugfixes-and-new-options

Bugfixes and new options
This commit is contained in:
Ismay 2016-02-14 15:22:23 +01:00
commit 81cdcc0ef0
20 changed files with 167 additions and 20 deletions

4
.hound.yml Normal file
View file

@ -0,0 +1,4 @@
javascript:
enabled: false
eslint:
enabled: true

View file

@ -1,4 +1,4 @@
sudo: false
language: node_js
node_js:
- "0.12"
- "node"

View file

@ -1,11 +0,0 @@
eslint=node_modules/.bin/eslint lib/**/*.js test/index.js
mocha=node_modules/.bin/mocha --reporter spec --harmony-generators
node_modules: package.json
@npm install
test: node_modules
@$(mocha)
@$(eslint)
.PHONY: test

View file

@ -1,10 +1,17 @@
# metalsmith-layouts
[![npm](https://img.shields.io/npm/v/metalsmith-layouts.svg)](https://www.npmjs.com/package/metalsmith-layouts) [![Build Status](https://travis-ci.org/superwolff/metalsmith-layouts.svg)](https://travis-ci.org/superwolff/metalsmith-layouts) [![Dependency Status](https://david-dm.org/superwolff/metalsmith-layouts.svg)](https://david-dm.org/superwolff/metalsmith-layouts) [![devDependency Status](https://david-dm.org/superwolff/metalsmith-layouts/dev-status.svg)](https://david-dm.org/superwolff/metalsmith-layouts#info=devDependencies) [![npm](https://img.shields.io/npm/dm/metalsmith-layouts.svg)](https://www.npmjs.com/package/metalsmith-layouts)
[![npm version][version-badge]][version-url]
[![build status][build-badge]][build-url]
[![dependency status][dependency-badge]][dependency-url]
[![devdependency status][devdependency-badge]][devdependency-url]
[![downloads][downloads-badge]][downloads-url]
> A metalsmith plugin for layouts
This plugin allows you to apply layouts to your source files. It passes your source files to the selected layout as `contents` and renders the result with the templating engine of your choice. You can use any templating engine supported by [consolidate.js](https://github.com/tj/consolidate.js#supported-template-engines).
[![stack overflow][stackoverflow-badge]][stackoverflow-url]
[![slack chat][slack-badge]][slack-url]
This plugin allows you to apply layouts to your source files. It passes your source files to the selected layout as the variable `contents` and renders the result with the templating engine of your choice. You can use any templating engine supported by [consolidate.js](https://github.com/tj/consolidate.js#supported-template-engines). For support questions please use [stack overflow][stackoverflow-url] or our [slack channel][slack-url]. For templating engine specific questions try the aforementioned channels, as well as the documentation for [consolidate.js](https://github.com/tj/consolidate.js) and your templating engine of choice.
## Installation
@ -64,7 +71,7 @@ Results in `build/index.html`:
</html>
```
This is a very basic example. For a ready-to-use boilerplate that utilizes this plugin see [metalsmith-boilerplates](https://github.com/superwolff/metalsmith-boilerplates).
This is a very basic example. For more elaborate examples see the [metalsmith tag on stack overflow][stackoverflow-url].
## Options
@ -143,6 +150,8 @@ The directory where `metalsmith-layouts` looks for partials. Each partial is nam
Would mean that a partial at `partials/nav.html` can be used in layouts as `{{> nav }}`, and `partials/nested/footer.html` can be used as `{{> nested/footer }}`. Note that passing anything but a string to the `partials` option will pass the option on to consolidate.
Make sure to check [consolidate.js](https://github.com/tj/consolidate.js) and your templating engine's documentation for guidelines on how to use partials.
### pattern
Only files that match this pattern will be processed. So this `metalsmith.json`:
@ -174,3 +183,18 @@ This plugin is a fork of the now deprecated [metalsmith-templates](https://githu
## License
MIT
[build-badge]: https://travis-ci.org/superwolff/metalsmith-layouts.svg
[build-url]: https://travis-ci.org/superwolff/metalsmith-layouts
[dependency-badge]: https://david-dm.org/superwolff/metalsmith-layouts.svg
[dependency-url]: https://david-dm.org/superwolff/metalsmith-layouts
[devdependency-badge]: https://david-dm.org/superwolff/metalsmith-layouts/dev-status.svg
[devdependency-url]: https://david-dm.org/superwolff/metalsmith-layouts#info=devDependencies
[downloads-badge]: https://img.shields.io/npm/dm/metalsmith-layouts.svg
[downloads-url]: https://www.npmjs.com/package/metalsmith-layouts
[slack-badge]: https://img.shields.io/badge/Slack-Join%20Chat%20→-blue.svg
[slack-url]: http://metalsmith-slack.herokuapp.com/
[stackoverflow-badge]: https://img.shields.io/badge/stack%20overflow-%23metalsmith-red.svg
[stackoverflow-url]: http://stackoverflow.com/questions/tagged/metalsmith
[version-badge]: https://img.shields.io/npm/v/metalsmith-layouts.svg
[version-url]: https://www.npmjs.com/package/metalsmith-layouts

View file

@ -31,8 +31,8 @@ function readPartials(partialsRel, layoutsRel, metalsmith) {
// Read and process all partials
for (var i = 0; i < files.length; i++) {
var ext = path.extname(files[i]);
var name = files[i].replace(ext, '');
var fileInfo = path.parse(files[i]);
var name = path.join(fileInfo.dir, fileInfo.name);
var partialAbs = path.join(partialsAbs, name);
var partialPath = path.relative(layoutsAbs, partialAbs);

View file

@ -6,6 +6,7 @@ var debug = require('debug')('metalsmith-layouts');
var each = require('async').each;
var extend = require('extend');
var omit = require('lodash.omit');
var path = require('path');
/**
* Helpers
@ -23,7 +24,14 @@ module.exports = plugin;
*
* Options supported by metalsmith-layouts
*/
var settings = ['default', 'directory', 'engine', 'partials', 'pattern'];
var settings = [
'default',
'directory',
'engine',
'partials',
'pattern',
'rename'
];
/**
* Metalsmith plugin to run files through any layout in a layout `dir`.
@ -34,6 +42,7 @@ var settings = ['default', 'directory', 'engine', 'partials', 'pattern'];
* @property {String} engine
* @property {String} partials (optional)
* @property {String} pattern (optional)
* @property {Boolean} rename (optional)
* @return {Function}
*/
function plugin(opts){
@ -63,6 +72,7 @@ function plugin(opts){
var engine = opts.engine;
var partials = opts.partials;
var pattern = opts.pattern;
var rename = opts.rename;
// Move all unrecognised options to params
var params = omit(opts, settings);
@ -112,6 +122,15 @@ function plugin(opts){
var str = metalsmith.path(dir, data.layout || def);
var render = consolidate[engine];
// Rename file if necessary
var fileInfo;
if (rename) {
delete files[file];
fileInfo = path.parse(file);
file = path.join(fileInfo.dir, fileInfo.name + '.html');
debug('renamed file to: %s', file);
}
render(str, clone, function(err, str){
if (err) {
return done(err);
@ -119,6 +138,8 @@ function plugin(opts){
data.contents = new Buffer(str);
debug('converted file: %s', file);
files[file] = data;
done();
});
}

View file

@ -7,7 +7,7 @@
"license": "MIT",
"main": "lib/index.js",
"scripts": {
"test": "make test"
"test": "mocha --reporter spec"
},
"dependencies": {
"async": "^1.3.0",
@ -21,7 +21,6 @@
},
"devDependencies": {
"assert-dir-equal": "^1.0.1",
"eslint": "^1.0.0",
"handlebars": "^4.0.2",
"metalsmith": "^2.0.1",
"mocha": "^2.2.5",

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -149,4 +149,42 @@ describe('metalsmith-layouts', function(){
});
});
it('should not change file extension by default', function(done) {
Metalsmith('test/fixtures/rename-option-default')
.use(layouts({
engine: 'handlebars'
}))
.build(function (err) {
if (err) return done(err);
equal('test/fixtures/rename-option-default/expected', 'test/fixtures/rename-option-default/build');
done();
});
});
it('should change file extension when rename option is set to true', function(done) {
Metalsmith('test/fixtures/rename-option')
.use(layouts({
engine: 'handlebars',
rename: true
}))
.build(function (err) {
if (err) return done(err);
equal('test/fixtures/rename-option/expected', 'test/fixtures/rename-option/build');
done();
});
});
it('should change file extension for nested files when rename option is set to true', function(done) {
Metalsmith('test/fixtures/rename-option-nested')
.use(layouts({
engine: 'handlebars',
rename: true
}))
.build(function (err) {
if (err) return done(err);
equal('test/fixtures/rename-option-nested/expected', 'test/fixtures/rename-option-nested/build');
done();
});
});
});