-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlight.go
230 lines (214 loc) · 4.61 KB
/
highlight.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
package mdedit
import "bytes"
const (
// blocks
mdHeading uint16 = 1 << iota
mdBlockquote
mdCodeBlock
mdThematicBreak
// inlines
mdItalic
mdStrong
mdCodeSpan
mdListMarker
mdLinkURL
)
type mdStyleMark struct {
col int
value uint16
}
type styleBuilder struct {
markers [][]mdStyleMark
row int
}
func (s *styleBuilder) startNewRow() {
if s.row == len(s.markers) {
s.markers = append(s.markers, []mdStyleMark{})
}
s.row++
}
func (s *styleBuilder) add(v uint16, col int) {
s.markers[s.row] = append(s.markers[s.row], mdStyleMark{
col: col,
value: v,
})
}
type mdHighlighter struct{}
func (mdHighlighter) highlight(buf *buffer) [][]mdStyleMark {
const (
bqStarted uint8 = iota + 1
bqHitChar
codeSpan1 uint8 = iota + 1
codeSpan2
)
var (
marks uint16
maybeHeading bool
bqState uint8
inCodeSpan uint8
inEmphasis1 byte
inEmphasis2 byte
)
sb := styleBuilder{markers: make([][]mdStyleMark, len(buf.lines))}
lineloop:
for row := 0; row < len(buf.lines); row++ {
line := buf.lines[row].text
if len(line) == 0 {
if marks&mdBlockquote == mdBlockquote && bqState == bqHitChar {
marks = marks &^ mdBlockquote
bqState = 0
}
}
marks = marks &^ mdHeading
sb.add(marks, 0)
var start int
for start = 0; start < len(line); start++ {
if line[start] != ' ' && line[start] != '\t' {
break
}
}
for col := start; col < len(line); col++ {
char := line[col]
if col == start {
switch char {
case '#':
maybeHeading = true
case '>':
marks |= mdBlockquote
sb.add(marks, col)
bqState = bqStarted
case '*', '+', '-':
if col+1 < len(line)-1 && line[col+1] == ' ' {
sb.add(marks|mdListMarker, col)
sb.add(marks, col+1)
col++
}
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
if col+2 < len(line)-1 && line[col+1] == '.' && line[col+2] == ' ' {
sb.add(marks|mdListMarker, col)
sb.add(marks, col+2)
col += 2
}
}
}
if bqState == bqStarted && char != ' ' && char != '\t' && char != '>' {
bqState = bqHitChar
}
if maybeHeading && char != '#' {
if char == ' ' {
marks |= mdHeading
sb.add(marks, start)
}
maybeHeading = false
}
switch char {
case '*', '_':
var prev, next byte
if col > 0 {
prev = line[col-1]
}
if col+1 < len(line) {
next = line[col+1]
}
isPrevBlank := (prev == 0 || prev == ' ' || prev == '\t')
isNextBlank := (next == 0 || next == ' ' || next == '\t')
if char == next {
switch {
case inEmphasis2 == 0 && isPrevBlank:
var next2 byte
if col+2 < len(line) {
next2 = line[col+2]
}
if next2 != ' ' && next2 != '\t' {
inEmphasis2 = char
marks |= mdStrong
sb.add(marks, col)
col++
}
case inEmphasis2 == char && !isPrevBlank:
inEmphasis2 = 0
marks &^= mdStrong
sb.add(marks, col+2)
col++
}
} else {
switch {
case inEmphasis1 == 0 && isPrevBlank && !isNextBlank:
inEmphasis1 = char
marks |= mdItalic
sb.add(marks, col)
case inEmphasis1 != 0 && !isPrevBlank && (isNextBlank || isPunct(next) || next == inEmphasis2):
inEmphasis1 = 0
marks &^= mdItalic
if col < len(line)-1 {
sb.add(marks, col+1)
}
}
}
case '`':
var next byte
if col+1 < len(line) {
next = line[col+1]
}
if col == start && next == '`' && col+2 < len(line) && line[col+2] == '`' {
sb.add(marks|mdCodeBlock, start)
delimCodeBlock := []byte("```")
for {
row++
if row >= len(buf.lines) {
break lineloop
}
sb.startNewRow()
sb.add(marks|mdCodeBlock, start)
ln := &buf.lines[row]
if bytes.Equal(ln.text[ln.startingIndex():], delimCodeBlock) {
break
}
}
sb.startNewRow()
sb.add(marks, 0)
continue lineloop
}
switch inCodeSpan {
case 0:
inCodeSpan = codeSpan1
marks |= mdCodeSpan
sb.add(marks, col)
if next == '`' {
inCodeSpan = codeSpan2
col += 2
}
case codeSpan1:
marks &^= mdCodeSpan
inCodeSpan = 0
if col < len(line)-1 {
sb.add(marks, col+1)
}
case codeSpan2:
if next == '`' {
marks &^= mdCodeSpan
inCodeSpan = 0
col++
if col+1 < len(line)-1 {
sb.add(marks, col+2)
}
}
}
}
}
if maybeHeading {
sb.add(marks|mdHeading, start)
maybeHeading = false
}
sb.startNewRow()
}
return sb.markers
}
func isPunct(char byte) bool {
switch char {
case '.', ',', '!', '?':
return true
default:
return false
}
}