diff options
Diffstat (limited to 'lib/utils/page.js')
-rw-r--r-- | lib/utils/page.js | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/lib/utils/page.js b/lib/utils/page.js index 2f56a52..7d21e94 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -7,6 +7,8 @@ var links = require('./links'); var imgUtils = require('./images'); var fs = require('./fs'); +var imgConversionCache = {}; + function replaceText($, el, search, replace, text_only ) { return $(el).each(function(){ var node = this.firstChild, @@ -65,6 +67,9 @@ function pregQuote( str ) { function normalizeHtml(src, options) { var $ = cheerio.load(src); var toConvert = []; + var outputRoot = options.book.options.output; + + imgConversionCache[outputRoot] = imgConversionCache[outputRoot] || {}; $("img").each(function() { var src = $(this).attr("src"); @@ -78,30 +83,41 @@ function normalizeHtml(src, options) { if (options.convertImages) { var ext = path.extname(src); if (_.contains(imgUtils.INVALID, ext)) { - var dest = ""; - - if (links.isExternal(src)) { - dest = path.basename(src, ext)+".png"; + if (imgConversionCache[outputRoot][src]) { + // Already converted + src = imgConversionCache[outputRoot][src]; } else { - // Replace extension - var dest = path.join(path.dirname(src), path.basename(src, ext)+".png"); - } + // Not converted yet + var dest = ""; + + if (links.isExternal(src)) { + dest = path.basename(src, ext)+".png"; + } else { + // Replace extension + var dest = path.join(path.dirname(src), path.basename(src, ext)+".png"); + } + + // Absolute with input + dest = path.resolve(outputRoot, dest); + + // Get a name that doesn't exists + dest = fs.getUniqueFilename(dest); - // Absolute with input - dest = path.resolve(options.book.root, dest); + // Reset as relative to book + dest = path.relative(outputRoot, dest); - // Get a name that doesn't exists - dest = fs.getUniqueFilename(dest); + options.book.log.debug.ln("detect invalid image (will be converted to png):", src); - // Reset as relative to book - dest = path.relative(options.book.root, dest); + // Add to cache + imgConversionCache[outputRoot][src] = dest; - options.book.log.debug.ln("detect invalid image (will be converted to png):", src); - toConvert.push({ - source: src, - dest: dest - }); - src = dest; + // Push to convert + toConvert.push({ + source: src, + dest: dest + }); + src = dest; + } } } @@ -152,12 +168,15 @@ function normalizeHtml(src, options) { // Convert svg images to png function convertImages(images, options) { + options.book.log.info("convert ", images.length, "images to png"); + return _.reduce(images, function(prev, image) { return prev.then(function() { var imgin = links. isExternal(image.source)? image.source : path.resolve(options.book.options.output, image.source); var imgout = path.resolve(options.book.options.output, image.dest); options.book.log.debug("convert image", image.source, "to", image.dest, "..."); + return imgUtils.convertSVG(imgin, imgout) .then(function() { options.book.log.debug.ok(); @@ -166,7 +185,10 @@ function convertImages(images, options) { throw err; }); }); - }, Q()); + }, Q()) + .then(function() { + options.book.log.info.ok(); + }); }; @@ -211,7 +233,6 @@ function normalizePage(sections, options) { return Q() .then(function() { toConvert = _.uniq(toConvert, 'source'); - return convertImages(toConvert, options); }) .thenResolve(sections); |