mirror of
https://github.com/tauri-apps/tauri-inliner.git
synced 2026-02-04 02:31:19 +01:00
feat: allow user to specific encoding
Support for manually specifying the source encoding using iconv-lite to convert to utf-8 from the specified encoding.
This commit is contained in:
committed by
Remy Sharp
parent
f720629d0d
commit
715678cfaa
11
cli/index.js
11
cli/index.js
@@ -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',
|
||||
},
|
||||
});
|
||||
|
||||
@@ -77,4 +80,4 @@ if (argv.verbose) {
|
||||
inliner.on('jobs', function jobs(event) {
|
||||
console.error(event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
$ inliner [options] url-or-filename
|
||||
|
||||
Options:
|
||||
Flags:
|
||||
|
||||
-h, --help output usage information
|
||||
-V, --version output the version number
|
||||
@@ -11,9 +11,14 @@
|
||||
-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/
|
||||
|
||||
@@ -41,6 +41,7 @@ module.exports = function get(url, options) {
|
||||
debug('inliner.get url: %s', url);
|
||||
|
||||
var settings = assign({}, options, {
|
||||
encoding: null,
|
||||
followRedirect: true,
|
||||
});
|
||||
|
||||
@@ -63,4 +64,4 @@ module.exports = function get(url, options) {
|
||||
});
|
||||
|
||||
return cache[url];
|
||||
};
|
||||
};
|
||||
|
||||
25
lib/index.js
25
lib/index.js
@@ -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;
|
||||
@@ -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
|
||||
@@ -346,4 +367,4 @@ function completeJob(type) {
|
||||
|
||||
// this allows me to include addJob as part of a promise chain
|
||||
return arguments[1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user