@@ -6,6 +6,7 @@ import (
66 "fmt"
77 "html"
88 "html/template"
9+ "io"
910 "os"
1011 "path"
1112 "path/filepath"
@@ -107,10 +108,27 @@ func highlight(name, text string) (template.HTML, error) {
107108 if lexer == nil {
108109 lexer = lexers.Fallback
109110 }
110- it, err := lexer.Tokenise(nil, text)
111+ tokens, err := tokenise(lexer, text)
111112 if err != nil {
112113 return "", err
113114 }
115+ var buf strings.Builder
116+ buf.WriteString(`<pre class="code">`)
117+ for i, line := range chroma.SplitTokensIntoLines(tokens) {
118+ fmt.Fprintf(&buf, `<span id="L%d"><a href="#L%d">%d</a>`, i+1, i+1, i+1)
119+ writeTokens(&buf, line)
120+ buf.WriteString("</span>")
121+ }
122+ buf.WriteString("</pre>")
123+ return template.HTML(buf.String()), nil
124+}
125+
126+// tokenise lexes text into the palette's token types, merging neighbours of the same type.
127+func tokenise(lexer chroma.Lexer, text string) ([]chroma.Token, error) {
128+ it, err := lexer.Tokenise(nil, text)
129+ if err != nil {
130+ return nil, err
131+ }
114132 var tokens []chroma.Token
115133 for _, t := range it.Tokens() {
116134 t.Type = colour(t.Type)
@@ -120,21 +138,18 @@ func highlight(name, text string) (template.HTML, error) {
120138 tokens = append(tokens, t)
121139 }
122140 }
123- var buf strings.Builder
124- buf.WriteString(`<pre class="code">`)
125- for i, line := range chroma.SplitTokensIntoLines(tokens) {
126- fmt.Fprintf(&buf, `<span id="L%d"><a href="#L%d">%d</a>`, i+1, i+1, i+1)
127- for _, t := range line {
128- if c := chroma.StandardTypes[t.Type]; c != "" {
129- fmt.Fprintf(&buf, `<b class="%s">%s</b>`, c, html.EscapeString(t.Value))
130- } else {
131- buf.WriteString(html.EscapeString(t.Value))
132- }
141+ return tokens, nil
142+}
143+
144+// writeTokens writes tokens as escaped text, each coloured one in a <b> with its class.
145+func writeTokens(w io.StringWriter, tokens []chroma.Token) {
146+ for _, t := range tokens {
147+ if c := chroma.StandardTypes[t.Type]; c != "" {
148+ w.WriteString(`<b class="` + c + `">` + html.EscapeString(t.Value) + `</b>`)
149+ } else {
150+ w.WriteString(html.EscapeString(t.Value))
133151 }
134- buf.WriteString("</span>")
135152 }
136- buf.WriteString("</pre>")
137- return template.HTML(buf.String()), nil
138153 }
139154
140155 // palette is the token types site.css colours. Every other token is plain text, which needs no span,