summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-27 21:39:41 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-27 21:39:41 +0100
commitcf12d090928907bb82c9f1796b642cbef9b42640 (patch)
treeb229ff036508d52cbc8c9b50614c99f8ee8d0c65
parent8e18adbe4cd64b8d6baf63627cba4109774fc7e5 (diff)
downloadgitbook-cf12d090928907bb82c9f1796b642cbef9b42640.zip
gitbook-cf12d090928907bb82c9f1796b642cbef9b42640.tar.gz
gitbook-cf12d090928907bb82c9f1796b642cbef9b42640.tar.bz2
Complete parsing of blocks from plugin extensions
-rw-r--r--lib/template.js42
-rw-r--r--test/plugins.js4
-rw-r--r--test/plugins/blocks/index.js12
3 files changed, 39 insertions, 19 deletions
diff --git a/lib/template.js b/lib/template.js
index e57ef1b..ac7bc3e 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -113,8 +113,8 @@ TemplateEngine.prototype.addBlock = function(name, block) {
var args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);
- console.log("start parsing", allBlocks);
while (1) {
+ // Read body
var currentBody = parser.parseUntilBlocks.apply(parser, allBlocks);
// Handle body with previous block name and args
@@ -128,29 +128,49 @@ TemplateEngine.prototype.addBlock = function(name, block) {
body = currentBody;
}
- var tok = parser.peekToken();
- lastBlockName = tok.value;
+ // Read new block
+ lastBlockName = parser.peekToken().value;
if (lastBlockName == block.end) {
- console.log("finish!");
break;
}
+ // Parse signature and move to the end of the block
lastBlockArgs = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(lastBlockName);
-
- console.log(" -> keep going");
}
-
- console.log(body, subbodies);
parser.advanceAfterBlockEnd();
- return new nodes.CallExtensionAsync(this, 'run', args, [body]);
+ var bodies = [body];
+ _.each(block.blocks, function(blockName) {
+ subbodies[blockName] = subbodies[blockName] || [];
+ if (subbodies[blockName].length == 0) {
+ subbodies[blockName].push({
+ args: new nodes.NodeList(),
+ body: new nodes.NodeList()
+ });
+ }
+
+ bodies.push(subbodies[blockName][0].body);
+ });
+
+ return new nodes.CallExtensionAsync(this, 'run', args, bodies);
};
this.run = function(context) {
var args = Array.prototype.slice.call(arguments, 1);
var callback = args.pop();
- var bodies = {} //args.pop();
+
+ // Extract blocks body
+ var _blocks = _.chain(block.blocks)
+ .reverse()
+ .map(function(blockName){
+ return {
+ name: blockName,
+ body: args.pop()()
+ };
+ })
+ .value();
+
var body = args.pop();
var kwargs = args.pop() || {};
@@ -163,7 +183,7 @@ TemplateEngine.prototype.addBlock = function(name, block) {
body: body(),
args: args,
kwargs: kwargs,
- bodies: bodies
+ blocks: _blocks
});
})
.nodeify(callback)
diff --git a/test/plugins.js b/test/plugins.js
index 560a779..0f826d2 100644
--- a/test/plugins.js
+++ b/test/plugins.js
@@ -110,9 +110,9 @@ describe('Plugins', function () {
it('should correctly extend template blocks with sub-blocks', function(done) {
qdone(
- books[0].template.renderString('{% test3join %}hello{% also %}the{% also %}world{% endtest3join %}')
+ books[0].template.renderString('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}')
.then(function(content) {
- assert.equal(content, "hello the world");
+ assert.equal(content, "hello;world");
}),
done
);
diff --git a/test/plugins/blocks/index.js b/test/plugins/blocks/index.js
index 0f5fb87..f2c588a 100644
--- a/test/plugins/blocks/index.js
+++ b/test/plugins/blocks/index.js
@@ -1,22 +1,22 @@
module.exports = {
blocks: {
"test": {
- process: function(args) {
- return "test"+args.body+"test";
+ process: function(blk) {
+ return "test"+blk.body+"test";
}
},
"test2": {
end: "endtest2end",
- process: function(args) {
- return "test2"+args.body+"test2";
+ process: function(blk) {
+ return "test2"+blk.body+"test2";
}
},
"test3join": {
blocks: [
"also"
],
- process: function(args) {
- return "test";
+ process: function(blk) {
+ return [blk.body, blk.blocks[0].body].join(blk.kwargs.separator);
}
}
}