Skip to content

Commit

Permalink
bin: Add he --encode --everything 'abc'
Browse files Browse the repository at this point in the history
Ref. #12.
  • Loading branch information
mathiasbynens committed Aug 7, 2013
1 parent cb44712 commit eed4e4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 22 additions & 8 deletions bin/he
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var main = function() {
var option = strings[0];
var count = 0;

if (/^(?:-h|--help|undefined)$/.test(option)) {
log(
Expand All @@ -22,7 +23,7 @@
log([
'\nUsage:\n',
'\the [--escape] string',
'\the [--encode] [--use-named-refs] string',
'\the [--encode] [--use-named-refs] [--everything] string',
'\the [--decode] [--attribute] [--strict] string',
'\the [-v | --version]',
'\the [-h | --help]',
Expand Down Expand Up @@ -53,6 +54,11 @@
options.useNamedReferences = true;
return;
}
if (string == '--everything') {
action = 'encode';
options.encodeEverything = true;
return;
}
if (string == '--decode') {
action = 'decode';
return;
Expand All @@ -77,6 +83,7 @@
try {
result = he[action](string, options);
log(result);
count++;
} catch(error) {
log(error.message + '\n');
log('Error: failed to %s.', action);
Expand All @@ -90,25 +97,32 @@
return process.exit(1);
}
});
if (!count) {
log('Error: he requires a string argument.');
log('Try `he --help` for more information.');
return process.exit(1);
}
// Return with exit status 0 outside of the `forEach` loop, in case
// multiple strings were passed in.
return process.exit(0);

};

if (stdin.isTTY) {
// handle shell arguments
main();
} else {
// Either the script is called from within a non-TTY context,
// or `stdin` content is being piped in.
if (!process.stdout.isTTY) { // called from a non-TTY context
// Either the script is called from within a non-TTY context, or `stdin`
// content is being piped in.
if (!process.stdout.isTTY) {
// The script was called from a non-TTY context. This is a rather uncommon
// use case we don’t actively support. However, we don’t want the script
// to wait forever in such cases, so…
timeout = setTimeout(function() {
// if no piped data arrived after a while, handle shell arguments
// …if no piped data arrived after a whole minute, handle shell
// arguments instead.
main();
}, 250);
}, 60000);
}

data = '';
stdin.on('data', function(chunk) {
clearTimeout(timeout);
Expand Down
4 changes: 3 additions & 1 deletion man/he.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.br
.Op Fl -encode Ar string
.br
.Op Fl -encode Fl -use-named-refs Ar string
.Op Fl -encode Fl -use-named-refs Fl -everything Ar string
.br
.Op Fl -decode Ar string
.br
Expand All @@ -31,6 +31,8 @@ Take a string of text and escape it for use in text contexts in XML or HTML docu
Take a string of text and encode any symbols that aren't printable ASCII symbols and that can be replaced with character references. For example, it would turn `©` into `©`, but it wouldn't turn `+` into `+` since there is no point in doing so. Additionally, it replaces any remaining non-ASCII symbols with a hexadecimal escape sequence (e.g. `𝌆`). The return value of this function is always valid HTML.
.It Sy "--encode --use-named-refs"
Enable the use of named character references (like `©`) in the output. If compatibility with older browsers is a concern, don't use this option.
.It Sy "--encode --everything"
Encode every symbol in the input string, even safe printable ASCII symbols.
.It Sy "--decode"
Takes a string of HTML and decode any named and numerical character references in it using the algorithm described in the HTML spec.
.It Sy "--decode --attribute"
Expand Down

0 comments on commit eed4e4f

Please sign in to comment.