diff options
Diffstat (limited to 'lib/template/index.js')
-rw-r--r-- | lib/template/index.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/template/index.js b/lib/template/index.js index a3021e6..b59dd04 100644 --- a/lib/template/index.js +++ b/lib/template/index.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var path = require('path'); var nunjucks = require('nunjucks'); var parsers = require('gitbook-parsers'); +var escapeStringRegexp = require('escape-string-regexp'); var Promise = require('../utils/promise'); var error = require('../utils/error'); @@ -333,7 +334,7 @@ TemplateEngine.prototype.renderString = function(content, context, options) { } // Replace shortcuts - //content = this.applyShortcuts(options.type, content); + content = this.applyShortcuts(options.type, content); return Promise.nfcall(this.env.renderString.bind(this.env), content, context, options) .fail(function(err) { @@ -343,5 +344,26 @@ TemplateEngine.prototype.renderString = function(content, context, options) { }); }; +// Apply a shortcut to a string +TemplateEngine.prototype.applyShortcut = function(content, shortcut) { + var regex = new RegExp( + escapeStringRegexp(shortcut.start) + '([\\s\\S]*?[^\\$])' + escapeStringRegexp(shortcut.end), + 'g' + ); + return content.replace(regex, function(all, match) { + return '{% '+shortcut.tag.start+' %}'+ match + '{% '+shortcut.tag.end+' %}'; + }); +}; + +// Apply all shortcuts to a template +TemplateEngine.prototype.applyShortcuts = function(type, content) { + return _.chain(this.shortcuts) + .filter(function(shortcut) { + return _.contains(shortcut.parsers, type); + }) + .reduce(this.applyShortcut, content) + .value(); +}; + module.exports = TemplateEngine; |