summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/batch.js1
-rw-r--r--lib/utils/page.js58
2 files changed, 32 insertions, 27 deletions
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");
});