diff options
author | Samy Pesse <samypesse@gmail.com> | 2015-09-14 11:21:16 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2015-09-14 11:21:16 +0200 |
commit | 35c4179ca12a0287279ced0535f11237cdf606ec (patch) | |
tree | 3c33389aee37d515b2fed396f57a7cefb2eb8cad | |
parent | 601ce3add380392fe9c1d598e01f05034ff058c3 (diff) | |
download | gitbook-35c4179ca12a0287279ced0535f11237cdf606ec.zip gitbook-35c4179ca12a0287279ced0535f11237cdf606ec.tar.gz gitbook-35c4179ca12a0287279ced0535f11237cdf606ec.tar.bz2 |
Use block code for highlight pre/code
Cleanup correctly predefined blocks
-rw-r--r-- | lib/pluginslist.js | 2 | ||||
-rw-r--r-- | lib/template.js | 78 | ||||
-rw-r--r-- | lib/utils/page.js | 9 | ||||
-rw-r--r-- | package.json | 2 |
4 files changed, 73 insertions, 18 deletions
diff --git a/lib/pluginslist.js b/lib/pluginslist.js index d15c464..37dbd41 100644 --- a/lib/pluginslist.js +++ b/lib/pluginslist.js @@ -79,7 +79,7 @@ PluginsList.prototype.load = function(plugin, options) { } // Extract filters - that.book.template.addFilters(plugin.getFilters()); + that.book.template.addFilters(plugin.getFilters()); // Extract blocks that.book.template.addBlocks(plugin.getBlocks()); diff --git a/lib/template.js b/lib/template.js index 8ea8038..b27be89 100644 --- a/lib/template.js +++ b/lib/template.js @@ -10,6 +10,11 @@ var batch = require("./utils/batch"); var pkg = require("../package.json"); var defaultBlocks = require("./blocks"); +// Normalize result from a block +function normBlockResult(blk) { + if (_.isString(blk)) blk = { body: blk }; + return blk; +} // The loader should handle relative and git url var BookLoader = nunjucks.Loader.extend({ @@ -85,10 +90,8 @@ var TemplateEngine = function(book) { this.addBlocks(defaultBlocks); }; -// Process a block in a context +// Process the result of block in a context TemplateEngine.prototype.processBlock = function(blk) { - if (_.isString(blk)) blk = { body: blk }; - blk = _.defaults(blk, { parse: false, post: undefined @@ -169,9 +172,32 @@ TemplateEngine.prototype.addFilters = function(filters) { }, this); }; +// Return nunjucks extension name of a block +TemplateEngine.prototype.blockExtName = function(name) { + return 'Block'+name+'Extension'; +}; + +// Test if a block is defined +TemplateEngine.prototype.hasBlock = function(name) { + return this.env.hasExtension(this.blockExtName(name)); +}; + +// Remove a block +TemplateEngine.prototype.removeBlock = function(name) { + if (!this.hasBlock(name)) return; + + // Remove nunjucks extension + this.env.removeExtension(this.blockExtName(name)); + + // Cleanup shortcuts + this.shortcuts = _.reject(this.shortcuts, { + block: name + }); +}; + // Add a block TemplateEngine.prototype.addBlock = function(name, block) { - var that = this; + var that = this, Ext, extName; if (_.isFunction(block)) block = { process: block }; @@ -182,12 +208,15 @@ TemplateEngine.prototype.addBlock = function(name, block) { blocks: [] }); - var extName = 'Block'+name+'Extension'; - if (this.env.getExtension(extName)) { + var extName = this.blockExtName(name); + + if (this.hasBlock(name) && !defaultBlocks[name]) { this.log.warn.ln("conflict in blocks, '"+name+"' is already defined"); - return false; } + // Cleanup previous block + this.removeBlock(name); + this.log.debug.ln("add block '"+name+"'"); this.blocks[name] = block; @@ -272,11 +301,9 @@ TemplateEngine.prototype.addBlock = function(name, block) { }; }); - var func = that.bindContext(block.process); - Q() .then(function() { - return func.call(context, { + return that.applyBlock(name, { body: body(), args: args, kwargs: kwargs, @@ -290,7 +317,6 @@ TemplateEngine.prototype.addBlock = function(name, block) { }; }; - // Add the Extension this.env.addExtension(extName, new Ext()); @@ -299,6 +325,7 @@ TemplateEngine.prototype.addBlock = function(name, block) { _.each(block.shortcuts, function(shortcut) { this.log.debug.ln("add template shortcut from '"+shortcut.start+"' to block '"+name+"' for parsers ", shortcut.parsers); this.shortcuts.push({ + block: name, parsers: shortcut.parsers, start: shortcut.start, end: shortcut.end, @@ -312,11 +339,40 @@ TemplateEngine.prototype.addBlock = function(name, block) { // Add multiple blocks TemplateEngine.prototype.addBlocks = function(blocks) { + console.log('add blocks', blocks); _.each(blocks, function(block, name) { this.addBlock(name, block); }, this); }; +// Apply a block to some content +// This method result depends on the type of block (async or sync) +TemplateEngine.prototype.applyBlock = function(name, blk) { + var func, block, func, r; + + block = this.blocks[name]; + console.log('applyBlock', name, block); + if (!block) throw new Error('Block not found "'+name+'"'); + if (_.isString(blk)) { + blk = { + body: blk + }; + } + + blk = _.defaults(blk, { + args: [], + kwargs: {}, + blocks: [] + }); + + // Bind and call block processor + func = this.bindContext(block.process); + r = func.call(context, blk); + + if (Q.isPromise(r)) return r.then(normBlockResult); + else return normBlockResult(r); +}; + // Apply a shortcut to a string TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) { if (!_.contains(shortcut.parsers, parser)) return content; diff --git a/lib/utils/page.js b/lib/utils/page.js index 2400480..c4af715 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -223,7 +223,7 @@ function normalizeHtml(src, options) { // Highlight code blocks $("code").each(function() { - // Extract language + // Normalize language var lang = _.chain( ($(this).attr("class") || "").split(" ") ) @@ -240,11 +240,10 @@ function normalizeHtml(src, options) { .first() .value(); - if (lang) { - var html = $(this).text(); + var source = $(this).text(); + var html = options.book.template.applyBlock('code', source).body; - $(this).html(html); - } + $(this).html(html); }); // Replace glossary terms diff --git a/package.json b/package.json index 8d34ea1..5480824 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "fs-extra": "0.16.5", "fstream-ignore": "1.0.2", "gitbook-parsers": "0.8.2", - "nunjucks": "mozilla/nunjucks#b4351ec0752b102ebd02c5e2cbc2898a6e0e4839", + "nunjucks": "mozilla/nunjucks#146a63571f1910d57cc391dfe64f3789ba1f1fd1", "nunjucks-autoescape": "1.0.0", "nunjucks-filter": "1.0.0", "i18n": "0.5.0", |