mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-03 20:20:35 +00:00
Add pipeline to outline svg as png
This commit is contained in:
@@ -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() {
|
||||
|
||||
|
||||
+11
-1
@@ -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());
|
||||
|
||||
@@ -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) {
|
||||
|
||||
};
|
||||
|
||||
|
||||
+27
-1
@@ -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.$);
|
||||
});
|
||||
|
||||
+2
-1
@@ -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);
|
||||
|
||||
|
||||
+8
-1
@@ -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
|
||||
};
|
||||
|
||||
@@ -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']
|
||||
};
|
||||
+1
-1
@@ -28,7 +28,7 @@
|
||||
"chokidar": "~1.0.5",
|
||||
"send": "0.2.0",
|
||||
"tiny-lr": "0.2.1",
|
||||
"tmp": "0.0.24",
|
||||
"tmp": "0.0.28",
|
||||
"crc": "3.2.1",
|
||||
"bash-color": "0.0.3",
|
||||
"urijs": "1.17.0",
|
||||
|
||||
Reference in New Issue
Block a user