mirror of
https://github.com/remy/inliner.git
synced 2024-09-11 17:25:30 +03:00
chore: Merge branch 'feature/inliner-string'
This commit is contained in:
commit
d9b53c96b1
@ -6,8 +6,7 @@ cache:
|
||||
notifications:
|
||||
email: false
|
||||
node_js:
|
||||
- iojs-v2
|
||||
- iojs-v1
|
||||
- 4
|
||||
- '0.12'
|
||||
- '0.10'
|
||||
before_install:
|
||||
|
91
cli/index.js
91
cli/index.js
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var readFileSync = require('fs').readFileSync;
|
||||
var Promise = require('es6-promise').Promise; // jshint ignore:line
|
||||
|
||||
main();
|
||||
|
||||
@ -18,7 +19,7 @@ function main() {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (!url || argv.help) {
|
||||
if ((!url && !argv.useStdin) || argv.help) {
|
||||
var usage = readFileSync(
|
||||
__dirname + '/../docs/usage.txt', 'utf8'
|
||||
);
|
||||
@ -28,42 +29,66 @@ function main() {
|
||||
|
||||
var Inliner = require('../');
|
||||
|
||||
var inliner = new Inliner(url, argv, function result(error, html) {
|
||||
if (error) {
|
||||
var message = Inliner.errors[error.code] || error.message;
|
||||
console.error(message);
|
||||
var p = Promise.resolve(url);
|
||||
|
||||
if (argv.debug) {
|
||||
console.error(error.stack);
|
||||
}
|
||||
if (argv.useStdin) {
|
||||
p = new Promise(function (resolve) {
|
||||
process.stdin.resume();
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
var data = '';
|
||||
|
||||
console.log(html);
|
||||
});
|
||||
process.stdin.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
// checks for available update and returns an instance
|
||||
// note: we're doing this after we kick off inliner, since there's a
|
||||
// noticeable lag in boot because of it
|
||||
var defaults = require('lodash.defaults');
|
||||
var pkg = JSON.parse(readFileSync(__dirname + '/../package.json'));
|
||||
|
||||
require('update-notifier')({
|
||||
pkg: defaults(pkg, { version: '0.0.0' }),
|
||||
}).notify();
|
||||
|
||||
inliner.on('warning', function progress(event) {
|
||||
console.warn('warning: ' + event);
|
||||
});
|
||||
|
||||
if (argv.verbose) {
|
||||
inliner.on('progress', function progress(event) {
|
||||
console.error(event);
|
||||
});
|
||||
|
||||
inliner.on('jobs', function jobs(event) {
|
||||
console.error(event);
|
||||
process.stdin.on('end', function () {
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
p.then(function (source) {
|
||||
var inliner = new Inliner(source, argv, function result(error, html) {
|
||||
if (error) {
|
||||
var message = Inliner.errors[error.code] || error.message;
|
||||
console.error(message);
|
||||
|
||||
if (argv.debug) {
|
||||
console.error(error.stack);
|
||||
}
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(html);
|
||||
});
|
||||
|
||||
return inliner;
|
||||
}).then(function (inliner) {
|
||||
// checks for available update and returns an instance
|
||||
// note: we're doing this after we kick off inliner, since there's a
|
||||
// noticeable lag in boot because of it
|
||||
var defaults = require('lodash.defaults');
|
||||
var pkg = JSON.parse(readFileSync(__dirname + '/../package.json'));
|
||||
|
||||
require('update-notifier')({
|
||||
pkg: defaults(pkg, { version: '0.0.0' }),
|
||||
}).notify();
|
||||
|
||||
inliner.on('warning', function progress(event) {
|
||||
console.warn('warning: ' + event);
|
||||
});
|
||||
|
||||
if (argv.verbose) {
|
||||
inliner.on('progress', function progress(event) {
|
||||
console.error(event);
|
||||
});
|
||||
|
||||
inliner.on('jobs', function jobs(event) {
|
||||
console.error(event);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
@ -37,5 +37,7 @@ function options(args) {
|
||||
}
|
||||
argv.images = !argv.noimages;
|
||||
|
||||
argv.useStdin = !process.stdin.isTTY;
|
||||
|
||||
return argv;
|
||||
}
|
@ -3,6 +3,8 @@
|
||||
|
||||
$ inliner [flags] url-or-filename
|
||||
|
||||
Inliner also supports HTML being piped and will inline from STDIN.
|
||||
|
||||
Flags:
|
||||
|
||||
-n, --nocompress don't compress CSS or HTML - useful for debugging
|
||||
@ -22,5 +24,6 @@
|
||||
$ inliner -v https://twitter.com > twitter.html
|
||||
$ inliner -ni local-file.html > local-file.min.html
|
||||
$ inliner -e windows-1253 http://foofootos.gr > foofootos-utf8.html
|
||||
$ cat local-file.html | inliner
|
||||
|
||||
For more details see http://github.com/remy/inliner/
|
||||
|
65
lib/index.js
65
lib/index.js
@ -30,10 +30,17 @@ var taskRunner = Object.keys(tasks).reduce(function (acc, curr) {
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
function Inliner(url, options, callback) {
|
||||
// source is typicaly a URL, but can be a file location OR an HTML string
|
||||
function Inliner(source, options, callback) {
|
||||
var inliner = this;
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
// allow for source to be optional
|
||||
if (typeof source !== 'string') {
|
||||
callback = options;
|
||||
options = source;
|
||||
}
|
||||
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
@ -43,7 +50,20 @@ function Inliner(url, options, callback) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this.url = url;
|
||||
if (options.url) {
|
||||
this.url = options.url;
|
||||
}
|
||||
|
||||
if (options.filename) {
|
||||
this.filename = options.filename;
|
||||
}
|
||||
|
||||
if (options.source) {
|
||||
this.source = options.source;
|
||||
} else {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
this.callback = function wrapper(error, res) {
|
||||
// noop the callback once it's fired
|
||||
inliner.callback = function noop() {
|
||||
@ -75,7 +95,7 @@ function Inliner(url, options, callback) {
|
||||
|
||||
// this allows the user code to get the object back before
|
||||
// it starts firing events
|
||||
if (this.url) {
|
||||
if (this.source) {
|
||||
if (typeof setImmediate === 'undefined') {
|
||||
global.setImmediate = function setImmediatePolyfill(fn) {
|
||||
// :-/
|
||||
@ -109,9 +129,9 @@ Inliner.defaults = require('./defaults');
|
||||
// main thread of functionality that does all the inlining
|
||||
function main() {
|
||||
var inliner = this;
|
||||
var url = this.url;
|
||||
var url = this.source;
|
||||
|
||||
fs.exists(url)
|
||||
fs.exists(this.filename || url)
|
||||
.then(function exists(isFile) {
|
||||
if (!isFile) {
|
||||
throw new Error();
|
||||
@ -120,21 +140,34 @@ function main() {
|
||||
debug('inlining file');
|
||||
|
||||
inliner.isFile = true;
|
||||
return url;
|
||||
inliner.url = url; // this is a hack for the `resolve` function later on
|
||||
return inliner.get(this.filename || url, { encoding: 'binary' });
|
||||
})
|
||||
.catch(function isUrl() {
|
||||
// check for protocol on URL
|
||||
if (url.indexOf('http') !== 0) {
|
||||
url = 'http://' + url;
|
||||
// make the best guess as to whether we're working with a url
|
||||
if (inliner.url || url.indexOf('<') === -1) {
|
||||
url = inliner.url || inliner.source;
|
||||
// check for protocol on URL
|
||||
if (url.indexOf('http') !== 0) {
|
||||
url = 'http://' + url;
|
||||
}
|
||||
|
||||
inliner.url = url;
|
||||
|
||||
debug('inlining url');
|
||||
return inliner.get(url, { encoding: 'binary' });
|
||||
}
|
||||
|
||||
inliner.url = url;
|
||||
|
||||
debug('inlining url');
|
||||
return url;
|
||||
})
|
||||
.then(function (url) {
|
||||
return inliner.get(url, { encoding: 'binary' });
|
||||
// otherwise we're dealing with an inline string
|
||||
debug('inlining by string: ', inliner.source);
|
||||
inliner.url = '.';
|
||||
var res = {
|
||||
body: new Buffer(inliner.source),
|
||||
headers: {
|
||||
'content-type': 'text/html',
|
||||
},
|
||||
};
|
||||
return res;
|
||||
})
|
||||
.then(inliner.jobs.add('html'))
|
||||
.then(function processHTML(res) {
|
||||
|
@ -13,6 +13,8 @@ test.createStream().pipe(tapSpec()).pipe(process.stdout);
|
||||
test('inliner core functions', function coreTests(t) {
|
||||
var Inliner = require('../');
|
||||
|
||||
t.plan(4);
|
||||
|
||||
t.equal(typeof Inliner, 'function', 'Inliner is a function');
|
||||
t.equal(Inliner.version,
|
||||
require('../package.json').version, 'should have version');
|
||||
@ -20,7 +22,10 @@ test('inliner core functions', function coreTests(t) {
|
||||
var inliner = new Inliner();
|
||||
t.ok(inliner, 'inline is instantiated');
|
||||
|
||||
t.end();
|
||||
var roundtripHTML = '<!DOCTYPE html><html></html>';
|
||||
new Inliner(roundtripHTML, function(error, html) {
|
||||
t.equal(html, roundtripHTML, 'recognizes HTML as main input');
|
||||
});
|
||||
});
|
||||
|
||||
test('inliner fixtures', function fixtureTests(t) {
|
||||
|
Loading…
Reference in New Issue
Block a user