-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtml-tty
executable file
·49 lines (48 loc) · 1.61 KB
/
html-tty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env node
"use strict";
new Promise((resolve, reject) => {
const fs = require("fs");
if(process.argv[2])
resolve(fs.readFileSync(process.argv[2], "utf8"));
else{
let input = "";
process.stdin.setEncoding("UTF8");
process.stdin.on("error", error => reject(error));
process.stdin.on("readable", () => {
const chunk = process.stdin.read();
null !== chunk ? input += chunk : resolve(input);
});
}
}).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(/</g, "<")
.replace(/>/g, ">")
.replace(/&/g, "&")
.replace(/&#(\d+);/g, (_, char) => String.fromCharCode(char));
process.stdout.write(output + "\n");
}).catch(error => {
console.error(error);
process.exit(1);
});