Skip to content

Commit

Permalink
highlighting no escape html option, everything now finally working fo…
Browse files Browse the repository at this point in the history
…r code command output highlighting
  • Loading branch information
rcoreilly committed Jul 22, 2024
1 parent be72cf8 commit 5068c06
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
30 changes: 20 additions & 10 deletions texteditor/highlighting/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,36 @@ func ChromaTagsLine(clex chroma.Lexer, txt string) (lexer.Line, error) {

// maxLineLen prevents overflow in allocating line length
const (
maxLineLen = 64 * 1024 * 1024
maxNumTags = 1024
maxLineLen = 64 * 1024 * 1024
maxNumTags = 1024
EscapeHTML = true
NoEscapeHTML = false
)

// 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,
// with any dangling ends truncated. only operates on given inputs, does not
// require any locking in terms of internal state.
func MarkupLine(txt []rune, hitags, tags lexer.Line) []byte {
// with any dangling ends truncated.
func MarkupLine(txt []rune, hitags, tags lexer.Line, escapeHtml bool) []byte {
if len(txt) > maxLineLen { // avoid overflow
return nil
}
sz := len(txt)
if sz == 0 {
return nil
}
var escf func([]rune) []byte
if escapeHtml {
escf = HtmlEscapeRunes
} else {
escf = func(r []rune) []byte {
return []byte(string(r))
}
}

ttags := lexer.MergeLines(hitags, tags) // ensures that inner-tags are *after* outer tags
nt := len(ttags)
if nt == 0 || nt > maxNumTags {
return HtmlEscapeRunes(txt)
return escf(txt)
}
sps := []byte(`<span class="`)
sps2 := []byte(`">`)
Expand All @@ -262,7 +272,7 @@ func MarkupLine(txt []rune, hitags, tags lexer.Line) []byte {
if ts.Ed <= tr.St {
ep := min(sz, ts.Ed)
if cp < ep {
mu = append(mu, HtmlEscapeRunes(txt[cp:ep])...)
mu = append(mu, escf(txt[cp:ep])...)
cp = ep
}
mu = append(mu, spe...)
Expand All @@ -273,7 +283,7 @@ func MarkupLine(txt []rune, hitags, tags lexer.Line) []byte {
break
}
if tr.St > cp {
mu = append(mu, HtmlEscapeRunes(txt[cp:tr.St])...)
mu = append(mu, escf(txt[cp:tr.St])...)
}
mu = append(mu, sps...)
clsnm := tr.Token.Token.StyleName()
Expand Down Expand Up @@ -302,15 +312,15 @@ func MarkupLine(txt []rune, hitags, tags lexer.Line) []byte {
}
ep = min(len(txt), ep)
if tr.St < ep {
mu = append(mu, HtmlEscapeRunes(txt[tr.St:ep])...)
mu = append(mu, escf(txt[tr.St:ep])...)
}
if addEnd {
mu = append(mu, spe...)
}
cp = ep
}
if sz > cp {
mu = append(mu, HtmlEscapeRunes(txt[cp:sz])...)
mu = append(mu, escf(txt[cp:sz])...)
}
// pop any left on stack..
for si := len(tstack) - 1; si >= 0; si-- {
Expand Down
3 changes: 2 additions & 1 deletion texteditor/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func (ed *Editor) layoutAllLines() {
mxwd := sz.X // always start with our render size

ed.hasLinks = false
cssAgg := ed.textStyleProperties()
for ln := 0; ln < nln; ln++ {
if ln >= len(ed.renders) || ln >= len(buf.Markup) {
break
}
rn := &ed.renders[ln]
rn.SetHTMLPre(buf.Markup[ln], fst, &sty.Text, &sty.UnitContext, ed.textStyleProperties())
rn.SetHTMLPre(buf.Markup[ln], fst, &sty.Text, &sty.UnitContext, cssAgg)
rn.Layout(&sty.Text, sty.FontRender(), &sty.UnitContext, sz)
if !ed.hasLinks && len(rn.Links) > 0 {
ed.hasLinks = true
Expand Down
2 changes: 1 addition & 1 deletion texteditor/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (ed *Editor) RenderWidget() {

// textStyleProperties returns the styling properties for text based on HiStyle Markup
func (ed *Editor) textStyleProperties() map[string]any {
if ed.Buffer == nil || !ed.Buffer.Highlighter.Has {
if ed.Buffer == nil {
return nil
}
return ed.Buffer.Highlighter.CSSProperties
Expand Down
5 changes: 3 additions & 2 deletions texteditor/textbuf/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ func (ls *Lines) markupApplyEdits(tags []lexer.Line) []lexer.Line {
} else {
stln := tbe.Reg.Start.Ln + 1
nlns := (tbe.Reg.End.Ln - tbe.Reg.Start.Ln)
stln = min(stln, len(tags))
tags = slices.Insert(tags, stln, make([]lexer.Line, nlns)...)
}
}
Expand All @@ -1371,7 +1372,7 @@ func (ls *Lines) markupApplyTags(tags []lexer.Line) {
for ln := range maxln {
ls.hiTags[ln] = tags[ln]
ls.tags[ln] = ls.adjustedTags(ln)
ls.Markup[ln] = highlighting.MarkupLine(ls.lines[ln], tags[ln], ls.tags[ln])
ls.Markup[ln] = highlighting.MarkupLine(ls.lines[ln], tags[ln], ls.tags[ln], highlighting.EscapeHTML)
}
if ls.MarkupDoneFunc != nil {
ls.MarkupDoneFunc()
Expand All @@ -1396,7 +1397,7 @@ func (ls *Lines) markupLines(st, ed int) bool {
mt, err := ls.Highlighter.MarkupTagsLine(ln, ltxt)
if err == nil {
ls.hiTags[ln] = mt
ls.Markup[ln] = highlighting.MarkupLine(ltxt, mt, ls.adjustedTags(ln))
ls.Markup[ln] = highlighting.MarkupLine(ltxt, mt, ls.adjustedTags(ln), highlighting.EscapeHTML)
} else {
ls.Markup[ln] = highlighting.HtmlEscapeRunes(ltxt)
allgood = false
Expand Down

0 comments on commit 5068c06

Please sign in to comment.