summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/batch.js52
-rw-r--r--lib/utils/i18n.js3
-rw-r--r--lib/utils/images.js6
-rw-r--r--lib/utils/page.js63
4 files changed, 96 insertions, 28 deletions
diff --git a/lib/utils/batch.js b/lib/utils/batch.js
new file mode 100644
index 0000000..bd3b80f
--- /dev/null
+++ b/lib/utils/batch.js
@@ -0,0 +1,52 @@
+var Q = require("q");
+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 || {}, {
+ max: 100,
+ fn: function(item) {}
+ });
+
+
+ function startItem(item, i) {
+ if (concurrents >= options.max) {
+ pending.push([item, i]);
+ return;
+ }
+
+ concurrents++;
+ Q()
+ .then(function() {
+ return options.fn(item, i);
+ })
+ .then(function() {
+ concurrents--;
+
+ // Next pending
+ var next = pending.shift();
+
+ if (concurrents == 0 && !next) {
+ d.resolve();
+ } else if (next) {
+ startItem.apply(null, next);
+ }
+ })
+ .fail(function(err) {
+ pending = [];
+ d.reject(err);
+ })
+ }
+
+ _.each(items, startItem);
+
+ return d.promise;
+}
+
+module.exports = {
+ execEach: execEach
+};
+
diff --git a/lib/utils/i18n.js b/lib/utils/i18n.js
index 3ef1557..dcd9150 100644
--- a/lib/utils/i18n.js
+++ b/lib/utils/i18n.js
@@ -48,6 +48,9 @@ var normalizeLanguage = _.memoize(function(lang) {
score: compareLocales(lang, locale)
}
})
+ .filter(function(lang) {
+ return lang.score > 0;
+ })
.sortBy("score")
.pluck("locale")
.last()
diff --git a/lib/utils/images.js b/lib/utils/images.js
index 61a52dc..1e90317 100644
--- a/lib/utils/images.js
+++ b/lib/utils/images.js
@@ -22,7 +22,11 @@ var convertSVG = function(source, dest, options) {
if (error.code == 127) error = new Error("Need to install 'svgexport' using 'npm install svgexport -g'");
return d.reject(error);
}
- d.resolve();
+ if (fs.existsSync(dest)) {
+ d.resolve();
+ } else {
+ d.reject(new Error("Error converting "+source));
+ }
});
return d.promise;
diff --git a/lib/utils/page.js b/lib/utils/page.js
index ccf5dfa..0714958 100644
--- a/lib/utils/page.js
+++ b/lib/utils/page.js
@@ -4,10 +4,12 @@ var path = require('path');
var cheerio = require('cheerio');
var domSerializer = require('dom-serializer');
var request = require('request');
+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) {
@@ -111,6 +113,7 @@ function normalizeHtml(src, options) {
$("img").each(function() {
var origin = undefined;
var src = $(this).attr("src");
+ if (!src) return;
var isExternal = links.isExternal(src);
// Transform as relative to the bases
@@ -123,7 +126,7 @@ function normalizeHtml(src, options) {
// If image is external and ebook, then downlaod the images
if (isExternal) {
origin = src;
- src = "/"+fs.getUniqueFilename(outputRoot, path.basename(src));
+ src = "/"+crc.crc32(origin).toString(16)+path.extname(origin);
src = links.toAbsolute(src, options.base, options.output);
isExternal = false;
}
@@ -181,6 +184,7 @@ function normalizeHtml(src, options) {
$("a").each(function() {
var href = $(this).attr("href");
+ if (!href) return;
if (links.isRelative(href)) {
var absolutePath = path.join(options.base, href);
@@ -226,34 +230,39 @@ function normalizeHtml(src, options) {
function convertImages(images, options) {
if (!options.convertImages) return Q();
+ var downloaded = [];
options.book.log.debug.ln("convert ", images.length, "images to png");
- return _.reduce(images, function(prev, image) {
- var imgin = path.resolve(options.book.options.output, image.source);
-
- return prev
-
- // Write image if need to be download
- .then(function() {
- if (!image.origin) return;
- options.book.log.debug("download image", 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));
- });
- }, Q())
+ 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");
});