Highlight
Highlight — lightweight, server-side syntax highlighting for fenced code blocks. No client-side JavaScript: every token is html-escaped and wrapped in a <span class="tok-*"> so the CSS theme in the host page colors it.
Design / RC discipline (mirrors markdown.march):
- The tokenizer walks a List(String) of single-character strings
(`string_chars`) and accumulates output as a List(String) that is
reversed + joined at the very end. We NEVER use the recursive
`acc ++ char` string-accumulation pattern (a known compiled-codegen /
Perceus RC hazard); multi-character tokens are collected as a
List(String) of chars and joined with String.join(_, "").
* Every inner scanning loop is a top-level `pfn` (not a self-capturing
recursive closure) to avoid Perceus RC underflow.
* html_escape is applied to token *text* before it is wrapped in a span,
so we can never emit unescaped source (e.g. `<` always becomes `<`).Robustness contract: the tokenizer never crashes and never drops a character. Anything it does not recognize is emitted as escaped text with no span. Correctness (escaping + no character loss) beats completeness.
Functions
Syntax-highlight a fenced code block. lang is the fence language (may be ""); code is the raw block body. Returns HTML with tokens wrapped in <span class="tok-*"> — always html-escaped, never crashing or dropping chars. An empty/unknown language still gets a generic strings/comments/numbers pass.