summaryrefslogtreecommitdiffstats
path: root/lib/output/assets-inliner.js
blob: b5d076eac3e5bb130cf532c4438ad1bbef635e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var util = require('util');
var path = require('path');
var crc = require('crc');

var FolderOutput = require('./folder')();
var Promise = require('../utils/promise');
var fs = require('../utils/fs');
var imagesUtil = require('../utils/images');
var location = require('../utils/location');

var DEFAULT_ASSETS_FOLDER = 'assets';

/*
Mixin to inline all the assets in a book:
    - Outline <svg> tags
    - Download remote images
    - Convert .svg images as png
*/

module.exports = function assetsInliner(Base) {
    Base = Base || FolderOutput;

    function AssetsInliner() {
        Base.apply(this, arguments);

        // Map of svg already converted
        this.svgs = {};
        this.inlineSvgs = {};

        // Map of images already downloaded
        this.downloaded = {};
    }
    util.inherits(AssetsInliner, Base);

    // Output a SVG buffer as a file
    AssetsInliner.prototype.onOutputSVG = function(page, svg) {
        this.log.debug.ln('output svg from', page.path);

        // 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()

        // Download file if external
        .then(function() {
            if (!location.isExternal(src)) return;

            return that.downloadAsset(src)
            .then(function(_asset) {
                src = '/' + _asset;
            });

        })
        .then(function() {
            if (path.extname(src).toLowerCase() != '.svg') {
                return src;
            }

            // Convert SVG to PNG
            return that.convertSVGFile(that.resolveForPage(page, src));
        })

        // Return relative path from the page
        .then(function(filename) {
            return page.relative('/' + filename);
        });
    };

    // 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]);

        var that = this;
        var ext = path.extname(src);
        var hash = crc.crc32(src).toString(16);

        // Create new file
        return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + ext)
        .then(function(filename) {
            that.downloaded[src] = filename;

            that.log.debug.ln('downloading asset', src);
            return fs.download(src, that.resolve(filename))
            .thenResolve(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]);

        var that = this;
        var hash = crc.crc32(src).toString(16);

        // Create new file
        return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png')
        .then(function(filename) {
            that.svgs[src] = filename;

            return imagesUtil.convertSVGToPNG(src, that.resolve(filename))
            .thenResolve(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);

        // Already converted?
        if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]);

        return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png')
        .then(function(filename) {
            that.inlineSvgs[hash] = filename;

            return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename))
            .thenResolve(filename);
        });
    };

    return AssetsInliner;
};