-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole2html.cpp
More file actions
executable file
·604 lines (474 loc) · 12.9 KB
/
console2html.cpp
File metadata and controls
executable file
·604 lines (474 loc) · 12.9 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#if 0
input="$(readlink -f "$(command -v "$0")")"
output="${0%.*}"
#// Compile ourselves
if [ "$input" -nt "$output" ] ; then
printf 'Compiling %s...\n' "${output##*/}" >&2
${CXX:-g++} -std=c++11 -Wall -Wextra "$input" -o "$output" > /dev/null < /dev/null || exit 1
fi
#// Run the executable
exec "$output" "$@"
exit
#endif // 0
/*
* Copyright (C) 2011-2014 Daniel Scharrer
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the author(s) be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <stack>
#include <utility>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
static bool is_end_char(char c) {
return (c >= 64 && c < 127);
}
static void print_escaped(char cc) {
unsigned char c = cc;
if(c == '\\') {
std::cerr << "\\\\";
} else if(c == '\e') {
std::cerr << "\\e";
} else if(c == '\n') {
std::cerr << "\\n";
} else if(c == '\r') {
std::cerr << "\\r";
} else if(c == '\b') {
std::cerr << "\\b";
} else if(c == '\a') {
std::cerr << "\\a";
} else if(c == '\0') {
std::cerr << "\\0";
} else if(c == '\t') {
std::cerr << "\\t";
} else if(c == '"') {
std::cerr << "\\\"";
} else if(c <= 32 && c != ' ') {
std::cerr << "\\x" << std::setfill('0') << std::setw(2) << int(c);
} else {
std::cerr << (char)c;
}
}
static void print_escaped(const char * str, std::size_t length) {
for(std::size_t i = 0; i < length; i++) {
print_escaped(str[i]);
}
}
typedef std::size_t command_class;
typedef std::size_t command;
typedef std::map<command_class, command> commands_t;
command_class get_command_class(command cmd) {
if(cmd == 2 || cmd == 21 || cmd == 22) {
return 1; // Boldness
} else if(cmd == 6 || cmd == 25) {
return 5; // Blink
} else if(cmd >= 10 && cmd <= 19) {
return 10; // Font
} else if(cmd == 20 || cmd == 23) {
return 3; // Italic/Fraktur
} else if(cmd == 24) {
return 4; // Underlined
} else if(cmd == 27) {
return 7; // Negative
} else if(cmd == 28) {
return 8; // Conceal
} else if(cmd == 29) {
return 9; // Strike-through
} else if((cmd > 30 && cmd <= 39) || (cmd >= 90 && cmd <= 99)) {
return 30; // Foreground color
} else if((cmd > 40 && cmd <= 49) || (cmd >= 100 && cmd <= 109)) {
return 40; // Background color
} else if(cmd == 52 || cmd == 54) {
return 52; // Framed / encircled
} else if(cmd == 55) {
return 53; // Overlined
} else {
return cmd;
}
}
class output {
public:
virtual ~output() { };
virtual void text(const char * text, std::size_t n) = 0;
virtual void color(commands_t && commands) = 0;
virtual void reset() = 0;
virtual void begin_link(const char * url, std::size_t n) = 0;
virtual void end_link() = 0;
};
template <typename K, typename V1, typename V2, typename Compare>
bool is_subset(const std::map<K, V1, Compare> & a, const std::map<K, V2, Compare> & b) {
if(a.size() > b.size()) {
return false; // a is larger, cannot be subset
}
auto ia = a.begin();
auto ib = b.begin();
// technically, a.key_comp() and b.key_comp() might not be equivalent
Compare comp{}; // cannot use value_comp() as V1 and V2 might differ
while(ia != a.end()) {
while(ib != b.end() && comp(ib->first, ia->first)) {
ib++;
}
if(ib == b.end() || comp(ia->first, ib->first)) {
return false; // b does not contain *ia
}
// *ia == *ib
ia++, ib++;
}
return true;
}
typedef std::map<command, const char *> tags_t;
class html_format : public output {
const commands_t defaults = []() {
commands_t defaults;
defaults[1] = 21; // Not bold, not faint
defaults[3] = 23; // Not italic, not fraktur
defaults[4] = 24; // Not underlined
defaults[5] = 25; // Not blinking
defaults[7] = 27; // Not negative
defaults[8] = 28; // Not conceiled
defaults[9] = 29; // No strike-through
defaults[10] = 10; // Default font
defaults[30] = 39; // Default foreground color
defaults[40] = 49; // Default background color
defaults[52] = 54; // Not framed, not encircled
defaults[53] = 55; // Not overlined
return defaults;
}();
const tags_t tags = []() {
tags_t tags;
tags[1] = "b";
tags[3] = "i";
tags[4] = "u";
tags[5] = "blink";
tags[9] = "strike";
return tags;
}();
typedef std::pair<commands_t, commands_t> frame_t;
std::stack<frame_t> stack;
void close() {
const char * tag = "span";
if(stack.top().first.size() == 1) {
auto it = tags.find(stack.top().first.begin()->second);
if(it != tags.end()) {
tag = it->second;
}
}
std::cout << "</" << tag << ">";
stack.pop();
}
public:
html_format() { }
void text(const char * text, std::size_t length) {
const char * end = text + length;
while(true) {
const char * c = std::find_if(text, end, [](char c) {
return c == '&' || c == '<' || c == '>';
});
if(c == end) {
std::cout.write(text, length);
break;
} else {
std::cout.write(text, c - text);
text = c + 1;
length = end - text;
}
switch(*c) {
case '&': std::cout << "&"; break;
case '<': std::cout << "<"; break;
case '>': std::cout << ">"; break;
}
}
}
void color(commands_t && cmds) {
while(!stack.empty() && !stack.top().first.empty() && is_subset(stack.top().first, cmds)) {
// Completely overwrites stack frame, can close now
close();
}
commands_t current;
if(stack.empty()) {
current = defaults;
} else {
current = stack.top().second;
}
for(commands_t::iterator it = cmds.begin(); it != cmds.end(); ) {
auto cit = current.find(it->first);
if(cit != current.end() && cit->second == it->second) {
// No change
it = cmds.erase(it);
continue;
}
// Record the changed state
current[it->first] = it->second;
auto tit = tags.find(it->second);
if(tit != tags.end()) {
// We have a special tag for this type
std::cout << "<" << tit->second << ">";
commands_t cmd;
cmd[it->first] = it->second;
stack.emplace(std::move(cmd), current);
it = cmds.erase(it);
continue;
}
it++;
}
if(cmds.empty()) {
// No commands left
return;
}
std::cout << "<span class=\"";
bool first = true;
for(std::pair<command_class, command> cmd : cmds) {
if(first) {
first = false;
} else {
std::cout << ' ';
}
std::cout << 'c' << cmd.second;
}
std::cout << "\">";
stack.emplace(std::move(cmds), std::move(current));
}
void reset() {
while(!stack.empty() && !stack.top().first.empty()) {
close();
}
if(!stack.empty()) {
color(commands_t{defaults});
}
}
void begin_link(const char * url, std::size_t n) {
commands_t current;
if(stack.empty()) {
current = defaults;
} else {
current = stack.top().second;
}
std::cout << "<a href=\"";
const char * end = url + n;
while(true) {
const char * c = std::find_if(url, end, [](char c) {
return c == '"' || c == '\\';
});
if(c == end) {
std::cout.write(url, n);
break;
} else {
std::cout.write(url, c - url);
url = c + 1;
n = end - url;
}
std::cout << '\\' << *c;
}
std::cout << "\">";
stack.emplace(commands_t{}, current);
}
void end_link() {
if(stack.empty()) {
return;
}
commands_t cmds;
while(!stack.empty() && !stack.top().first.empty()) {
cmds.insert(stack.top().first.begin(), stack.top().first.end());
close();
}
if(!stack.empty()) {
std::cout << "</a>";
stack.pop();
}
// Restore current color
color(std::move(cmds));
}
};
class plain_format : public output {
public:
void text(const char * text, std::size_t n) {
std::cout.write(text, std::streamsize(n));
}
void color(commands_t && cmds) {
(void)cmds;
// No colors supported
}
void reset() {
// Nothing to reset
}
void begin_link(const char * url, std::size_t n) {
(void)url, (void)n;
// Links not supported
}
void end_link() {
// Links not supported
}
};
int main(int argc, char * argv[]) {
const char * format = "html";
if(argc > 1) {
if(argc == 3 && boost::equals(argv[1], "--format")) {
format = argv[2];
} else {
std::cerr << "Usage: console2html [--format <format>]\n";
return 1;
}
}
std::unique_ptr<output> out;
if(boost::equals(format, "html")) {
out.reset(new html_format);
} else if(boost::equals(format, "plain")) {
out.reset(new plain_format);
} else {
std::cerr << "Unknown format: " << format << "\n";
return 1;
}
std::size_t line = 0;
std::string buf;
commands_t cmds;
bool close = false;
std::size_t newlines = 0;
std::string link;
bool in_link = false;
while(!std::cin.eof()) {
getline(std::cin, buf);
size_t p = 0;
while(true) {
size_t i = buf.find('\x1B', p);
// Ignore carriage return at start/end end of lines
std::size_t end = (i == std::string::npos) ? buf.length() : i;
while(p != end && buf[p] == '\r') p++;
while(p != end && buf[end - 1] == '\r') end--;
// We have actual text!
if(end != p) {
// Flush the command buffer: resets
if(close) {
out->reset();
close = false;
}
// Print newlines
for(; newlines; newlines--) {
out->text("\n", 1);
}
// Flush the command buffer: new colors
if(!cmds.empty()) {
out->color(std::move(cmds));
cmds.clear();
}
if(!link.empty()) {
out->begin_link(link.data(), link.length());
in_link = true;
}
// Print plain text
out->text(buf.data() + p, end - p);
}
if(i == std::string::npos) {
break;
}
p = i + 1;
if(p >= buf.length()) {
std::cerr << line << ':' << i << ": line terminated after escape code\n";
break;
}
char start = buf[p];
if(start == '=') {
// msvc/wine generates this
p++;
continue;
} else if(start != '[' && start != ']') {
std::cerr << line << ':' << i << ": invalid escape code start: \"\\e";
print_escaped(buf[p]);
std::cerr << "\"\n";
continue;
}
p++;
size_t e = p;
while(e < buf.length()) {
char c = buf[e];
e++;
if(start == ']' ? c == '\a' : is_end_char(c)) {
break;
}
}
if(start != ']' && p < e && buf[e - 1] == 'm') {
// Color command
while(p < e) {
i = buf.find_first_not_of("0123456789", p);
if(i == std::string::npos) {
std::cerr << line << ':' << i << ": line terminated after escape code\n";
break;
}
std::size_t cmd;
try {
cmd = (i == p) ? 0 : boost::lexical_cast<std::size_t>(buf.data() + p, i - p);
} catch(...) {
std::cerr << line << ':' << i << ": bad color number: \"";
print_escaped(buf.data() + p, i - p);
std::cerr << "\"\n";
}
if(cmd == 0) {
cmds.clear();
close = true;
} else {
cmds[get_command_class(cmd)] = cmd;
}
p = i + 1;
if(buf[i] != 'm' && buf[i] != ';') {
std::cerr << line << ':' << i << ": invalid color char: '";
print_escaped(buf[i]);
std::cerr << "'\n";
}
}
} else if(start != ']' && e - p == 3 && buf[p] == '?' && buf[p + 1] == '1' && buf[p + 2] == 'h') {
// msvc/wine generates this
} else if(start != ']' && e - p == 1 && buf[p] == 'K') {
/*
* gcc 4.10 generates this
* This is meant to clear the background of wrapped lines as some terminals
* fill them with the current background color.
* Unfortunately they emit this even if they never changed the background color!
* As we don't wrap lines it can be safely ignored.
*/
} else if(start == ']' && e - p >= 4 && buf[p] == '8' && buf[p + 1] == ';' && buf[p + 2] == ';'
&& buf[e - 1] == '\a') {
if(in_link) {
out->end_link();
in_link = false;
}
link = buf.substr(p + 3, e - p - 4);
} else {
// Other command
std::cerr << line << ':' << i << ": ignoring unknown escape code: \"\\e[";
print_escaped(buf.data() + p, e - p);
std::cerr << "\"\n";
}
p = e;
}
newlines++;
line++;
}
if(in_link) {
out->end_link();
}
out->reset();
// Ignore trailing commands!
for(; newlines > 1; newlines--) {
out->text("\n", 1);
}
return 0;
}