Change api to separate html file and string processing.

This commit is contained in:
Alexandre Gigliotti
2015-10-13 13:59:25 -07:00
parent 2c981a6038
commit bd793daae4
9 changed files with 407 additions and 381 deletions

View File

@@ -6,30 +6,22 @@ var inlineLess = require('./inline-less');
var R = require('ramda');
var Ru = require('@panosoft/ramda-utils');
var inlineHtml = {};
/**
* Embed referenced local assets within and HTML file.
*
* @param {String} html
* Filename or html string.
* @param {Object} options
*
* @return {Promise}
*/
var inlineHtml = co.wrap(function * (html, options) {
inlineHtml.html = co.wrap(function * (html, options) {
options = Ru.defaults({
filename: null,
less: {},
verbose: false
}, options || {});
var filename;
try {
filename = html;
html = yield fs.readFile(filename, 'utf8');
}
catch (error) {
if (error.code === 'ENOENT') filename = options.filename;
else throw error;
}
var filename = options.filename;
// Embed assets
var files = [filename];
@@ -53,11 +45,14 @@ var inlineHtml = co.wrap(function * (html, options) {
}
files = R.uniq(files);
var result = {
html,
files
};
var result = { html, files };
return (options.verbose ? result : result.html);
});
inlineHtml.file = co.wrap(function * (filename, options) {
var html = yield fs.readFile(filename, 'utf8');
options = R.merge(options || {}, {filename});
return yield inlineHtml.html(html, options);
});
module.exports = inlineHtml;