summaryrefslogtreecommitdiffstats
path: root/lib/template.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/template.js')
-rw-r--r--lib/template.js35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib/template.js b/lib/template.js
index ec1b391..070173c 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -4,6 +4,7 @@ var path = require("path");
var nunjucks = require("nunjucks");
var git = require("./utils/git");
+var stringUtils = require("./utils/string");
var fs = require("./utils/fs");
var pkg = require("../package.json");
@@ -65,6 +66,9 @@ var TemplateEngine = function(book) {
}
}
);
+
+ // List of tags shortcuts
+ this.shortcuts = [];
};
@@ -86,6 +90,7 @@ TemplateEngine.prototype.addBlock = function(name, block) {
var that = this;
block = _.defaults(block || {}, {
+ shortcuts: [],
end: "end"+name,
process: _.identity,
blocks: []
@@ -93,7 +98,7 @@ TemplateEngine.prototype.addBlock = function(name, block) {
var extName = 'Block'+name+'Extension';
if (this.env.getExtension(extName)) {
- this.log.warn.ln("conflict in blocks, '"+extName+"' is already defined");
+ this.log.warn.ln("conflict in blocks, '"+name+"' is already defined");
return false;
}
@@ -194,6 +199,31 @@ TemplateEngine.prototype.addBlock = function(name, block) {
// Add the Extension
this.env.addExtension(extName, new Ext());
+
+ // Add shortcuts
+ if (!_.isArray(block.shortcuts)) block.shortcuts = [block.shortcuts];
+ _.each(block.shortcuts, function(shortcut) {
+ this.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"'");
+ this.shortcuts.push({
+ start: shortcut.start,
+ end: shortcut.end,
+ tag: {
+ start: name,
+ end: block.end
+ }
+ });
+ }, this);
+};
+
+// Apply a shortcut to a string
+TemplateEngine.prototype._applyShortcut = function(content, shortcut) {
+ var regex = new RegExp(
+ stringUtils.escapeRegex(shortcut.start) + "\\s*([\\s\\S]*?[^\\$])\\s*" + stringUtils.escapeRegex(shortcut.end),
+ 'g'
+ );
+ return content.replace(regex, function(all, match) {
+ return "{% "+shortcut.tag.start+" %}"+ match + "{% "+shortcut.tag.end+" %}";
+ });
};
// Render a string from the book
@@ -210,6 +240,9 @@ TemplateEngine.prototype.renderString = function(content, context, options) {
options = _.defaults(options || {}, { path: null});
if (options.path) options.path = this.book.resolve(options.path);
+ // Replace shortcuts
+ content = _.reduce(this.shortcuts, this._applyShortcut.bind(this), content);
+
return Q.nfcall(this.env.renderString.bind(this.env), content, context, options);
};