summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-02-12 21:59:49 +0100
committerSamy Pesse <samypesse@gmail.com>2016-02-12 21:59:49 +0100
commit39b6562d1445e9a6c43a377d2a978eefa6458755 (patch)
tree77d530bca613c44e8c8fee8e6944d538d44f3ca7 /lib
parent0d966fe19738089607de3927694ac5f2bd41f03f (diff)
downloadgitbook-39b6562d1445e9a6c43a377d2a978eefa6458755.zip
gitbook-39b6562d1445e9a6c43a377d2a978eefa6458755.tar.gz
gitbook-39b6562d1445e9a6c43a377d2a978eefa6458755.tar.bz2
Add pipeline to outline svg as png
Diffstat (limited to 'lib')
-rw-r--r--lib/output/base.js5
-rw-r--r--lib/output/folder.js12
-rw-r--r--lib/output/website/index.js8
-rw-r--r--lib/page/html.js28
-rw-r--r--lib/page/index.js3
-rw-r--r--lib/utils/fs.js9
-rw-r--r--lib/utils/images.js16
7 files changed, 73 insertions, 8 deletions
diff --git a/lib/output/base.js b/lib/output/base.js
index 39e46c2..d301b14 100644
--- a/lib/output/base.js
+++ b/lib/output/base.js
@@ -100,6 +100,11 @@ Output.prototype.onRelativeLink = function(currentPage, href) {
return href;
};
+// Output a SVG as a file
+Output.prototype.onOutputSVG = function(page, svg) {
+ return null;
+};
+
// Finish the generation
Output.prototype.finish = function() {
diff --git a/lib/output/folder.js b/lib/output/folder.js
index 96ea443..7e6ddf0 100644
--- a/lib/output/folder.js
+++ b/lib/output/folder.js
@@ -5,10 +5,11 @@ var path = require('path');
var Output = require('./base');
var fs = require('../utils/fs');
var pathUtil = require('../utils/path');
+var imagesUtil = require('../utils/images');
var Promise = require('../utils/promise');
/*
-This output require the native fs module to output
+This output requires the native fs module to output
book as a directory (mapping assets and pages)
*/
@@ -25,6 +26,15 @@ FolderOutput.prototype.onAsset = function(filename) {
);
};
+// Output a SVG as a file
+Output.prototype.onOutputSVG = function(page, svg) {
+ this.log.debug.ln('output svg from', page.path);
+ var filename = _.uniqueId('svg_') + '.png';
+
+ return imagesUtil.convertSVGBufferToPNG(svg, this.resolve(filename))
+ .thenResolve('/' + filename);
+};
+
// Prepare the generation by creating the output folder
FolderOutput.prototype.prepare = function() {
return fs.mkdirp(this.root());
diff --git a/lib/output/website/index.js b/lib/output/website/index.js
index 35e81c2..e60dae2 100644
--- a/lib/output/website/index.js
+++ b/lib/output/website/index.js
@@ -1,13 +1,13 @@
var util = require('util');
-var Output = require('../base');
+var FolderOutput = require('../base');
function WebsiteOutput() {
- Output.apply(this, arguments);
+ FolderOutput.apply(this, arguments);
}
-util.inherits(WebsiteOutput, Output);
+util.inherits(WebsiteOutput, FolderOutput);
// Write a page (parsable file)
-WebsiteOutput.prototype.writePage = function(page) {
+WebsiteOutput.prototype.onPage = function(page) {
};
diff --git a/lib/page/html.js b/lib/page/html.js
index b19d5ed..948462a 100644
--- a/lib/page/html.js
+++ b/lib/page/html.js
@@ -22,7 +22,10 @@ function HTMLPipeline(htmlString, opts) {
convertImages: true,
// Calcul new href for a relative link
- onRelativeLink: _.identity
+ onRelativeLink: _.identity,
+
+ // Output a svg, if returns null the svg is kept inlined
+ onOutputSVG: _.constant(null)
});
this.$ = cheerio.load(htmlString, {
@@ -70,6 +73,28 @@ HTMLPipeline.prototype.addHeadingIDs = function() {
});
};
+// Outline SVG from the HML
+HTMLPipeline.prototype.outlineSVG = function() {
+ var that = this;
+
+ var $svgs = this.$('svg');
+
+ return Promise.serie($svgs, function(svg) {
+ var $svg = that.$(svg);
+ var content = [
+ '<?xml version="1.0" encoding="UTF-8"?>',
+ renderDOM(that.$, $svg)
+ ].join('\n');
+
+ return Promise(that.opts.onOutputSVG(content))
+ .then(function(filename) {
+ if (!filename) return;
+
+ $svg.replaceWith(that.$('<img>').attr('src', filename));
+ });
+ });
+};
+
// Write content to the pipeline
HTMLPipeline.prototype.output = function() {
var that = this;
@@ -77,6 +102,7 @@ HTMLPipeline.prototype.output = function() {
return Promise()
.then(this.normalizeLinks)
.then(this.addHeadingIDs)
+ .then(this.outlineSVG)
.then(function() {
return renderDOM(that.$);
});
diff --git a/lib/page/index.js b/lib/page/index.js
index 8d1bfed..282cffe 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -119,7 +119,8 @@ Page.prototype.parse = function(output) {
.then(function() {
var pipelineOpts = {
// Replace links to page of summary
- onRelativeLink: _.partial(output.onRelativeLink, that)
+ onRelativeLink: _.partial(output.onRelativeLink, that),
+ onOutputSVG: _.partial(output.onOutputSVG, that)
};
var pipeline = new HTMLPipeline(that.content, pipelineOpts);
diff --git a/lib/utils/fs.js b/lib/utils/fs.js
index 2fa6ff9..7745448 100644
--- a/lib/utils/fs.js
+++ b/lib/utils/fs.js
@@ -1,6 +1,7 @@
var fs = require('graceful-fs');
var mkdirp = require('mkdirp');
var destroy = require('destroy');
+var tmp = require('tmp');
var Promise = require('./promise');
@@ -52,6 +53,11 @@ function fileExists(filename) {
return d.promise;
}
+// Generate temporary file
+function genTmpFile(opts) {
+ return Promise.nfcall(tmp.file, opts)
+ .get(0);
+}
module.exports = {
exists: fileExists,
@@ -62,5 +68,6 @@ module.exports = {
statSync: fs.statSync,
readdir: Promise.nfbind(fs.readdir),
writeStream: writeStream,
- copy: copyFile
+ copy: copyFile,
+ tmpFile: genTmpFile
};
diff --git a/lib/utils/images.js b/lib/utils/images.js
index 3ba0f1f..45bc0b0 100644
--- a/lib/utils/images.js
+++ b/lib/utils/images.js
@@ -2,6 +2,7 @@ var fs = require('fs');
var Promise = require('./promise');
var command = require('./command');
+var fs = require('./fs');
var error = require('./error');
// Convert a svg file to a pmg
@@ -21,7 +22,22 @@ function convertSVGToPNG(source, dest, options) {
});
}
+// Convert a svg buffer to a png file
+function convertSVGBufferToPNG(buf, dest) {
+ // Create a temporary SVG file to convert
+ return fs.tmpFile({
+ postfix: '.svg'
+ })
+ .then(function(tmpSvg) {
+ return fs.writeFile(tmpSvg, buf)
+ .then(function() {
+ return convertSVGToPNG(tmpSvg, dest);
+ });
+ });
+}
+
module.exports = {
convertSVGToPNG: convertSVGToPNG,
+ convertSVGBufferToPNG: convertSVGBufferToPNG,
INVALID: ['.svg']
}; \ No newline at end of file