diff options
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) |