summaryrefslogtreecommitdiffstats
path: root/lib/template.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-02-12 23:36:22 +0100
committerSamy Pessé <samypesse@gmail.com>2015-02-12 23:36:22 +0100
commit57d5c23eafbce5dc3b5351a4a5a6520434c717fd (patch)
tree76f588a497d475c13abcac015ab213d4548f6ac2 /lib/template.js
parent8d92b74e6152eb81d95b3494e5c864d9a0871ef3 (diff)
downloadgitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.zip
gitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.tar.gz
gitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.tar.bz2
Accept post function from block to post process content
Diffstat (limited to 'lib/template.js')
-rw-r--r--lib/template.js31
1 files changed, 23 insertions, 8 deletions
diff --git a/lib/template.js b/lib/template.js
index 29ce0a9..91b4850 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -87,18 +87,21 @@ TemplateEngine.prototype.processBlock = function(blk) {
if (_.isString(blk)) blk = { body: blk };
blk = _.defaults(blk, {
- parse: false
+ parse: false,
+ post: undefined
});
blk.id = _.uniqueId("blk");
+ var toAdd = blk.parse || (blk.post != undefined);
+
+ // Add to global map
+ if (toAdd) this.blocks[blk.id] = blk;
+
//Parsable block, just return it
if (blk.parse) {
return blk.body;
}
- // Add to global map
- this.blocks[blk.id] = blk;
-
// Return it as a macro
return "%+%"+blk.id+"%+%";
};
@@ -114,9 +117,6 @@ TemplateEngine.prototype.replaceBlocks = function(content) {
var body = blk.body;
- // Remove it from map
- delete that.blocks[key];
-
return body;
});
};
@@ -363,8 +363,23 @@ TemplateEngine.prototype.renderPage = function(page) {
// Post process content
TemplateEngine.prototype.postProcess = function(content) {
+ var that = this;
+
return Q(content)
- .then(this.replaceBlocks);
+ .then(that.replaceBlocks)
+ .then(function(content) {
+ return Q.all(_.map(that.blocks, function(blk, blkId) {
+ return Q()
+ .then(function() {
+ if (!blk.post) return Q();
+ return blk.post();
+ })
+ .then(function() {
+ delete that.blocks[blkId];
+ });
+ }))
+ .thenResolve(content);
+ });
};
module.exports = TemplateEngine;