go / syntax highlighting
highlight colors source code for server-rendered HTML. It has a hand-written, line-oriented scanner per language over a shared token type.
highlight.Code(w, "main.go", src) // or "go", from a markdown fence
highlight.Diff("main.go", patch) // a unified-diff patch
I use it for code fences and diff views in server-rendered pages.
Scope
Scanners exist for Go, SQL, CSS, JavaScript, C, Ruby, JSON, Lua, HTML, Markdown, shell, patch files, and hml. A format with no scanner comes out escaped and uncolored. A word a keyword list misses comes out uncolored.
The patch scanner accepts .patch and .diff files, and patch or
diff fence labels. It colors file headers, hunk headers, additions,
and deletions. This is separate from Diff, which colors a source
file's changed lines with that file's language scanner.
Classes
The output uses a short list of classes: k keywords, n names, s
strings, m numbers, c comments. A diff row adds one more: gi
inserted, gd deleted, gu hunk header, gc context.
<span class="k">func</span> <span class="n">main</span>() {}
highlight ships no palette. The site that uses it styles the classes. This blog styles them twice: a light palette and Catppuccin Frappe, the flavor my editor uses.
pre .k {
color: #81c8be;
}
pre .s {
color: #a6d189;
}
pre .c {
color: #737994;
}
Two layers
Tokens are the lower layer. A scanFunc reads one line, takes the
state the previous line ended in, and returns the state this one ends
in. A lexer restarted per line colors the second half of a raw string
as code.
Emitters are the upper layer. Code writes a whole file. Diff writes
a unified-diff patch, and each row also gets its diff class.
Both emitters HTML-escape every byte they write. The source can come from an untrusted file.
Line orientation
A patch is a set of hunks with gaps, so there is no whole file to lex. The scanner colors each row from the state the row above it left. Rows the patch skipped leave that state approximate, which is acceptable in a diff. A whole file has no gap, so its state is exact.