diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-28 12:46:15 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-28 12:46:15 +0100 |
commit | 534edaceac1bc436a5230094cc2ae5cb32596fb8 (patch) | |
tree | 707de4eac7610ee8426cd63a8c8e80e13c999cad | |
parent | ee13b4bf75b05bf4814deffdfc9609ce7fa8922a (diff) | |
download | gitbook-534edaceac1bc436a5230094cc2ae5cb32596fb8.zip gitbook-534edaceac1bc436a5230094cc2ae5cb32596fb8.tar.gz gitbook-534edaceac1bc436a5230094cc2ae5cb32596fb8.tar.bz2 |
Handle absolute url for images conversion (svg -> png)
-rw-r--r-- | lib/utils/images.js | 12 | ||||
-rw-r--r-- | lib/utils/page.js | 12 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/ebook.js | 1 | ||||
-rw-r--r-- | test/fixtures/test4/README.md | 1 |
5 files changed, 24 insertions, 5 deletions
diff --git a/lib/utils/images.js b/lib/utils/images.js index 5c2db69..ebe9043 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -2,9 +2,12 @@ var _ = require("lodash"); var Q = require("q"); var fs = require("fs"); var gm = require("gm"); +var request = require("request"); +var links = require("./links"); -var convert = function(source, dest, options) { +// Convert a file +var convertFile = function(source, dest, options) { var d = Q.defer(); options = _.defaults(options || {}, { resize: null @@ -30,6 +33,13 @@ var convert = function(source, dest, options) { return d.promise; }; +// Convert a file or url +var convert = function(source, dest, options) { + if (links.isExternal(source)) source = request(source); + + return convertFile(source, dest, options); +}; + module.exports = { convert: convert, INVALID: [".svg"] diff --git a/lib/utils/page.js b/lib/utils/page.js index fa333f2..483fdfd 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -78,8 +78,14 @@ function normalizeHtml(src, options) { if (options.convertImages) { var ext = path.extname(src); if (_.contains(imgUtils.INVALID, ext)) { - // Replace extension - var dest = path.join(path.dirname(src), path.basename(src, ext)+".png"); + 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(options.book.root, dest); @@ -148,7 +154,7 @@ function normalizeHtml(src, options) { function convertImages(images, options) { return _.reduce(images, function(prev, image) { return prev.then(function() { - var imgin = path.resolve(options.book.root, image.source); + var imgin = links. isExternal(image.source)? image.source : path.resolve(options.book.root, image.source); var imgout = path.resolve(options.book.options.output, image.dest); options.book.log.debug("convert image", image.source, "to", image.dest, "..."); diff --git a/package.json b/package.json index 8f541d6..870dc16 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "crc": "3.2.1", "bash-color": "0.0.3", "URIjs": "1.14.1", - "gm": "1.17.0" + "gm": "1.17.0", + "request": "2.51.0" }, "devDependencies": { "mocha": "1.18.2", diff --git a/test/ebook.js b/test/ebook.js index b82bb19..e0083c8 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, "NewTux.png"))); }, done); }); }); diff --git a/test/fixtures/test4/README.md b/test/fixtures/test4/README.md index 6d7efdc..0d3bd13 100644 --- a/test/fixtures/test4/README.md +++ b/test/fixtures/test4/README.md @@ -3,3 +3,4 @@ A description  + |