summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-29 18:16:23 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-29 18:16:23 +0100
commit5fc0c6b507cdeadca710ce3810a886b2eaed52e9 (patch)
tree2d31f77b0d8ea03e346b2299adf9b0693555794e
parentc8e72092c707d69799c1ccf99ed101970482880b (diff)
downloadgitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.zip
gitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.tar.gz
gitbook-5fc0c6b507cdeadca710ce3810a886b2eaed52e9.tar.bz2
Convert inline svg as png if needed
-rw-r--r--lib/template.js7
-rw-r--r--lib/utils/page.js39
-rw-r--r--test/ebook.js1
-rw-r--r--test/fixtures/test4/sub/PAGE.md12
4 files changed, 53 insertions, 6 deletions
diff --git a/lib/template.js b/lib/template.js
index f142763..e30074d 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 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)
diff --git a/test/ebook.js b/test/ebook.js
index 9e2dab6..38e6f13 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('<svg') < 0);
assert(readmeContent.indexOf('src="test.png"') >= 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 094da59..f2ebc34 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 %}
+<svg width="400" height="110">
+ <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)">
+</svg>
+{% endhtml %}