mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Improve image conversion to png to avoid doublons
This commit is contained in:
+42
-18
@@ -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;
|
||||
// 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";
|
||||
}
|
||||
});
|
||||
|
||||
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()
|
||||
);
|
||||
|
||||
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());
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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 || {}, {
|
||||
|
||||
+45
-24
@@ -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);
|
||||
|
||||
// Reset as relative to book
|
||||
dest = path.relative(outputRoot, dest);
|
||||
|
||||
options.book.log.debug.ln("detect invalid image (will be converted to png):", src);
|
||||
|
||||
// Add to cache
|
||||
imgConversionCache[outputRoot][src] = dest;
|
||||
|
||||
// Push to convert
|
||||
toConvert.push({
|
||||
source: src,
|
||||
dest: dest
|
||||
});
|
||||
src = dest;
|
||||
}
|
||||
|
||||
// Absolute with input
|
||||
dest = path.resolve(options.book.root, dest);
|
||||
|
||||
// Get a name that doesn't exists
|
||||
dest = fs.getUniqueFilename(dest);
|
||||
|
||||
// Reset as relative to book
|
||||
dest = path.relative(options.book.root, dest);
|
||||
|
||||
options.book.log.debug.ln("detect invalid image (will be converted to png):", src);
|
||||
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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||

|
||||

|
||||

|
||||
Vendored
+1
@@ -4,3 +4,4 @@ A description
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
Vendored
+2
@@ -1 +1,3 @@
|
||||
# Summary
|
||||
|
||||
* [Page](PAGE.md)
|
||||
|
||||
Reference in New Issue
Block a user