diff options
Diffstat (limited to 'lib/output/assets-inliner.js')
-rw-r--r-- | lib/output/assets-inliner.js | 181 |
1 files changed, 92 insertions, 89 deletions
diff --git a/lib/output/assets-inliner.js b/lib/output/assets-inliner.js index 9733505..b5d076e 100644 --- a/lib/output/assets-inliner.js +++ b/lib/output/assets-inliner.js @@ -2,7 +2,7 @@ var util = require('util'); var path = require('path'); var crc = require('crc'); -var FolderOutput = require('./folder'); +var FolderOutput = require('./folder')(); var Promise = require('../utils/promise'); var fs = require('../utils/fs'); var imagesUtil = require('../utils/images'); @@ -11,123 +11,126 @@ var location = require('../utils/location'); var DEFAULT_ASSETS_FOLDER = 'assets'; /* -Utility mixin to inline all the assets in a book: +Mixin to inline all the assets in a book: - Outline <svg> tags - Download remote images - Convert .svg images as png */ -function AssetsInliner() { - FolderOutput.apply(this, arguments); +module.exports = function assetsInliner(Base) { + Base = Base || FolderOutput; - // Map of svg already converted - this.svgs = {}; - this.inlineSvgs = {}; + function AssetsInliner() { + Base.apply(this, arguments); - // Map of images already downloaded - this.downloaded = {}; -} -util.inherits(AssetsInliner, FolderOutput); + // Map of svg already converted + this.svgs = {}; + this.inlineSvgs = {}; -// Output a SVG buffer as a file -AssetsInliner.prototype.onOutputSVG = function(page, svg) { - this.log.debug.ln('output svg from', page.path); + // Map of images already downloaded + this.downloaded = {}; + } + util.inherits(AssetsInliner, Base); - // Convert svg buffer to a png file - return this.convertSVGBuffer(svg) + // Output a SVG buffer as a file + AssetsInliner.prototype.onOutputSVG = function(page, svg) { + this.log.debug.ln('output svg from', page.path); - // Return relative path from the page - .then(function(filename) { - return page.relative('/' + filename); - }); -}; + // Convert svg buffer to a png file + return this.convertSVGBuffer(svg) + // Return relative path from the page + .then(function(filename) { + return page.relative('/' + filename); + }); + }; -// Output an image as a file -AssetsInliner.prototype.onOutputImage = function(page, src) { - var that = this; - return Promise() + // Output an image as a file + AssetsInliner.prototype.onOutputImage = function(page, src) { + var that = this; - // Download file if external - .then(function() { - if (!location.isExternal(src)) return; + return Promise() - return that.downloadAsset(src) - .then(function(_asset) { - src = '/' + _asset; - }); + // Download file if external + .then(function() { + if (!location.isExternal(src)) return; - }) - .then(function() { - if (path.extname(src).toLowerCase() != '.svg') { - return src; - } + return that.downloadAsset(src) + .then(function(_asset) { + src = '/' + _asset; + }); - // Convert SVG to PNG - return that.convertSVGFile(that.resolveForPage(page, src)); - }) + }) + .then(function() { + if (path.extname(src).toLowerCase() != '.svg') { + return src; + } - // Return relative path from the page - .then(function(filename) { - return page.relative('/' + filename); - }); -}; + // Convert SVG to PNG + return that.convertSVGFile(that.resolveForPage(page, src)); + }) -// Download an asset if not already download; returns the output file -AssetsInliner.prototype.downloadAsset = function(src) { - if (this.downloaded[src]) return Promise(this.downloaded[src]); + // Return relative path from the page + .then(function(filename) { + return page.relative('/' + filename); + }); + }; - var that = this; - var ext = path.extname(src); - var hash = crc.crc32(src).toString(16); + // Download an asset if not already download; returns the output file + AssetsInliner.prototype.downloadAsset = function(src) { + if (this.downloaded[src]) return Promise(this.downloaded[src]); - // Create new file - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + ext) - .then(function(filename) { - that.downloaded[src] = filename; + var that = this; + var ext = path.extname(src); + var hash = crc.crc32(src).toString(16); - that.log.debug.ln('downloading asset', src); - return fs.download(src, that.resolve(filename)) - .thenResolve(filename); - }); -}; + // Create new file + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + ext) + .then(function(filename) { + that.downloaded[src] = filename; -// Convert a .svg into an .png -// Return the output filename for the .png -AssetsInliner.prototype.convertSVGFile = function(src) { - if (this.svgs[src]) return Promise(this.svgs[src]); + that.log.debug.ln('downloading asset', src); + return fs.download(src, that.resolve(filename)) + .thenResolve(filename); + }); + }; - var that = this; - var hash = crc.crc32(src).toString(16); + // Convert a .svg into an .png + // Return the output filename for the .png + AssetsInliner.prototype.convertSVGFile = function(src) { + if (this.svgs[src]) return Promise(this.svgs[src]); - // Create new file - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') - .then(function(filename) { - that.svgs[src] = filename; + var that = this; + var hash = crc.crc32(src).toString(16); - return imagesUtil.convertSVGToPNG(src, that.resolve(filename)) - .thenResolve(filename); - }); -}; + // Create new file + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') + .then(function(filename) { + that.svgs[src] = filename; -// Convert an inline svg into an .png -// Return the output filename for the .png -AssetsInliner.prototype.convertSVGBuffer = function(buf) { - var that = this; - var hash = crc.crc32(buf).toString(16); + return imagesUtil.convertSVGToPNG(src, that.resolve(filename)) + .thenResolve(filename); + }); + }; - // Already converted? - if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]); + // Convert an inline svg into an .png + // Return the output filename for the .png + AssetsInliner.prototype.convertSVGBuffer = function(buf) { + var that = this; + var hash = crc.crc32(buf).toString(16); - return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') - .then(function(filename) { - that.inlineSvgs[hash] = filename; + // Already converted? + if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]); - return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename)) - .thenResolve(filename); - }); -}; + return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png') + .then(function(filename) { + that.inlineSvgs[hash] = filename; + return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename)) + .thenResolve(filename); + }); + }; -module.exports = AssetsInliner; + return AssetsInliner; +}; |