Skip to content

Commit

Permalink
feat: allow user to specific encoding
Browse files Browse the repository at this point in the history
Support for manually specifying the source encoding using iconv-lite to convert to utf-8 from the specified encoding.
  • Loading branch information
m000 authored and remy committed Aug 23, 2015
1 parent f720629 commit 715678c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
11 changes: 7 additions & 4 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/usr/bin/env node

var minimist = require('minimist');
var readFileSync = require('fs').readFileSync;

var argv = require('minimist')(process.argv.slice(2), opts={
'boolean': ['V', 'h', 'd', 'v', 'i', 'n',],
'alias': {
var argv = minimist(process.argv.slice(2), opts = {
boolean: ['V', 'h', 'd', 'v', 'i', 'n',],
string: ['e',],
alias: {
V: 'version',
h: 'help',
d: 'debug',
v: 'verbose',
i: 'images',
n: 'nocompress',
e: 'encoding',
},
});

Expand Down Expand Up @@ -77,4 +80,4 @@ if (argv.verbose) {
inliner.on('jobs', function jobs(event) {
console.error(event);
});
}
}
7 changes: 6 additions & 1 deletion docs/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@

$ inliner [options] url-or-filename

Options:
Flags:

-h, --help output usage information
-V, --version output the version number
-v, --verbose echo on STDERR the progress of inlining
-n, --nocompress don't compress CSS or HTML - useful for debugging
-i, --images don't encode images - keeps files size small, but more requests

Options:

-e, --encoding attempt to transcode the page from the specified encoding to utf-8

Examples:

$ 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

For more details see http://github.com/remy/inliner/
3 changes: 2 additions & 1 deletion lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = function get(url, options) {
debug('inliner.get url: %s', url);

var settings = assign({}, options, {
encoding: null,
followRedirect: true,
});

Expand All @@ -63,4 +64,4 @@ module.exports = function get(url, options) {
});

return cache[url];
};
};
25 changes: 23 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var assign = require('lodash.assign');
var forEach = require('lodash.foreach');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var findAssets = require('./find-assets');
var iconv = require('iconv-lite');

function Inliner(url, options, callback) {
var inliner = this;
Expand Down Expand Up @@ -129,10 +130,30 @@ function main() {
inliner.jobs.done.html();
debug('processing HTML');

var todo = findAssets(res.body);
if (inliner.options.encoding !== undefined) {
// force transcoding to utf-8 from specified encoding
// todo: check the meta header to automatically pick encoding
var body = iconv.encode(
iconv.decode(res.body, inliner.options.encoding),
'utf-8'
);
} else {
var body = res.body;
}

var todo = findAssets(body);
var $ = todo.$;
delete todo.$;

if (inliner.options.encoding !== undefined) {
// when transcoding remove any meta tags setting the charset
$('meta').each(function charMeta(index) {
if ($(this).attr('content').toLowerCase().indexOf('charset=')) {
$(this).remove();
}
});
}

forEach(todo, function forEach(todo, key) {
if (key === 'images' && !inliner.options.images) {
// skip images if the user doesn't want them
Expand Down Expand Up @@ -346,4 +367,4 @@ function completeJob(type) {

// this allows me to include addJob as part of a promise chain
return arguments[1];
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"cheerio": "^0.19.0",
"debug": "^2.2.0",
"es6-promise": "^2.3.0",
"iconv-lite": "^0.4.11",
"lodash.assign": "^3.2.0",
"lodash.defaults": "^3.1.2",
"lodash.foreach": "^3.0.3",
Expand Down

0 comments on commit 715678c

Please sign in to comment.