-
Notifications
You must be signed in to change notification settings - Fork 2
/
retro.js
170 lines (140 loc) · 4.21 KB
/
retro.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// TODO: make streamed
const http = require('http');
const h2t = require('./html.js');
let log = 0;
//let url = 'http://www.google.com';
let url = '';
url = 'http://yesco.org/'; // TODO: 404
// TODO: write redirector? or just call wget?
url = 'http://runeberg.org/';
url = 'http://yesco.org/index.html';
url = 'http://www.lysator.liu.se/';
url = process.argv[2] || url;
function wget(url, cb) {
let chunks = [];
http.get(url, (res)=>{
res.on('data', (data)=>{
// TODO: this assumes UTF-8
// can't recover iso-8895-1
chunks.push(data.toString());
if (log) console.log("CHUNK:", data.toString());
});
res.on('end', ()=>{
if (log) console.log("END:");
cb(null, chunks.join(''));
});
});
}
let debug = 1;
function teletext2ansi(t) {
// - \u0320 non-breakable space
// - \u030a force newline
// (These retain color after new line)
// - \u0102 set green START
// - \u0202 set green END (matching)
// - \u0112 set green back START
// - \u0212 set green back END (matching)
let stack = [];
let bg = '\u0117'; // white text
let fg = '\u0100'; // black back
let normal = '\u0110\u0102'; // green on black
let bold = '[1m';
let nobold = '[22m';
function ord(ch) {
return ch.charCodeAt(0);
}
function color(ch) {
let n = ord(ch);
let c = n & 0xf;
// TODO: these were intially made to occupy one space on screen ala oric attributes...
//return `<0x${n.toString(16)}>`;
if (1)
if (c >= 0 && c <= 8) {
// x = `<0x${n.toString(16)}>`;
if ((n & 0x10) == 0x00) {
fg = ch;
return (debug?`[fg:${c}] `:(c&&c!=7?bold:nobold)+'[3'+c+'m');
return (debug?`[fg:${c}] `:bold+'[3'+c+'m');
}
if ((n & 0x10) == 0x10) {
bg = ch;
return (debug?`[bg:${c}] `:'[4'+c+'m');
}
}
return `<?: 0x${n.toString(16)}>`;
}
function start(ch) {
stack.push([ch, bg, fg]);
return color(ch);
}
function end(ch) {
let [_ch,_bg,_fg] = stack.pop();
// TODO: make sure _ch matches ch
// TODO: only if changed
if (debug) {
return ' R:'+color(_bg)+' R:'+color(_fg);
} else {
return color(_bg)+color(_fg);
}
bg = _bg; fg = _fg;
}
function startend(c) {
return c.replace(/([\u0100-\u0107\u0110-\u0117])/g, start)
.replace(/([\u0200-\u0207\u0210-\u0217])/g, end);
}
return (bg+fg+"\n"+t+normal+'\n')
.replace(/([\u0100-\u0217])/g, startend)
// collapse white space
.replace(/ +/g, ' ')
// at this stage \n newlines do matter
// TODO: However, they are intermixed with
// ansi control-codes.
.replace(/\n+/g, "\n")
.replace(/ *\n* *\u0320/g, ' ')
// keep hard spaces
.replace(/\u0320/g, (debug?'_':' ')) // hard-space
.replace(/[\n ]*\u030a[ \n]*/g, (debug?"\\N\n":"\n")) // hard-newline
// \n+ maybe not good for <pre>
.replace(/\n+/g, '[K\n') // workaround eol-bug! https://stackoverflow.com/questions/53740460/ansi-escape-code-weird-behavior-at-end-of-line
;
}
wget(url, (status, h)=>{
if (log) console.log("WGET: f", h);
let nid = 0;
let hclean = h
.replace(/([\u0100-\u0107\u0110-\u0117])/g, '<?>')
.replace(/([\u0200-\u0207\u0210-\u0217])/g, '<?>');
let tele = h2t.HTML2TELETEXT(nid, hclean);
if (log) {
process.stdout.write("\n\n---TELE---\n\n");
process.stdout.write(tele);
process.stdout.write("\n");
}
if (log) {
debug = 1;
let dansi = teletext2ansi(tele);
process.stdout.write("\n\n---debugANSI---\n\n");
process.stdout.write(dansi);
process.stdout.write("\n\n");
}
debug = 0;
let ansi = teletext2ansi(tele);
let fg = 3, bg = 0;
// process.stdout.write('[H[2J[3J'); // cls
// process.stdout.write('[1m[3'+4+'m');
//process.stdout.write('[1m[3'+1+'m');
//process.stdout.write('[4'+3+'m');
// process.stdout.write('./retro ');
process.stdout.write('[3'+bg+'m');
process.stdout.write('[4'+fg+'m');
//process.stdout.write('./retro ');
process.stdout.write('<retro> ');
process.stdout.write('[3'+fg+'m');
process.stdout.write('[4'+bg+'m');
if (log) {
process.stdout.write("\n\n---ANSI---\n\n\n\n");
}
process.stdout.write(' '+url+'\n');
process.stdout.write(ansi);
process.stdout.write("\n\n");
});