-
Notifications
You must be signed in to change notification settings - Fork 6
/
ansi2html.js
110 lines (97 loc) · 2.79 KB
/
ansi2html.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
define(function (require, exports, module) {
//Fonte: https://github.com/agnoster/ansi2html
(function(){
function declare(module_name, exports) {
if (typeof module != 'undefined') module.exports = exports
else window[module_name] = exports
}
function ansi2html(str) {
var props = {}
, open = false
var stylemap =
{ bold: "font-weight"
, underline: "text-decoration"
, color: "color"
, background: "background"
}
function style() {
var key, val, style = []
for (var key in props) {
val = props[key]
if (!val) continue
if (val == true) {
style.push(stylemap[key] + ':' + key)
} else {
style.push(stylemap[key] + ':' + val)
}
}
return style.join(';')
}
function tag(code) {
var i
, tag = ''
, n = ansi2html.table[code]
if (open) tag += '</span>'
open = false
if (n) {
for (i in n) props[i] = n[i]
tag += '<span style="' + style() + '">'
open = true
} else {
props = {}
}
return tag
}
return str.replace(/\[(\d+;)?(\d+)*m/g, function(match, b1, b2) {
var i, code, res = ''
if (b2 == '' || b2 == null) b2 = '0'
for (i = 1; i < arguments.length - 2; i++) {
if (!arguments[i]) continue
code = parseInt(arguments[i])
res += tag(code)
}
return res
}) + tag()
}
/* not implemented:
* italic
* blink
* invert
* strikethrough
*/
ansi2html.table =
{ 0: null
, 1: { bold: true }
, 3: { italic: true }
, 4: { underline: true }
, 5: { blink: true }
, 6: { blink: true }
, 7: { invert: true }
, 9: { strikethrough: true }
, 23: { italic: false }
, 24: { underline: false }
, 25: { blink: false }
, 27: { invert: false }
, 29: { strikethrough: false }
, 30: { color: 'black' }
, 31: { color: 'red' }
, 32: { color: 'green' }
, 33: { color: 'yellow' }
, 34: { color: 'blue' }
, 35: { color: 'magenta' }
, 36: { color: 'cyan' }
, 37: { color: 'white' }
, 39: { color: null }
, 40: { background: 'black' }
, 41: { background: 'red' }
, 42: { background: 'green' }
, 43: { background: 'yellow' }
, 44: { background: 'blue' }
, 45: { background: 'magenta' }
, 46: { background: 'cyan' }
, 47: { background: 'white' }
, 49: { background: null }
}
declare('ansi2html', ansi2html)
})()
});