diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-28 16:36:17 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-28 16:36:17 +0100 |
commit | 56d1720aaea2472f12e045f61a2fb0bdf7da9343 (patch) | |
tree | ac1b18bd3851e3395d1dee9eca5a2ae11ee66c84 /lib | |
parent | 02d7102c687c3d2295bd1586fe6e79f14396e740 (diff) | |
download | gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.zip gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.gz gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.bz2 |
Improve image conversion to png to avoid doublons
Diffstat (limited to 'lib')
-rw-r--r-- | lib/book.js | 62 | ||||
-rw-r--r-- | lib/utils/images.js | 2 | ||||
-rw-r--r-- | lib/utils/page.js | 63 |
3 files changed, 87 insertions, 40 deletions
diff --git a/lib/book.js b/lib/book.js index 4e4279a..62fe0db 100644 --- a/lib/book.js +++ b/lib/book.js @@ -200,30 +200,54 @@ Book.prototype.generate = function(generator) { return generator.prepare(); }) + + // Generate content .then(function() { if (that.isMultilingual()) { return that.generateMultiLingual(generator); } else { - // Copy file and replace markdown file - return Q.all( - _.chain(that.files) - .map(function(file) { - if (!file) return; - - if (file[file.length -1] == "/") { - that.log.debug.ln("transferring folder", file); - return Q(generator.transferFolder(file)); - } else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) { - that.log.debug.ln("converting", file); - return Q(generator.convertFile(file)); - } else { - that.log.debug.ln("transferring file", file); - return Q(generator.transferFile(file)); - } - }) - .value() - ); + // Separate list of files into the different operations needed + var ops = _.groupBy(that.files, function(file) { + if (file[file.length -1] == "/") { + return "directories"; + } else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) { + return "content"; + } else { + return "files"; + } + }); + + + return Q() + + // First, let's create folder + .then(function() { + return _.reduce(ops["directories"] || [], function(prev, folder) { + return prev.then(function() { + that.log.debug.ln("transferring folder", folder); + return Q(generator.transferFolder(folder)); + }); + }, Q()); + }) + + // Then, let's copy other files + .then(function() { + return Q.all(_.map(ops["files"] || [], function(file) { + that.log.debug.ln("transferring file", file); + return Q(generator.transferFile(file)); + })); + }) + + // Finally let's generate content + .then(function() { + return _.reduce(ops["content"] || [], function(prev, file) { + return prev.then(function() { + that.log.debug.ln("converting", file); + return Q(generator.convertFile(file)); + }); + }, Q()); + }); } }) diff --git a/lib/utils/images.js b/lib/utils/images.js index 9a97f98..ecbc2fb 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -8,6 +8,8 @@ var links = require("./links"); // Convert a svg file var convertSVGFile = function(source, dest, options) { + if (!fs.existsSync(source)) return Q.reject(new Error("File doesn't exist: "+source)); + var d = Q.defer(); options = _.defaults(options || {}, { 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); |