diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-02-12 23:57:43 +0100 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-02-12 23:57:43 +0100 |
commit | 4c6717e23488656686f276aa2b40ce1d1c7641f8 (patch) | |
tree | c728f106224cedc836b83b06609011990eac4f5a /lib/output/assets-inliner.js | |
parent | 39b6562d1445e9a6c43a377d2a978eefa6458755 (diff) | |
download | gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.zip gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.gz gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.bz2 |
Add conversion of svg to png for assets inliner
Diffstat (limited to 'lib/output/assets-inliner.js')
-rw-r--r-- | lib/output/assets-inliner.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js new file mode 100644 index 0000000..5f33956 --- /dev/null +++ b/lib/output/assets-inliner.js @@ -0,0 +1,42 @@ +var _ = require('lodash'); +var util = require('util'); +var path = require('path'); + +var FolderOutput = require('./folder'); +var imagesUtil = require('../utils/images'); + +/* +Utility mixin to inline all the assets in a book: + - Outline <svg> tags + - Convert svg images as png + - Download remote images +*/ + +function AssetsInliner() { + FolderOutput.apply(this, arguments); +} +util.inherits(AssetsInliner, FolderOutput); + +// Output a SVG buffer as a file +AssetsInliner.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); +}; + +// Output an image as a file +AssetsInliner.prototype.onOutputImage = function(page, imgFile) { + if (path.extname(imgFile).toLowerCase() != '.svg') { + return imgFile; + } + + // Convert SVG to PNG + var filename = _.uniqueId('svg_') + '.png'; + return imagesUtil.convertSVGToPNG(page.resolve(imgFile), this.resolve(filename)) + .thenResolve('/' + filename); +}; + + +module.exports = AssetsInliner; |