diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-29 14:09:27 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-29 14:09:27 +0100 |
commit | 2bec7c5e10274b260faea3e007b056c19760cc6e (patch) | |
tree | 7c16616115430b151093e634afe3e66b8341ffe1 | |
parent | 907ce6dbcdbebd59f4ce5a43073b0dd68056d6c2 (diff) | |
download | gitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.zip gitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.tar.gz gitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.tar.bz2 |
Add option to block to not parse output
-rw-r--r-- | lib/book.js | 17 | ||||
-rw-r--r-- | lib/plugin.js | 31 | ||||
-rw-r--r-- | lib/template.js | 77 | ||||
-rw-r--r-- | test/plugins.js | 15 |
4 files changed, 98 insertions, 42 deletions
diff --git a/lib/book.js b/lib/book.js index 24ab505..5099291 100644 --- a/lib/book.js +++ b/lib/book.js @@ -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, diff --git a/lib/plugin.js b/lib/plugin.js index 2af81c6..995385a 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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 diff --git a/lib/template.js b/lib/template.js index a34f79b..433104c 100644 --- a/lib/template.js +++ b/lib/template.js @@ -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; diff --git a/test/plugins.js b/test/plugins.js index f72f719..2351537 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -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"); }), |