Check parser used before applying template shortcuts

This commit is contained in:
Samy Pessé
2015-01-27 22:34:48 +01:00
parent d8995200e6
commit d1b2950993
2 changed files with 14 additions and 6 deletions
+11 -5
View File
@@ -203,8 +203,9 @@ TemplateEngine.prototype.addBlock = function(name, block) {
// 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.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"' for parsers ", shortcut.parsers);
this.shortcuts.push({
parsers: shortcut.parsers,
start: shortcut.start,
end: shortcut.end,
tag: {
@@ -216,7 +217,8 @@ TemplateEngine.prototype.addBlock = function(name, block) {
};
// Apply a shortcut to a string
TemplateEngine.prototype._applyShortcut = function(content, shortcut) {
TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) {
if (!_.contains(shortcut.parsers, parser)) return content;
var regex = new RegExp(
stringUtils.escapeRegex(shortcut.start) + "\\s*([\\s\\S]*?[^\\$])\\s*" + stringUtils.escapeRegex(shortcut.end),
'g'
@@ -237,11 +239,14 @@ TemplateEngine.prototype.renderString = function(content, context, options) {
version: pkg.version
}
});
options = _.defaults(options || {}, { path: null});
options = _.defaults(options || {}, {
path: null,
type: null
});
if (options.path) options.path = this.book.resolve(options.path);
// Replace shortcuts
content = _.reduce(this.shortcuts, this._applyShortcut.bind(this), content);
content = _.reduce(this.shortcuts, _.partial(this._applyShortcut.bind(this), options.type), content);
return Q.nfcall(this.env.renderString.bind(this.env), content, context, options);
};
@@ -273,7 +278,8 @@ TemplateEngine.prototype.renderPage = function(page) {
};
return that.renderString(page.content, context, {
path: page.path
path: page.path,
type: page.type
});
});
};
+3 -1
View File
@@ -100,7 +100,9 @@ describe('Plugins', function () {
it('should correctly accept shortcuts', function(done) {
qdone(
books[0].template.renderString('$$hello$$')
books[0].template.renderString('$$hello$$', {}, {
type: "markdown"
})
.then(function(content) {
assert.equal(content, "testhellotest");
}),