diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-27 22:27:59 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-27 22:27:59 +0100 |
commit | d8995200e6802f77de1f0e3699e208695c89b95e (patch) | |
tree | 451b7af168723148e1de81714872b00d47b5fdf8 /lib/template.js | |
parent | bc389ab4a1ea9013a869c0b33162fb67bb0d0708 (diff) | |
download | gitbook-d8995200e6802f77de1f0e3699e208695c89b95e.zip gitbook-d8995200e6802f77de1f0e3699e208695c89b95e.tar.gz gitbook-d8995200e6802f77de1f0e3699e208695c89b95e.tar.bz2 |
Add options "shortcuts" for blocks from plugins
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 35 |
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); }; |