summaryrefslogtreecommitdiffstats
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
parent02d7102c687c3d2295bd1586fe6e79f14396e740 (diff)
downloadgitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.zip
gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.gz
gitbook-56d1720aaea2472f12e045f61a2fb0bdf7da9343.tar.bz2
Improve image conversion to png to avoid doublons
-rw-r--r--lib/book.js62
-rw-r--r--lib/utils/images.js2
-rw-r--r--lib/utils/page.js63
-rw-r--r--test/ebook.js1
-rw-r--r--test/fixtures/test4/PAGE.md5
-rw-r--r--test/fixtures/test4/README.md1
-rw-r--r--test/fixtures/test4/SUMMARY.md2
7 files changed, 96 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);
diff --git a/test/ebook.js b/test/ebook.js
index e0083c8..05a2000 100644
--- a/test/ebook.js
+++ b/test/ebook.js
@@ -16,6 +16,7 @@ describe('eBook Generator', function () {
it('should correctly convert svg images to png', function(done) {
testGeneration(books[4], "ebook", function(output) {
assert(fs.existsSync(path.join(output, "test.png")));
+ assert(!fs.existsSync(path.join(output, "test_0.png")));
assert(fs.existsSync(path.join(output, "NewTux.png")));
}, done);
});
diff --git a/test/fixtures/test4/PAGE.md b/test/fixtures/test4/PAGE.md
new file mode 100644
index 0000000..0320232
--- /dev/null
+++ b/test/fixtures/test4/PAGE.md
@@ -0,0 +1,5 @@
+
+
+![test image to be converted](./test.svg)
+![test url](http://upload.wikimedia.org/wikipedia/commons/b/b0/NewTux.svg)
+![test image to be converted, second use](./test.svg)
diff --git a/test/fixtures/test4/README.md b/test/fixtures/test4/README.md
index 0d3bd13..19892a5 100644
--- a/test/fixtures/test4/README.md
+++ b/test/fixtures/test4/README.md
@@ -4,3 +4,4 @@ A description
![test image to be converted](./test.svg)
![test url](http://upload.wikimedia.org/wikipedia/commons/b/b0/NewTux.svg)
+![test image to be converted, second use](./test.svg)
diff --git a/test/fixtures/test4/SUMMARY.md b/test/fixtures/test4/SUMMARY.md
index ac9323c..44a33de 100644
--- a/test/fixtures/test4/SUMMARY.md
+++ b/test/fixtures/test4/SUMMARY.md
@@ -1 +1,3 @@
# Summary
+
+* [Page](PAGE.md)