diff options
Diffstat (limited to 'lib/utils/page.js')
-rw-r--r-- | lib/utils/page.js | 63 |
1 files changed, 36 insertions, 27 deletions
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"); }); |