Skip to content

Commit

Permalink
Improve error handling of html-tty(1) executable
Browse files Browse the repository at this point in the history
It's too easy to forget this program is a post-processor, and pass files
which haven't yet been processed by troff(1). Diagnostics will receive a
more detailed explanation in the program's (yet-to-be-written) man page.
  • Loading branch information
Alhadis committed Mar 14, 2019
1 parent f73eb6a commit 761b893
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions bin/html-tty
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ new Promise(resolve => {
});
}
}).then(data => {

// If input isn't ditroff, bail with a meaningful error message
if(!/^x\s*T\s+\S/.test(data.replace(/^\s*#.*$/gm, "").trim())){
const reason = /(?:^|\n)\.\S+/.test(data)
? "input appears to be unprocessed Roff source"
: "input does not contain a ditroff(7) header";
process.stderr.write(`html-tty: Aborting, ${reason}\n`);
process.exit(2);
}

const htmlTTY = new (require("..").TTYRenderer)();
const isRaw = /\x08/.test(data);
let output = htmlTTY.process(data, isRaw);
if(process.stdout.isTTY)
output = output
.replace(/^\n+/, "")
.replace(/<b>/g, "\x1B[1m")
.replace(/<u>/g, "\x1B[4m")
.replace(/<\/b>/g, "\x1B[22m")
.replace(/<\/u>/g, "\x1B[24m")
.replace(/<b data-sgr="(\d+)"[^>]+>/g, "\x1B[1;38;5;$1m")
.replace(/<u data-sgr="(\d+)"[^>]+>/g, "\x1B[4;38;5;$1m")
.replace(/<span data-sgr="(\d+)"[^>]+>/g, "\x1B[38;5;$1m")
.replace(/<\/span>/g, "\x1B[39m")
.replace(/<a[^>]+>|<\/a>/g, "")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&amp;/g, "&")
.replace(/&#(\d+);/g, (_,c) => String.fromCharCode(c));
.replace(/^\n+/, "")
.replace(/<b>/g, "\x1B[1m")
.replace(/<u>/g, "\x1B[4m")
.replace(/<\/b>/g, "\x1B[22m")
.replace(/<\/u>/g, "\x1B[24m")
.replace(/<b data-sgr="(\d+)"[^>]+>/g, "\x1B[1;38;5;$1m")
.replace(/<u data-sgr="(\d+)"[^>]+>/g, "\x1B[4;38;5;$1m")
.replace(/<span data-sgr="(\d+)"[^>]+>/g, "\x1B[38;5;$1m")
.replace(/<\/span>/g, "\x1B[39m")
.replace(/<a[^>]+>|<\/a>/g, "")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&amp;/g, "&")
.replace(/&#(\d+);/g, (_, char) => String.fromCharCode(char));
process.stdout.write(output + "\n");
}).catch(error => {
console.error(error);
Expand Down

0 comments on commit 761b893

Please sign in to comment.