Add option to block to not parse output

This commit is contained in:
Samy Pessé
2015-01-29 14:09:27 +01:00
parent 907ce6dbcd
commit 2bec7c5e10
4 changed files with 98 additions and 42 deletions
+15 -2
View File
@@ -524,9 +524,22 @@ Book.prototype.parsePage = function(filename, options) {
return filetype.parser.page(page.content);
})
// Post process sections
.then(function(_page) {
return _.reduce(_page.sections, function(prev, section) {
return prev.then(function(_sections) {
return that.template.postProcess(section.content || "")
.then(function(content) {
section.content = content;
return _sections.concat([section]);
})
});
}, Q([]));
})
// Prepare html
.then(function(_page) {
return pageUtil.normalize(_page.sections, {
.then(function(_sections) {
return pageUtil.normalize(_sections, {
book: that,
convertImages: options.convertImages,
input: filename,
+1 -30
View File
@@ -103,36 +103,7 @@ Plugin.prototype.getResources = function(base) {
// Normalize filters and return them
Plugin.prototype.getFilters = function() {
var that = this;
return _.chain(this.infos.filters || {})
.map(function(func, key) {
if (!_.isString(key)) {
that.book.log.warn.ln("Invalid filter '"+key+"' in plugin '"+that.name+"'");
return null;
}
return [
key,
function() {
var ctx = {
ctx: this.ctx,
book: that.book
};
var args = Array.prototype.slice.apply(arguments);
var callback = _.last(args);
Q()
.then(function() {
return func.apply(ctx, args.slice(0, -1));
})
.nodeify(callback);
}
];
})
.compact()
.object()
.value();
return this.infos.filters || {};
};
// Normalize blocks and return them
+72 -5
View File
@@ -69,8 +69,56 @@ var TemplateEngine = function(book) {
// List of tags shortcuts
this.shortcuts = [];
// Map of blocks
this.blocks = {};
// Bind methods
_.bindAll(this);
};
// Process a block in a context
TemplateEngine.prototype.processBlock = function(blk) {
if (_.isString(blk)) blk = { body: blk };
blk = _.defaults(blk, {
parse: false
});
blk.id = _.uniqueId("blk");
// Add to global map
this.blocks[blk.id] = blk;
// If don't parse id, return it as a macro
if (!blk.parse) return "%+%"+blk.id+"%+%";
return blk.body;
};
// Replace blocks by body after processing
// This is done to avoid that markdown processer parse the block content
TemplateEngine.prototype.replaceBlocks = function(content) {
var that = this;
return content.replace(/\%\+\%([\s\S]+?)\%\+\%/g, function(match, key) {
var blk = that.blocks[key];
if (!blk || blk.parse) return match;
return blk.body;
});
};
// Bind a function to a context
TemplateEngine.prototype.bindContext = function(func) {
var that = this;
return function() {
var ctx = {
ctx: this.ctx,
book: that.book
};
return func.apply(ctx, arguments);
};
};
// Add filter
TemplateEngine.prototype.addFilter = function(filterName, func) {
@@ -81,7 +129,17 @@ TemplateEngine.prototype.addFilter = function(filterName, func) {
} catch(e) {}
this.log.debug.ln("add filter '"+filterName+"'");
this.env.addFilter(filterName, func, true);
this.env.addFilter(filterName, this.bindContext(function() {
var ctx = this;
var args = Array.prototype.slice.apply(arguments);
var callback = _.last(args);
Q()
.then(function() {
return func.apply(ctx, args.slice(0, -1));
})
.nodeify(callback);
}), true);
return true;
};
@@ -180,18 +238,21 @@ TemplateEngine.prototype.addBlock = function(name, block) {
var body = args.pop();
var kwargs = args.pop() || {};
var func = that.bindContext(block.process);
Q()
.then(function() {
return block.process.call({
ctx: context.ctx,
book: that.book
}, {
return func.call(context, {
body: body(),
args: args,
kwargs: kwargs,
blocks: _blocks
});
})
// process the block returned
.then(that.processBlock)
.nodeify(callback)
};
};
@@ -284,4 +345,10 @@ TemplateEngine.prototype.renderPage = function(page) {
});
};
// Post process content
TemplateEngine.prototype.postProcess = function(content) {
return Q(content)
.then(this.replaceBlocks);
};
module.exports = TemplateEngine;
+10 -5
View File
@@ -107,6 +107,11 @@ describe('Plugins', function () {
var plugin = new Plugin(books[0], "blocks");
plugin.load("./blocks", PLUGINS_ROOT);
var testTpl = function(str, args, options) {
return books[0].template.renderString(str, args, options)
.then(books[0].template.postProcess)
};
before(function(done) {
qdone(books[0].plugins.load(plugin), done);
});
@@ -117,7 +122,7 @@ describe('Plugins', function () {
it('should correctly extend template blocks', function(done) {
qdone(
books[0].template.renderString('{% test %}hello{% endtest %}')
testTpl('{% test %}hello{% endtest %}')
.then(function(content) {
assert.equal(content, "testhellotest");
}),
@@ -127,7 +132,7 @@ describe('Plugins', function () {
it('should correctly accept shortcuts', function(done) {
qdone(
books[0].template.renderString('$$hello$$', {}, {
testTpl('$$hello$$', {}, {
type: "markdown"
})
.then(function(content) {
@@ -139,7 +144,7 @@ describe('Plugins', function () {
it('should correctly extend template blocks with defined end', function(done) {
qdone(
books[0].template.renderString('{% test2 %}hello{% endtest2end %}')
testTpl('{% test2 %}hello{% endtest2end %}')
.then(function(content) {
assert.equal(content, "test2hellotest2");
}),
@@ -149,7 +154,7 @@ describe('Plugins', function () {
it('should correctly extend template blocks with sub-blocks', function(done) {
qdone(
books[0].template.renderString('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}')
testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}')
.then(function(content) {
assert.equal(content, "hello;world");
}),
@@ -159,7 +164,7 @@ describe('Plugins', function () {
it('should correctly extend template blocks with different sub-blocks', function(done) {
qdone(
books[0].template.renderString('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}')
testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}')
.then(function(content) {
assert.equal(content, "hello;the;world");
}),