summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-28 18:23:48 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-28 18:23:48 +0100
commitc0d922560daa53ad31f356480e6fbe3f2191f471 (patch)
tree8f4f783fae262b318505208e8d2ff4ab51007783
parentf5ded5e5e1cccacd9288f8888943a105b6b3aaa5 (diff)
downloadgitbook-c0d922560daa53ad31f356480e6fbe3f2191f471.zip
gitbook-c0d922560daa53ad31f356480e6fbe3f2191f471.tar.gz
gitbook-c0d922560daa53ad31f356480e6fbe3f2191f471.tar.bz2
Improve svg conversion and links normalization
-rw-r--r--lib/utils/page.js30
-rw-r--r--test/ebook.js10
-rw-r--r--test/fixtures/test4/PAGE.md5
-rw-r--r--test/fixtures/test4/SUMMARY.md2
-rw-r--r--test/fixtures/test4/sub/PAGE.md5
5 files changed, 34 insertions, 18 deletions
diff --git a/lib/utils/page.js b/lib/utils/page.js
index 6a6ee9c..8de0b24 100644
--- a/lib/utils/page.js
+++ b/lib/utils/page.js
@@ -73,8 +73,9 @@ function normalizeHtml(src, options) {
$("img").each(function() {
var src = $(this).attr("src");
+ var isExternal = links.isExternal(src);
- // Transform as absolute
+ // Transform as relative to the bases
if (links.isRelative(src)) {
src = links.toAbsolute(src, options.base, options.output);
}
@@ -82,19 +83,24 @@ function normalizeHtml(src, options) {
// Convert if needed
if (options.convertImages) {
var ext = path.extname(src);
+
+ // Test image extension
if (_.contains(imgUtils.INVALID, ext)) {
- if (imgConversionCache[outputRoot][src]) {
+ var srcAbs = isExternal? src : path.join("/", options.base, src);
+
+ if (imgConversionCache[outputRoot][srcAbs]) {
// Already converted
- src = imgConversionCache[outputRoot][src];
+ src = imgConversionCache[outputRoot][srcAbs];
} else {
// Not converted yet
var dest = "";
- if (links.isExternal(src)) {
+ // Replace extension
+ if (isExternal) {
dest = path.basename(src, ext)+".png";
} else {
- // Replace extension
- var dest = path.join(path.dirname(src), path.basename(src, ext)+".png");
+ dest = path.join(path.dirname(srcAbs), path.basename(srcAbs, ext)+".png");
+ dest = dest[0] == "/"? dest.slice(1) : dest;
}
// Absolute with input
@@ -106,18 +112,20 @@ function normalizeHtml(src, options) {
// Reset as relative to book
dest = path.relative(outputRoot, dest);
- options.book.log.debug.ln("detect invalid image (will be converted to png):", src);
+ options.book.log.debug.ln("detect invalid image (will be converted to png):", srcAbs);
// Add to cache
- imgConversionCache[outputRoot][src] = dest;
+ imgConversionCache[outputRoot][srcAbs] = "/"+dest;
// Push to convert
toConvert.push({
- source: src,
- dest: dest
+ source: isExternal? srcAbs : path.join("./", srcAbs),
+ dest: path.join("./", dest)
});
- src = dest;
}
+
+ // Reset as relative to output
+ src = links.toAbsolute(src, options.base, options.output);
}
}
diff --git a/test/ebook.js b/test/ebook.js
index 05a2000..6facd8b 100644
--- a/test/ebook.js
+++ b/test/ebook.js
@@ -15,9 +15,17 @@ describe('eBook Generator', function () {
it('should correctly convert svg images to png', function(done) {
testGeneration(books[4], "ebook", function(output) {
+ var pageContent = fs.readFileSync(path.join(output, "sub/PAGE.html"), {encoding: "utf8"});
+
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")));
+
+ assert(!fs.existsSync(path.join(output, "test_0.png")));
+ assert(!fs.existsSync(path.join(output, "sub/test.png")));
+ assert(!fs.existsSync(path.join(output, "sub/NewTux.png")));
+
+ assert(pageContent.indexOf('src="../test.png"') >= 0);
+ assert(pageContent.indexOf('src="../NewTux.png"') >= 0);
}, done);
});
});
diff --git a/test/fixtures/test4/PAGE.md b/test/fixtures/test4/PAGE.md
deleted file mode 100644
index 0320232..0000000
--- a/test/fixtures/test4/PAGE.md
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-![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 44a33de..3cda114 100644
--- a/test/fixtures/test4/SUMMARY.md
+++ b/test/fixtures/test4/SUMMARY.md
@@ -1,3 +1,3 @@
# Summary
-* [Page](PAGE.md)
+* [Page](sub/PAGE.md)
diff --git a/test/fixtures/test4/sub/PAGE.md b/test/fixtures/test4/sub/PAGE.md
new file mode 100644
index 0000000..094da59
--- /dev/null
+++ b/test/fixtures/test4/sub/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)