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

Added substitution 'template' to allow importing of markup from external .html files #62

Closed
wants to merge 4 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
1 change: 1 addition & 0 deletions assets/test-fixtures/substitition-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---TEMPLATE---
Binary file modified build/fonts/SG-icons.eot
Binary file not shown.
Binary file modified build/fonts/SG-icons.ttf
Binary file not shown.
Binary file modified build/fonts/SG-icons.woff
Binary file not shown.
16 changes: 8 additions & 8 deletions build/scripts/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/styles/app.css

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions docs/src/views/substitutions-flags.pug
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ block content
div.SG-config__label Example
div.SG-config__value @{image:400:300}

div.SG-config
div.SG-config__row
div.SG-config__label Substitution
div.SG-config__value @{template:path}
div.SG-config__row
div.SG-config__label Description
div.SG-config__value Include the whole content of file. 'path' is either an absolute path, a path relative to where nucleus is being run, or a path appended to the config variable templatePath
div.SG-config__row
div.SG-config__label Example
div.SG-config__value @{template: src/templates/navigation.html}

code.code.language-nucleus(data-d-code-preview).
/**
* A navigation element for the menu bar.
Expand Down
29 changes: 29 additions & 0 deletions src/Substitute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* With contributions from: -
* - Ryan Potter (www.ryanpotter.co.nz)
* - Fritz Stelluto (@gotofritz)
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -24,6 +25,20 @@ var Substitute = {
Substitute.injectConfig = function ( config ) {
this.staticLipsum = config.staticLipsum;
this.placeholderService = config.placeholderService;
if (config.templatePath) {
// absolute path
if (config.templatePath[0] === '/') {
this.templatePath = config.templatePath;
}
// relative path
else {
this.templatePath = require('path').join(process.cwd(), config.templatePath);
}
}
else {
// no path
this.templatePath = process.cwd();
}
return this;
};

Expand Down Expand Up @@ -118,4 +133,18 @@ Substitute.methods.image = function (width, height) {
}
};

Substitute.methods.template = function ( selector = '') {
selector = selector.trim();
var filePath = require('path').join(this.templatePath, selector);
var fs = require('fs');
try {
fs.statSync(filePath);
return fs.readFileSync(filePath).toString();
}
catch (e) {
Verbose.warn('unknown_template', [filePath, selector, this.templatePath]);
return '';
}
}

module.exports = Substitute;
9 changes: 9 additions & 0 deletions src/messages/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (C) 2016 Michael Seibt
*
* With contributions from: -
* - Fritz Stelluto (@gotofritz)
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -69,4 +70,12 @@ module.exports = {
'Make sure '+selector+' really exists and it is annotated (which makes it \'visible\' for Nucleus.'
};
},
// Whenever we try to substitute with unknown markup template.
'unknown_template': function (filePath, selector, templatePath) {
return {
'title': 'Can\'t find template ' + filePath + ' for markup substitution',
'text': 'You try to substitute with @{template:' + selector + '}, but path cannot be found. ' +
'Make sure the file really exists and check templatePath '+templatePath+' in the config'
};
},
};
25 changes: 25 additions & 0 deletions test/Substitution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,29 @@ describe('Substitution', function () {
assert.ok(subs.split(' ').length === 3);

});


/********************************************************/

it('includes templates', function () {
var markup = 'Test @{template: assets/test-fixtures/substitition-template.html}';
Substitute.injectConfig({});
var subs = Substitute.substitute(markup);
assert.ok(subs.indexOf('{template') === -1);
assert.ok(/---TEMPLATE---$/.test(subs));

markup = 'Test @{template: substitition-template.html}';
Substitute.injectConfig({
templatePath: 'assets/test-fixtures'
});
var subs = Substitute.substitute(markup);
assert.ok(subs.indexOf('{template') === -1);
assert.ok(/---TEMPLATE---$/.test(subs));

markup = 'Test @{template: random-file.txt}';
Substitute.injectConfig({});
var subs = Substitute.substitute(markup);
assert.ok(subs.indexOf('{template') === -1);
assert.ok(!/---TEMPLATE---$/.test(subs));
});
});