diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-29 18:16:23 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-29 18:16:23 +0100 |
commit | 5fc0c6b507cdeadca710ce3810a886b2eaed52e9 (patch) | |
tree | 2d31f77b0d8ea03e346b2299adf9b0693555794e /lib/utils/page.js | |
parent | c8e72092c707d69799c1ccf99ed101970482880b (diff) | |
download | gitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.zip gitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.tar.gz gitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.tar.bz2 |
Convert inline svg as png if needed
Diffstat (limited to 'lib/utils/page.js')
-rw-r--r-- | lib/utils/page.js | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/lib/utils/page.js b/lib/utils/page.js index 4c8605d..3f04e99 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -67,10 +67,30 @@ function pregQuote( str ) { function normalizeHtml(src, options) { var $ = cheerio.load(src, { xmlMode: true}); var toConvert = []; + var svgContent = {}; var outputRoot = options.book.options.output; imgConversionCache[outputRoot] = imgConversionCache[outputRoot] || {}; + // Find svg images to extract and process + if (options.convertImages) { + $("svg").each(function() { + var content = $.html($(this), { xmlMode: true}); + + var svgId = _.uniqueId("svg"); + var dest = svgId+".svg"; + + // Generate filename + dest = path.resolve(outputRoot, dest); + dest = fs.getUniqueFilename(dest); + dest = "/"+path.relative(outputRoot, dest); + + svgContent[dest] = content; + $(this).replaceWith($("<img>").attr("src", dest)); + }); + } + + // Find images to normalize $("img").each(function() { var src = $(this).attr("src"); var isExternal = links.isExternal(src); @@ -119,6 +139,7 @@ function normalizeHtml(src, options) { // Push to convert toConvert.push({ + content: svgContent[srcAbs], source: isExternal? srcAbs : path.join("./", srcAbs), dest: path.join("./", dest) }); @@ -178,13 +199,25 @@ function normalizeHtml(src, options) { // Convert svg images to png function convertImages(images, options) { + if (!options.convertImages) return Q(); + options.book.log.info.ln("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); + 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); + + return prev + + // Write svg if content + .then(function() { + if (!image.content) return; + + return fs.writeFile(imgin, image.content); + }) + // Convert + .then(function(){ options.book.log.debug("convert image", image.source, "to", image.dest, "..."); return imgUtils.convertSVG(imgin, imgout) |