From 5fc0c6b507cdeadca710ce3810a886b2eaed52e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 29 Jan 2015 18:16:23 +0100 Subject: [PATCH] Convert inline svg as png if needed --- lib/template.js | 7 +++++- lib/utils/page.js | 39 ++++++++++++++++++++++++++++++--- test/ebook.js | 1 + test/fixtures/test4/sub/PAGE.md | 12 ++++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/lib/template.js b/lib/template.js index f1427631c..e30074db0 100644 --- a/lib/template.js +++ b/lib/template.js @@ -75,6 +75,11 @@ var TemplateEngine = function(book) { // Bind methods _.bindAll(this); + + // Default block "html" that return html not parsed + this.addBlock("html", { + process: _.identity + }); }; // Process a block in a context @@ -291,7 +296,7 @@ TemplateEngine.prototype.addBlock = function(name, block) { TemplateEngine.prototype._applyShortcut = function(parser, content, shortcut) { if (!_.contains(shortcut.parsers, parser)) return content; var regex = new RegExp( - stringUtils.escapeRegex(shortcut.start) + "\\s*([\\s\\S]*?[^\\$])\\s*" + stringUtils.escapeRegex(shortcut.end), + stringUtils.escapeRegex(shortcut.start) + "([\\s\\S]*?[^\\$])" + stringUtils.escapeRegex(shortcut.end), 'g' ); return content.replace(regex, function(all, match) { diff --git a/lib/utils/page.js b/lib/utils/page.js index 4c8605d35..3f04e9935 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($("").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) diff --git a/test/ebook.js b/test/ebook.js index 9e2dab6a5..38e6f1398 100644 --- a/test/ebook.js +++ b/test/ebook.js @@ -27,6 +27,7 @@ describe('eBook Generator', function () { assert(pageContent.indexOf('src="../test.png"') >= 0); assert(pageContent.indexOf('src="../NewTux.png"') >= 0); + assert(pageContent.indexOf('= 0); assert(readmeContent.indexOf('src="NewTux.png"') >= 0); diff --git a/test/fixtures/test4/sub/PAGE.md b/test/fixtures/test4/sub/PAGE.md index 094da5939..f2ebc34a9 100644 --- a/test/fixtures/test4/sub/PAGE.md +++ b/test/fixtures/test4/sub/PAGE.md @@ -1,5 +1,13 @@ - +## ![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) + + +## Inline svg + +{% html %} + + + +{% endhtml %}