Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support template inheritance from content files #27

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var each = require('async').each;
var extend = require('extend');
var match = require('multimatch');
var omit = require('lodash.omit');
var path = require('path');

var util = require('util');

/**
* Expose `plugin`.
Expand All @@ -16,7 +19,7 @@ module.exports = plugin;
* Settings.
*/

var settings = ['engine', 'directory', 'pattern', 'inPlace', 'default'];
var settings = ['engine', 'directory', 'pattern', 'inPlace', 'default', 'useExtends', 'defaultExtends', 'defaultBlock'];

/**
* Metalsmith plugin to run files through any template in a template `dir`.
Expand All @@ -27,6 +30,11 @@ var settings = ['engine', 'directory', 'pattern', 'inPlace', 'default'];
* @property {String} engine
* @property {String} inPlace (optional)
* @property {String} pattern (optional)
* @property {Boolean} useExtends (optional)
* @property {String} defaultExtends (optional)
* @property {String} defaultBlock (optional)
* @property {String} extendsPattern (optional)
* @property {String} blockPattern (optional)
* @return {Function}
*/

Expand All @@ -40,6 +48,29 @@ function plugin(opts){
var pattern = opts.pattern;
var inPlace = opts.inPlace;
var def = opts.default;

var useExtends = opts.useExtends;
var defaultExtends = opts.defaultExtends;
var defaultBlock = opts.defaultBlock;
var extendsPattern = opts.extendsPattern;
var blockPattern = opts.blockPattern;

if ((defaultExtends) && !useExtends) {
throw new Error('"useExtends" option required to use defaultExtends or defaultBlock');
}

if (useExtends && !extendsPattern) {
throw new Error('"extendsPattern" required with "useExtends"');
}

if (defaultBlock && !blockPattern) {
throw new Error('"blockPattern" required with "useExtends" and "defaultBlock"');
}

if (blockPattern && !(Array.isArray(blockPattern) && blockPattern.length >= 1)) {
throw new Error('"blockPattern" must be an array with at least one element (block opening pattern).');
}

var params = omit(opts, settings);

return function(files, metalsmith, done){
Expand All @@ -48,8 +79,10 @@ function plugin(opts){
function check(file){
var data = files[file];
var tmpl = data.template || def;
var ext = useExtends || data.extends ? data.extends || defaultExtends : false;

if (pattern && !match(file, pattern)[0]) return false;
if (!inPlace && !tmpl) return false;
if (!inPlace && !tmpl && !ext) return false;
return true;
}

Expand All @@ -70,9 +103,32 @@ function plugin(opts){
var str;
var render;

if (inPlace) {
if (useExtends || clone.extends) {
if (!extendsPattern) throw new Error('"extendsPattern" option required with extends');

clone.extends = data.extends || defaultExtends;
//fake a filename, make it in templates directory, so rest of templates
//in inheritance chain can be resolves
clone.filename = path.join(metalsmith.directory(), dir, file + "-" + clone.extends);

str = util.format(extendsPattern, clone.extends);

if (defaultBlock) {
str += util.format(blockPattern[0], defaultBlock);
}

str += clone.contents;

if (defaultBlock && blockPattern.length > 1) {
str += util.format(blockPattern[1], defaultBlock);
}

render = consolidate[engine].render;

} else if (inPlace) {
str = clone.contents;
render = consolidate[engine].render;

} else {
str = metalsmith.path(dir, data.template || def);
render = consolidate[engine];
Expand Down
6 changes: 0 additions & 6 deletions test/fixtures/basic/build/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/default/build/default.md

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/default/build/other.md

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/directory/build/index.html

This file was deleted.

12 changes: 12 additions & 0 deletions test/fixtures/extends-def_block/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>TITLE</title>
<style>

</style>
</head>
<body>
CONTENT here

</body>
</html>
12 changes: 12 additions & 0 deletions test/fixtures/extends-def_block/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>TITLE</title>
<style>

</style>
</head>
<body>
CONTENT here

</body>
</html>
6 changes: 6 additions & 0 deletions test/fixtures/extends-def_block/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: TITLE
content: CONTENT
extends: base.html
---
{{ content }} here
11 changes: 11 additions & 0 deletions test/fixtures/extends-def_block/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>{{ title }}</title>
<style>
{% block css %}{% endblock %}
</style>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
15 changes: 15 additions & 0 deletions test/fixtures/extends-def_extends/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>TITLE</title>
<style>

/* CSS here */

</style>
</head>
<body>

CONTENT here

</body>
</html>
15 changes: 15 additions & 0 deletions test/fixtures/extends-def_extends/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>TITLE</title>
<style>

/* CSS here */

</style>
</head>
<body>

CONTENT here

</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/extends-def_extends/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: TITLE
content: CONTENT
---
{% block css %}
/* CSS here */
{% endblock %}

{% block content %}
{{ content }} here
{% endblock %}
11 changes: 11 additions & 0 deletions test/fixtures/extends-def_extends/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>{{ title }}</title>
<style>
{% block css %}{% endblock %}
</style>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
15 changes: 15 additions & 0 deletions test/fixtures/extends/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>TITLE</title>
<style>

/* CSS here */

</style>
</head>
<body>

CONTENT here

</body>
</html>
15 changes: 15 additions & 0 deletions test/fixtures/extends/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>TITLE</title>
<style>

/* CSS here */

</style>
</head>
<body>

CONTENT here

</body>
</html>
12 changes: 12 additions & 0 deletions test/fixtures/extends/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: TITLE
content: CONTENT
extends: base.html
---
{% block css %}
/* CSS here */
{% endblock %}

{% block content %}
{{ content }} here
{% endblock %}
11 changes: 11 additions & 0 deletions test/fixtures/extends/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>{{ title }}</title>
<style>
{% block css %}{% endblock %}
</style>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
9 changes: 0 additions & 9 deletions test/fixtures/metadata/build/one.html

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixtures/metadata/build/two.html

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/pattern/build/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/pattern/build/index.md

This file was deleted.

47 changes: 46 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,49 @@ describe('metalsmith-templates', function(){
done();
});
});
});

it('should extend templates', function(done){
Metalsmith('test/fixtures/extends')
.use(templates({
engine: 'swig',
extendsPattern: '{% extends "%s" %}'
}))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/extends/expected', 'test/fixtures/extends/build');
done();
});
});

it('should extend using default block', function(done){
Metalsmith('test/fixtures/extends-def_block')
.use(templates({
engine: 'swig',
defaultBlock: 'content',
extendsPattern: '{% extends "%s" %}',
blockPattern: [ '{% block %s %}', '{% endblock %}' ]
}))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/extends-def_block/expected', 'test/fixtures/extends-def_block/build');
done();
});

});

it('should extend using default extends', function(done){
Metalsmith('test/fixtures/extends-def_extends')
.use(templates({
engine: 'swig',
useExtends: true,
defaultExtends: 'base.html',
extendsPattern: '{% extends "%s" %}'
}))
.build(function(err){
if (err) return done(err);
equal('test/fixtures/extends-def_extends/expected', 'test/fixtures/extends-def_extends/build');
done();
});
});

});