summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-28 16:36:17 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-28 16:36:17 +0100
commit56d1720aaea2472f12e045f61a2fb0bdf7da9343 (patch)
treeac1b18bd3851e3395d1dee9eca5a2ae11ee66c84 /lib/utils
parent02d7102c687c3d2295bd1586fe6e79f14396e740 (diff)
downloadgitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.zip
gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.gz
gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.bz2
Improve image conversion to png to avoid doublons
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/images.js2
-rw-r--r--lib/utils/page.js63
2 files changed, 44 insertions, 21 deletions
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);