summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-02-13 08:09:28 +0100
committerSamy Pessé <samypesse@gmail.com>2015-02-13 08:09:28 +0100
commitb0fd2a67ea586e59f4c8370acc9c093e44d7f3bd (patch)
treea790813b647c8ae93324232e5a1105d7cde7e891 /lib
parent7c8386986919f611dcdaaf917bf0c24fabcfe3a4 (diff)
downloadgitbook-b0fd2a67ea586e59f4c8370acc9c093e44d7f3bd.zip
gitbook-b0fd2a67ea586e59f4c8370acc9c093e44d7f3bd.tar.gz
gitbook-b0fd2a67ea586e59f4c8370acc9c093e44d7f3bd.tar.bz2
Fix test with post in blocks
Diffstat (limited to 'lib')
-rw-r--r--lib/template.js6
-rw-r--r--lib/utils/batch.js1
-rw-r--r--lib/utils/page.js58
3 files changed, 35 insertions, 30 deletions
diff --git a/lib/template.js b/lib/template.js
index 08e1877..0565824 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -93,7 +93,7 @@ TemplateEngine.prototype.processBlock = function(blk) {
});
blk.id = _.uniqueId("blk");
- var toAdd = blk.parse || (blk.post != undefined);
+ var toAdd = (!blk.parse) || (blk.post != undefined);
// Add to global map
if (toAdd) this.blocks[blk.id] = blk;
@@ -368,7 +368,7 @@ TemplateEngine.prototype.postProcess = function(content) {
return Q(content)
.then(that.replaceBlocks)
- .then(function(content) {
+ .then(function(_content) {
return batch.execEach(that.blocks, {
max: 20,
fn: function(blk, blkId) {
@@ -382,7 +382,7 @@ TemplateEngine.prototype.postProcess = function(content) {
});
}
})
- .thenResolve(content);
+ .thenResolve(_content);
});
};
diff --git a/lib/utils/batch.js b/lib/utils/batch.js
index 5b92be5..bd3b80f 100644
--- a/lib/utils/batch.js
+++ b/lib/utils/batch.js
@@ -3,6 +3,7 @@ var _ = require("lodash");
// Execute a method for all element
function execEach(items, options) {
+ if (_.size(items) == 0) return Q();
var concurrents = 0, d = Q.defer(), pending = [];
options = _.defaults(options || {}, {
diff --git a/lib/utils/page.js b/lib/utils/page.js
index f58e25a..0714958 100644
--- a/lib/utils/page.js
+++ b/lib/utils/page.js
@@ -9,6 +9,7 @@ var crc = require("crc");
var links = require('./links');
var imgUtils = require('./images');
var fs = require('./fs');
+var batch = require('./batch');
// Render a cheerio dom as html
var renderDom = function($, dom, options) {
@@ -232,33 +233,36 @@ function convertImages(images, options) {
var downloaded = [];
options.book.log.debug.ln("convert ", images.length, "images to png");
- return Q.all(_.map(images, function(image) {
- var imgin = path.resolve(options.book.options.output, image.source);
-
- return Q()
-
- // Write image if need to be download
- .then(function() {
- if (!image.origin && !_.contains(downloaded, image.origin)) return;
- options.book.log.debug("download image", image.origin, "...");
- downloaded.push(image.origin);
- return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin)));
- })
-
- // Write svg if content
- .then(function() {
- if (!image.content) return;
- return fs.writeFile(imgin, image.content);
- })
-
- // Convert
- .then(function() {
- if (!image.dest) return;
- var imgout = path.resolve(options.book.options.output, image.dest);
- options.book.log.debug("convert image", image.source, "to", image.dest, "...");
- return options.book.log.debug.promise(imgUtils.convertSVG(imgin, imgout));
- });
- }))
+ return batch.execEach(images, {
+ max: 100,
+ fn: function(image) {
+ var imgin = path.resolve(options.book.options.output, image.source);
+
+ return Q()
+
+ // Write image if need to be download
+ .then(function() {
+ if (!image.origin && !_.contains(downloaded, image.origin)) return;
+ options.book.log.debug("download image", image.origin, "...");
+ downloaded.push(image.origin);
+ return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin)));
+ })
+
+ // Write svg if content
+ .then(function() {
+ if (!image.content) return;
+ return fs.writeFile(imgin, image.content);
+ })
+
+ // Convert
+ .then(function() {
+ if (!image.dest) return;
+ var imgout = path.resolve(options.book.options.output, image.dest);
+ options.book.log.debug("convert image", image.source, "to", image.dest, "...");
+ return options.book.log.debug.promise(imgUtils.convertSVG(imgin, imgout));
+ });
+ }
+ })
.then(function() {
options.book.log.debug.ok(images.length+" images converted with success");
});