diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-27 17:07:25 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-27 17:07:25 +0100 |
commit | 0e0a23b79484d98695ffa56fa9f885dffef86d36 (patch) | |
tree | 42a6f406b8c2e708f5717c238d94db79bc3dd17c | |
parent | d9329295d60a94a02719869f58abdc3da3535190 (diff) | |
download | gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.zip gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.gz gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.bz2 |
Add blocks from plugins to template engine
-rw-r--r-- | lib/plugin.js | 5 | ||||
-rw-r--r-- | lib/pluginslist.js | 5 | ||||
-rw-r--r-- | lib/template.js | 82 | ||||
-rw-r--r-- | test/plugins/blocks/index.js | 8 |
4 files changed, 92 insertions, 8 deletions
diff --git a/lib/plugin.js b/lib/plugin.js index 79da7d7..2af81c6 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -135,6 +135,11 @@ Plugin.prototype.getFilters = function() { .value(); }; +// Normalize blocks and return them +Plugin.prototype.getBlocks = function() { + return this.infos.blocks || {}; +}; + // Test if it's a valid plugin Plugin.prototype.isValid = function() { var that = this; diff --git a/lib/pluginslist.js b/lib/pluginslist.js index d1d276d..a3d43e2 100644 --- a/lib/pluginslist.js +++ b/lib/pluginslist.js @@ -63,6 +63,11 @@ PluginsList.prototype.load = function(plugin, options) { that.book.template.addFilter(filterName, filterFunc); }); + // Extract blocks + _.each(plugin.getBlocks(), function(block, blockName) { + that.book.template.addBlock(blockName, block); + }); + return Q() .then(function() { return plugin.getResources(options.assetsBase); diff --git a/lib/template.js b/lib/template.js index 4b3fdbe..91cedca 100644 --- a/lib/template.js +++ b/lib/template.js @@ -74,11 +74,85 @@ TemplateEngine.prototype.addFilter = function(filterName, func) { this.env.getFilter(filterName); this.log.warn.ln("conflict in filters, '"+filterName+"' is already set"); return false; - } catch(e) { - this.log.debug.ln("add filter '"+filterName+"'"); - this.env.addFilter(filterName, func, true); - return true; + } catch(e) {} + + this.log.debug.ln("add filter '"+filterName+"'"); + this.env.addFilter(filterName, func, true); + return true; +}; + +// Add a block +TemplateEngine.prototype.addBlock = function(name, block) { + var that = this; + + block = _.defaults(block || {}, { + end: "end"+name, + process: _.identity, + blocks: [] + }); + + var extName = 'Block'+name+'Extension'; + if (this.env.getExtension(extName)) { + this.log.warn.ln("conflict in blocks, '"+extName+"' is already defined"); + return false; } + + this.log.debug.ln("add block '"+name+"'"); + + var Ext = function () { + this.tags = [name]; + + this.parse = function(parser, nodes, lexer) { + console.log("parse", name, block.end); + var bodies = {}; + + var tok = parser.nextToken(); + var args = parser.parseSignature(null, true); + parser.advanceAfterBlockEnd(tok.value); + + // parse the body and possibly the error block, which is optional + /*var body = parser.parseUntilBlocks('error', 'endtruncate'); + var errorBody = null; + + if(parser.skipSymbol('error')) { + parser.skip(lexer.TOKEN_BLOCK_END); + errorBody = parser.parseUntilBlocks('endremote'); + } + + parser.advanceAfterBlockEnd();*/ + + var body = parser.parseUntilBlocks(block.end); + parser.advanceAfterBlockEnd(); + + return new nodes.CallExtensionAsync(this, 'run', args, [body]); + }; + + this.run = function(context) { + var args = Array.prototype.slice.call(arguments, 1); + var callback = args.pop(); + var bodies = {} //args.pop(); + var body = args.pop(); + var kwargs = args.pop() || {}; + + Q() + .then(function() { + return block.process.call({ + ctx: context.ctx, + book: that.book + }, { + body: body(), + args: args, + kwargs: kwargs, + bodies: bodies + }); + }) + .nodeify(callback) + }; + }; + + + // Add the Extension + this.env.addExtension(extName, new Ext()); }; // Render a string from the book diff --git a/test/plugins/blocks/index.js b/test/plugins/blocks/index.js index 6415ec0..91e8b55 100644 --- a/test/plugins/blocks/index.js +++ b/test/plugins/blocks/index.js @@ -1,14 +1,14 @@ module.exports = { blocks: { "test": { - process: function(body, args, kwargs) { - return "test"+body+"test"; + process: function(args) { + return "test"+args.body+"test"; } }, "test2": { end: "endtest2end", - process: function(body, args, kwargs) { - return "test2"+body+"test2"; + process: function(args) { + return "test2"+args.body+"test2"; } } } |