summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-27 17:07:25 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-27 17:07:25 +0100
commit0e0a23b79484d98695ffa56fa9f885dffef86d36 (patch)
tree42a6f406b8c2e708f5717c238d94db79bc3dd17c /lib
parentd9329295d60a94a02719869f58abdc3da3535190 (diff)
downloadgitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.zip
gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.gz
gitbook-0e0a23b79484d98695ffa56fa9f885dffef86d36.tar.bz2
Add blocks from plugins to template engine
Diffstat (limited to 'lib')
-rw-r--r--lib/plugin.js5
-rw-r--r--lib/pluginslist.js5
-rw-r--r--lib/template.js82
3 files changed, 88 insertions, 4 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