-
Notifications
You must be signed in to change notification settings - Fork 0
/
himarkup.go
316 lines (291 loc) · 8.95 KB
/
himarkup.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
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
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
stdhtml "html"
"log"
"strings"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/goki/gi/gi"
"github.com/goki/gi/histyle"
"github.com/goki/ki/ints"
"github.com/goki/ki/ki"
"github.com/goki/pi/filecat"
"github.com/goki/pi/lex"
"github.com/goki/pi/pi"
_ "github.com/goki/pi/suplangs"
"github.com/goki/pi/token"
)
// HiMarkup manages the syntax highlighting state for TextBuf
// it uses Pi if available, otherwise falls back on chroma
type HiMarkup struct {
Info *FileInfo `desc:"full info about the file including category etc"`
Style gi.HiStyleName `desc:"syntax highlighting style"`
Lang string `desc:"chroma-based language name for syntax highlighting the code"`
Has bool `desc:"true if both lang and style are set"`
TabSize int `desc:"tab size, in chars"`
CSSProps ki.Props `json:"-" xml:"-" desc:"Commpiled CSS properties for given highlighting style"`
PiState *pi.FileStates `desc:"pi parser state info"`
PiLang pi.Lang `desc:"if supported, this is the pi Lang support for parsing"`
HiStyle *histyle.Style `desc:"current highlighting style"`
Off bool `desc:"external toggle to turn off automatic highlighting"`
lastLang string
lastStyle gi.HiStyleName
lexer chroma.Lexer
formatter *html.Formatter
}
// HasHi returns true if there are highlighting parameters set (only valid after Init)
func (hm *HiMarkup) HasHi() bool {
return hm.Has
}
// UsingPi returns true if markup is using GoPi lexer / parser -- affects
// use of results
func (hm *HiMarkup) UsingPi() bool {
return hm.PiLang != nil
}
// Init initializes the syntax highlighting for current params
func (hm *HiMarkup) Init(info *FileInfo, pist *pi.FileStates) {
hm.Info = info
hm.PiState = pist
if hm.Info.Sup != filecat.NoSupport {
if lp, err := pi.LangSupport.Props(hm.Info.Sup); err == nil {
if lp.Lang != nil {
hm.lexer = nil
hm.PiLang = lp.Lang
} else {
hm.PiLang = nil
}
}
}
if hm.PiLang == nil {
lexer := lexers.Match(hm.Info.Name)
// if lexer == nil && len(pist.Src.Lines) > 0 {
// lexer = lexers.Analyse(string(tb.Txt))
// }
if lexer != nil {
hm.Lang = lexer.Config().Name
hm.lexer = lexer
}
}
if hm.Style == "" || (hm.PiLang == nil && hm.lexer == nil) {
hm.Has = false
return
}
hm.Has = true
if hm.Style != hm.lastStyle {
hm.HiStyle = histyle.AvailStyle(hm.Style)
hm.CSSProps = hm.HiStyle.ToProps()
hm.lastStyle = hm.Style
}
if hm.lexer != nil && hm.Lang != hm.lastLang {
hm.lexer = chroma.Coalesce(lexers.Get(hm.Lang))
hm.formatter = html.New(html.WithClasses(true), html.TabWidth(hm.TabSize))
hm.lastLang = hm.Lang
}
}
// SetHiStyle sets the highlighting style and updates corresponding settings
func (hm *HiMarkup) SetHiStyle(style gi.HiStyleName) {
hm.Style = style
hm.HiStyle = histyle.AvailStyle(hm.Style)
hm.CSSProps = hm.HiStyle.ToProps()
hm.lastStyle = hm.Style
}
// MarkupTagsAll returns all the markup tags according to current
// syntax highlighting settings
func (hm *HiMarkup) MarkupTagsAll(txt []byte) ([]lex.Line, error) {
if hm.Off {
return nil, nil
}
if hm.PiLang != nil {
hm.PiLang.ParseFile(hm.PiState, txt) // processes in Proc(), does Switch()
return hm.PiState.Done().Src.Lexs, nil // Done() is previous Proc() -- still has type info coming in later, but lexs are good
} else if hm.lexer != nil {
return hm.ChromaTagsAll(txt)
}
return nil, nil
}
// MarkupTagsLine returns tags for one line according to current
// syntax highlighting settings
func (hm *HiMarkup) MarkupTagsLine(ln int, txt []rune) (lex.Line, error) {
if hm.Off {
return nil, nil
}
if hm.PiLang != nil {
ll := hm.PiLang.HiLine(hm.PiState, ln, txt)
return ll, nil
} else if hm.lexer != nil {
return hm.ChromaTagsLine(txt)
}
return nil, nil
}
// ChromaTagsForLine generates the chroma tags for one line of chroma tokens
func (hm *HiMarkup) ChromaTagsForLine(tags *lex.Line, toks []chroma.Token) {
cp := 0
for _, tok := range toks {
str := []rune(strings.TrimSuffix(tok.Value, "\n"))
slen := len(str)
if slen == 0 {
continue
}
if tok.Type == chroma.None { // always a parsing err AFAIK
// fmt.Printf("type: %v st: %v ed: %v txt: %v\n", tok.Type, cp, ep, str)
continue
}
ep := cp + slen
if tok.Type < chroma.Text {
ht := histyle.TokenFromChroma(tok.Type)
tags.AddLex(token.KeyToken{Tok: ht}, cp, ep)
}
cp = ep
}
}
// ChromaTagsAll returns all the markup tags according to current
// syntax highlighting settings
func (hm *HiMarkup) ChromaTagsAll(txt []byte) ([]lex.Line, error) {
txtstr := string(txt) // expensive!
iterator, err := hm.lexer.Tokenise(nil, txtstr)
if err != nil {
log.Println(err)
return nil, err
}
lines := chroma.SplitTokensIntoLines(iterator.Tokens())
sz := len(lines)
tags := make([]lex.Line, sz)
for li, lt := range lines {
hm.ChromaTagsForLine(&tags[li], lt)
}
return tags, nil
}
// ChromaTagsLine returns tags for one line according to current
// syntax highlighting settings
func (hm *HiMarkup) ChromaTagsLine(txt []rune) (lex.Line, error) {
txtstr := string(txt) + "\n"
iterator, err := hm.lexer.Tokenise(nil, txtstr)
if err != nil {
log.Println(err)
return nil, err
}
var tags lex.Line
toks := iterator.Tokens()
hm.ChromaTagsForLine(&tags, toks)
return tags, nil
}
// MarkupLine returns the line with html class tags added for each tag
// takes both the hi tags and extra tags. Only fully nested tags are supported --
// any dangling ends are truncated.
func (hm *HiMarkup) MarkupLine(txt []rune, hitags, tags lex.Line) []byte {
sz := len(txt)
if sz == 0 {
return nil
}
ttags := lex.MergeLines(hitags, tags) // ensures that inner-tags are *after* outer tags
nt := len(ttags)
if nt == 0 {
return HTMLEscapeRunes(txt)
}
sps := []byte(`<span class="`)
sps2 := []byte(`">`)
spe := []byte(`</span>`)
taglen := len(sps) + len(sps2) + len(spe) + 2
musz := sz + len(ttags)*taglen
mu := make([]byte, 0, musz)
cp := 0
var tstack []int // stack of tags indexes that remain to be completed, sorted soonest at end
for i, tr := range ttags {
if cp >= sz {
break
}
for si := len(tstack) - 1; si >= 0; si-- {
ts := ttags[tstack[si]]
if ts.Ed <= tr.St {
ep := ints.MinInt(sz, ts.Ed)
if cp < ep {
mu = append(mu, HTMLEscapeRunes(txt[cp:ep])...)
cp = ep
}
mu = append(mu, spe...)
tstack = append(tstack[:si], tstack[si+1:]...)
}
}
if cp >= sz || tr.St >= sz {
break
}
if tr.St > cp {
mu = append(mu, HTMLEscapeRunes(txt[cp:tr.St])...)
}
mu = append(mu, sps...)
clsnm := tr.Tok.Tok.StyleName()
mu = append(mu, []byte(clsnm)...)
mu = append(mu, sps2...)
ep := tr.Ed
addEnd := true
if i < nt-1 {
if ttags[i+1].St < tr.Ed { // next one starts before we end, add to stack
addEnd = false
ep = ttags[i+1].St
if len(tstack) == 0 {
tstack = append(tstack, i)
} else {
for si := len(tstack) - 1; si >= 0; si-- {
ts := ttags[tstack[si]]
if tr.Ed <= ts.Ed {
ni := si // + 1 // new index in stack -- right *before* current
tstack = append(tstack, i)
copy(tstack[ni+1:], tstack[ni:])
tstack[ni] = i
}
}
}
}
}
ep = ints.MinInt(len(txt), ep)
if tr.St < ep {
mu = append(mu, HTMLEscapeRunes(txt[tr.St:ep])...)
}
if addEnd {
mu = append(mu, spe...)
}
cp = ep
}
if sz > cp {
mu = append(mu, HTMLEscapeRunes(txt[cp:sz])...)
}
// pop any left on stack..
for si := len(tstack) - 1; si >= 0; si-- {
mu = append(mu, spe...)
}
return mu
}
///////////////////////////////////////////////////////////////////////////
// HTMLEscapeBytes
// var htmlEscaper = bytereplacer.New(
// `&`, "&",
// `'`, "'", // "'" is shorter than "'" and apos was not in HTML until HTML5.
// `<`, "<",
// `>`, ">",
// `"`, """, // """ is shorter than """.
// )
//
// HTMLEscapeBytes escapes special characters like "<" to become "<". It
// escapes only five such characters: <, >, &, ' and ".
// It operates on a *copy* of the byte string and does not modify the input!
// otherwise it causes major problems..
func HTMLEscapeBytes(b []byte) []byte {
// note: because we absolutely need to make copies, it is not clear
// that this is any faster, and requiring the extra dependency likely
// not worth it..
// bc := make([]byte, len(b), len(b)+10)
// copy(bc, b)
// return htmlEscaper.Replace(bc)
return []byte(stdhtml.EscapeString(string(b)))
}
// HTMLEscapeRunes escapes special characters like "<" to become "<". It
// escapes only five such characters: <, >, &, ' and ".
// It operates on a *copy* of the byte string and does not modify the input!
// otherwise it causes major problems..
func HTMLEscapeRunes(r []rune) []byte {
return []byte(stdhtml.EscapeString(string(r)))
}