summaryrefslogtreecommitdiffstats
path: root/lib/output/assets-inliner.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output/assets-inliner.js')
-rw-r--r--lib/output/assets-inliner.js42
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;