9 Commits

Author SHA1 Message Date
Alexandre Gigliotti
f28f0e718a 0.1.10 2015-09-25 13:39:08 -07:00
Alexandre Gigliotti
ff5e4401f7 Fix bug where template expressions were interpreted as paths. 2015-09-25 13:38:32 -07:00
Alexandre Gigliotti
99eed58f96 Replace lodash with ramda. 2015-09-21 10:16:38 -07:00
Alexandre Gigliotti
dad1267607 Fix bug where assets with a space in their path could not be bundled. 2015-09-11 09:17:10 -07:00
Alexandre Gigliotti
f0b9b3cc1e Update dependencies. 2015-09-11 09:10:04 -07:00
Alexandre Gigliotti
f143065dbf 0.1.9 2015-08-26 15:09:16 -07:00
Alexandre Gigliotti
4501132051 Updated build config. 2015-08-26 15:08:09 -07:00
Alexandre Gigliotti
006d059e1b 0.1.8 2015-08-26 15:06:04 -07:00
Alexandre Gigliotti
0a743f6b2e Updated build config. 2015-08-26 15:05:59 -07:00
9 changed files with 60 additions and 20 deletions

View File

@@ -1,3 +1,7 @@
language: node_js
node_js:
- "node"
- "iojs"
notifications:
email:
on_success: change
on_failure: always

View File

@@ -1,17 +1,18 @@
var _ = require('lodash');
var co = require('co');
var cheerio = require('cheerio');
var fs = require('mz/fs');
var inlineStyle = require('./inline-style');
var inlineImg = require('./inline-img');
var inlineLinkLess = require('./inline-link-less');
var R = require('ramda');
var Ru = require('@panosoft/ramda-utils');
var inline = co.wrap(function * (html, options) {
options = _.defaults(options || {}, {
options = Ru.defaults({
filename: null,
less: {},
verbose: false
});
}, options || {});
var filename;
try {
filename = html;
@@ -48,7 +49,7 @@ var inline = co.wrap(function * (html, options) {
var result = {
html: html,
files: _.unique(_.flatten(files, true))
files: R.uniq(R.flatten(files, true))
};
return (options.verbose ? result : result.html);
});

View File

@@ -1,9 +1,10 @@
var _ = require('lodash');
var datauri = require('datauri');
var isLocalPath = require('is-local-path');
var isTemplateExpression = require('./is-template-expression');
var path = require('path');
var postcss = require('postcss');
var postcssUrl = require('postcss-url');
var R = require('ramda');
var url = require('url');
/**
@@ -15,8 +16,10 @@ var url = require('url');
*/
var clean = function (path) {
path = url.parse(path);
path = _.pick(path, ['protocol', 'host', 'pathname']);
return url.format(path);
path = R.pick(['protocol', 'host', 'pathname'], path);
path = url.format(path);
path = decodeURI(path);
return path;
};
/**
* Convert local url data type paths to datauris.
@@ -31,7 +34,7 @@ var inline = function (css, filename) {
var result = postcss()
.use(postcssUrl({
url: function (urlPath) {
if (isLocalPath(urlPath)) {
if (isLocalPath(urlPath) && !isTemplateExpression(urlPath)) {
urlPath = clean(urlPath);
urlPath = path.resolve(basePath, urlPath);
files.push(urlPath);

View File

@@ -1,6 +1,7 @@
var cheerio = require('cheerio');
var datauri = require('datauri');
var isLocalPath = require('is-local-path');
var isTemplateExpression = require('./is-template-expression');
var path = require('path');
var inline = function (html, filename) {
@@ -8,7 +9,8 @@ var inline = function (html, filename) {
var basedir = path.dirname(filename);
var $ = cheerio.load(html, {decodeEntities: false});
var images = $('img').filter(function (index, element) {
return isLocalPath($(element).attr('src'));
var path = $(element).attr('src');
return isLocalPath(path) && !isTemplateExpression(path);
});
images.each(function (index, element) {
var src = $(element).attr('src');

View File

@@ -1,14 +1,15 @@
var _ = require('lodash');
var co = require('co');
var cheerio = require('cheerio');
var fs = require('mz/fs');
var isLocalPath = require('is-local-path');
var less = require('less');
var path = require('path');
var R = require('ramda');
var Ru = require('@panosoft/ramda-utils');
var url = require('url');
var render = co.wrap(function * (filename, options) {
options = _.assign(options || {}, {
options = R.merge(options || {}, {
filename: filename
});
var contents = yield fs.readFile(filename, 'utf8');
@@ -23,9 +24,9 @@ var render = co.wrap(function * (filename, options) {
var inline = co.wrap(function * (html, filename, options) {
var files = [];
var basedir = path.dirname(filename);
options = _.defaults(options || {}, {
options = Ru.defaults({
relativeUrls: true
});
}, options || {});
// TODO Import less links
// get links
@@ -52,9 +53,9 @@ var inline = co.wrap(function * (html, filename, options) {
});
// create list of imported files from all outputs, unique listing
files.push(_.map(outputs, function (output) {
files.push(R.map(function (output) {
return output.imports;
}));
}, outputs));
return {
html: $.xml(),

View File

@@ -0,0 +1,14 @@
/**
* Tests whether a path is a mustache template expression.
*
* Note: would be best if this was file extension specific
* (i.e. *.hbs => test for `{{ }}` )
*
* @param {String} path
* @return {Boolean}
*/
var isTemplateExpression = function (path) {
return /^{{.*}}$/.test(path);
};
module.exports = isTemplateExpression;

View File

@@ -1,6 +1,6 @@
{
"name": "inline-html",
"version": "0.1.7",
"version": "0.1.10",
"description": "Inline local assets referenced in an HTML document.",
"repository": "panosoft/inline-html",
"main": "lib/index.js",
@@ -10,15 +10,16 @@
"author": "",
"license": "MIT",
"dependencies": {
"@panosoft/ramda-utils": "^0.1.12",
"cheerio": "^0.19.0",
"co": "^4.6.0",
"datauri": "^0.7.1",
"is-local-path": "^0.1.0",
"less": "^2.5.1",
"lodash": "^3.10.0",
"mz": "^2.0.0",
"postcss": "^5.0.0",
"postcss-url": "^4.0.0",
"postcss-url": "^5.0.0",
"ramda": "^0.17.1",
"string": "^3.3.0"
},
"devDependencies": {

1
test/fixtures/file space.txt vendored Normal file
View File

@@ -0,0 +1 @@
Test.

View File

@@ -152,6 +152,14 @@ describe('inlineHtml', function () {
var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
return expect(inline(html)).to.eventually.equal(html);
});
it('ignore css url template expression paths', function () {
var html = `<style> div { background-image: url({{path}}); }</style>`;
return expect(inline(html)).to.eventually.equal(html);
});
it('ignore img src template expression paths', function () {
var html = `<img src="{{path}}"/>`;
return expect(inline(html)).to.eventually.equal(html);
});
it('ignore query strings and hashes on local paths', function () {
var filename = path.resolve(__dirname, 'fixtures/file.txt');
var url = `${filename}?query=string#hash`;
@@ -159,5 +167,10 @@ describe('inlineHtml', function () {
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
return expect(inline(html(url))).to.eventually.equal(html(uri));
});
it('handle assets with a space in their filename', function () {
var filename = path.resolve(__dirname, 'fixtures/file space.txt');
var uri = datauri(filename);
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
return expect(inline(html(filename))).to.eventually.equal(html(uri));
});
});