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 /lib/template.js | |
parent | d9329295d60a94a02719869f58abdc3da3535190 (diff) | |
download | gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.zip gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.gz gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.bz2 |
Add blocks from plugins to template engine
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 82 |
1 files changed, 78 insertions, 4 deletions
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 |