diff options
Diffstat (limited to 'lib/output')
-rw-r--r-- | lib/output/assets-inliner.js | 42 | ||||
-rw-r--r-- | lib/output/base.js | 11 | ||||
-rw-r--r-- | lib/output/ebook.js | 14 | ||||
-rw-r--r-- | lib/output/folder.js | 10 |
4 files changed, 64 insertions, 13 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; diff --git a/lib/output/base.js b/lib/output/base.js index d301b14..dd62cff 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -84,7 +84,7 @@ Output.prototype.prepare = function() { // Write a page (parsable file), ex: markdown, etc Output.prototype.onPage = function(page) { - + return page.parse(this); }; // Copy an asset file (non-parsable), ex: images, etc @@ -100,15 +100,20 @@ Output.prototype.onRelativeLink = function(currentPage, href) { return href; }; -// Output a SVG as a file +// Output a SVG buffer as a file Output.prototype.onOutputSVG = function(page, svg) { return null; }; +// Output an image as a file +Output.prototype.onOutputImage = function(page, imgFile) { + // Don't replace it + return imgFile; +}; + // Finish the generation Output.prototype.finish = function() { }; - module.exports = Output; diff --git a/lib/output/ebook.js b/lib/output/ebook.js new file mode 100644 index 0000000..b968006 --- /dev/null +++ b/lib/output/ebook.js @@ -0,0 +1,14 @@ +var util = require('util'); + +var WebsiteOutput = require('./website'); +var AssetsInliner = require('./assets-inliner'); + +function EbookOutput() { + WebsiteOutput.apply(this, arguments); + AssetsInliner.call(this); +} +util.inherits(EbookOutput, AssetsInliner); +util.inherits(EbookOutput, WebsiteOutput); + + +module.exports = EbookOutput; diff --git a/lib/output/folder.js b/lib/output/folder.js index 7e6ddf0..4b1d9fa 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -5,7 +5,6 @@ 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'); /* @@ -26,15 +25,6 @@ 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()); |