Added support for verbose option that returns filenames of each inlined file alongside inlined html.

This commit is contained in:
Alexandre Gigliotti
2015-07-24 15:51:46 -07:00
parent dd4f03257c
commit 3cfd4f2b86
6 changed files with 73 additions and 37 deletions

View File

@@ -7,13 +7,12 @@ var less = require('less');
var path = require('path');
var url = require('url');
var render = co.wrap(function * (filepath, options) {
var render = co.wrap(function * (filename, options) {
options = _.assign(options || {}, {
filename: filepath
filename: filename
});
var contents = yield fs.readFile(filepath, 'utf8');
var output = yield less.render(contents, options);
return output.css;
var contents = yield fs.readFile(filename, 'utf8');
return yield less.render(contents, options);
});
/**
* @params html
@@ -21,8 +20,9 @@ var render = co.wrap(function * (filepath, options) {
* @params options
* LESS compiler options
*/
var inline = co.wrap(function * (html, filepath, options) {
var basedir = path.dirname(filepath);
var inline = co.wrap(function * (html, filename, options) {
var files = [];
var basedir = path.dirname(filename);
options = _.defaults(options || {}, {
relativeUrls: true
});
@@ -36,23 +36,29 @@ var inline = co.wrap(function * (html, filepath, options) {
});
// render stylesheets
var contents = [];
var outputs = [];
links.each(function (index, element) {
var href = $(element).attr('href');
var filepath = path.resolve(basedir, href);
contents.push(render(filepath, options));
var filename = path.resolve(basedir, href);
files.push(filename);
outputs.push(render(filename, options));
});
contents = yield contents;
// TODO for each content: convert css url path -> datauri
// can use inline-css-url
outputs = yield outputs;
// replace links
links.each(function (index, element) {
var style = $('<style>').html(contents[index]);
var style = $('<style>').html(outputs[index].css);
$(element).replaceWith(style);
});
return $.html();
// create list of imported files from all outputs, unique listing
files.push(_.map(outputs, function (output) {
return output.imports;
}));
return {
html: $.html(),
files: files
};
});
module.exports = inline;