-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes2htm.go
246 lines (223 loc) · 4.43 KB
/
aes2htm.go
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
// https://en.wikipedia.org/wiki/ANSI_escape_code
package main
import (
"bufio"
"errors"
"fmt"
"io"
"strings"
"unicode/utf8"
)
// Aes2Htm is the main struct of Converting from
// ANSI escape sequences to HTML
type Aes2Htm struct {
w io.Writer // where to write html content
br *bufio.Reader // buffered reader wrapper for input
out func(s string) // wrapper for write
st State // current attribute state
stb State // backup attribute state
openTags int // open tags count
}
// NewAes2Htm creates a new Aes2Htm struct
func NewAes2Htm(w io.Writer) *Aes2Htm {
ah := &Aes2Htm{}
ah.w = w
ah.out = func(s string) {
ah.w.Write([]byte(s))
}
ah.st.Init()
ah.stb.Init()
return ah
}
// inputPlain consumes plain utf8 characteres
// and do html escape when needed
func (o *Aes2Htm) inputPlain(c byte) error {
if c < 128 {
switch c {
case '<':
o.out("<")
case '>':
o.out(">")
default:
o.out(fmt.Sprintf("%c", c))
}
return nil
}
o.br.UnreadByte()
r, s, er := o.br.ReadRune()
if r == utf8.RuneError || s < 1 || er != nil {
return errors.New("invalid rune")
}
o.out(fmt.Sprintf("%c", r))
return nil
}
// handleCSI handles `\033[` sequence
func (o *Aes2Htm) handleCSI() error {
var er error
var c byte
// backup state
o.stb = o.st
st := &o.st
hasNum := false
n := 0
ns := []int{}
for {
c, er = o.br.ReadByte()
if er != nil {
return er
}
if c >= '0' && c <= '9' {
if !hasNum {
hasNum = true
n = 0
}
n *= 10
n += int(c - '0')
continue
} else if c == ';' {
if hasNum {
ns = append(ns, n)
hasNum = false
}
continue
} else if c == 'm' {
if hasNum {
ns = append(ns, n)
hasNum = false
}
if len(ns) == 0 {
ns = append(ns, 0)
}
for i := 0; i < len(ns); i++ {
n = ns[i]
if n == 0 {
st.Init()
} else if n == 1 {
st.bold = true
} else if n == 3 {
st.italic = true
} else if n == 4 {
st.underline = true
} else if n == 5 || n == 6 {
st.blink = true
} else if 30 <= n && n <= 37 {
st.fgcolor.SetIndex(n - 30)
} else if n == 39 {
st.fgcolor.SetNone()
} else if 40 <= n && n <= 47 {
st.bgcolor.SetIndex(n - 40)
} else if n == 49 {
st.bgcolor.SetNone()
} else if n == 38 || n == 48 {
// 5;n or 2;r;g;b
if i++; i >= len(ns) {
return fmt.Errorf("expect color")
}
switch ns[i] {
case 5:
if i++; i >= len(ns) {
return fmt.Errorf("expect color")
}
index := ns[i]
if n == 38 {
st.fgcolor.SetIndex(index)
} else if n == 48 {
st.bgcolor.SetIndex(index)
}
case 2:
if i+3 >= len(ns) {
return fmt.Errorf("expect color")
}
r := ns[i+1]
g := ns[i+2]
b := ns[i+3]
if n == 38 {
st.fgcolor.SetRGB(r, g, b)
} else if n == 48 {
st.bgcolor.SetRGB(r, g, b)
}
i += 3
}
} else if 90 <= n && n <= 97 {
st.fgcolor.SetIndex(n - 90 + 8)
} else if 100 <= n && n <= 107 {
st.bgcolor.SetIndex(n - 100 + 8)
} else {
return fmt.Errorf("invalid code: %d", n)
}
}
break
} else if c == '?' {
// skip
for {
c, er := o.br.ReadByte()
if er != nil {
return errors.New("invalid")
}
if c >= '0' && c <= '9' {
continue
} else {
break
}
}
break
} else {
s := fmt.Sprintf("invalid terminate: %v %c", ns, c)
return errors.New(s)
}
}
// if some changes have been made
if st.AnyChange(&o.stb) {
// some attribute is closed
if st.AnyClose(&o.stb) {
o.out(strings.Repeat("</span>", o.openTags))
o.openTags = 0
}
// re-open attribute output
if !st.Empty() {
o.out("<span")
st.WriteStyles(o.w, &o.stb)
st.WriteClasses(o.w, &o.stb)
o.out(">")
o.openTags++
}
}
return nil
}
// Input does input processing
func (o *Aes2Htm) Input(r io.Reader) error {
var er error
var c byte
o.openTags = 0
o.st.Init()
o.stb.Init()
o.br = bufio.NewReader(r)
br := o.br
for {
c, er = br.ReadByte()
if er != nil {
if er == io.EOF {
return nil
}
return er
}
if c == '\033' {
c, er = br.ReadByte()
if er != nil {
return er
}
switch c {
case '[':
if er = o.handleCSI(); er != nil {
return er
}
default:
return fmt.Errorf("unhandled char after escape: %c", c)
}
} else {
if er = o.inputPlain(c); er != nil {
return er
}
}
}
}