mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Convert inline svg as png if needed
This commit is contained in:
+6
-1
@@ -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) {
|
||||
|
||||
+36
-3
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
Vendored
+10
-2
@@ -1,5 +1,13 @@
|
||||
|
||||
##
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
|
||||
## 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 %}
|
||||
|
||||
Reference in New Issue
Block a user