mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-29 13:39:11 +00:00
Add blocks from plugins to template engine
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+78
-4
@@ -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
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user