summaryrefslogtreecommitdiffstats
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
parent8d92b74e6152eb81d95b3494e5c864d9a0871ef3 (diff)
downloadgitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.zip
gitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.tar.gz
gitbook-57d5c23eafbce5dc3b5351a4a5a6520434c717fd.tar.bz2
Accept post function from block to post process content
-rw-r--r--lib/template.js31
-rw-r--r--lib/utils/page.js6
2 files changed, 26 insertions, 11 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;
diff --git a/lib/utils/page.js b/lib/utils/page.js
index b072fcf..f58e25a 100644
--- a/lib/utils/page.js
+++ b/lib/utils/page.js
@@ -232,10 +232,10 @@ function convertImages(images, options) {
var downloaded = [];
options.book.log.debug.ln("convert ", images.length, "images to png");
- return _.reduce(images, function(prev, image) {
+ return Q.all(_.map(images, function(image) {
var imgin = path.resolve(options.book.options.output, image.source);
- return prev
+ return Q()
// Write image if need to be download
.then(function() {
@@ -258,7 +258,7 @@ function convertImages(images, options) {
options.book.log.debug("convert image", image.source, "to", image.dest, "...");
return options.book.log.debug.promise(imgUtils.convertSVG(imgin, imgout));
});
- }, Q())
+ }))
.then(function() {
options.book.log.debug.ok(images.length+" images converted with success");
});