Hwaro::Content::Processors::ServerHighlighter
Build-time highlighter: Tartrazine lexers + hljs-compatible classes.
Constants
Blocks larger than this are highlighted but not cached, keeping memory bounded; the entry cap guards pathological unique-block counts (clear-on-full is deterministic for output, only a speed hit).
Pygments/Chroma token-type prefixes mapped to Highlight.js classes, checked in order — first matching prefix wins, so more specific prefixes must precede their generic parent (KeywordType before Keyword).
Precomputed token-type → hljs class for every known token type. Built once at program start from Tartrazine's own type list, so lookups during parallel rendering are read-only and fiber-safe.
Tokenization runs in parallel across -Dpreview_mt workers.
It used to be serialized behind a global mutex here: the shard's
compiled rules each held a single PCRE2 match_data buffer reused
by every match() call and shared across lexer instances via the
template cache, so two workers tokenizing the same language
corrupted each other's matches (raising intermittently, which the
rescue below degraded to nondeterministic plain output). That
shared state — and the template cache's unsynchronized fast path,
and combined actions mutating the shared states Hash — is fixed
at the source in ext/tartrazine_mt_fix.cr, so correctness no
longer needs a lock around Tartrazine work.
Concurrency is still BOUNDED, not unlimited: tokenization is allocation-dense (token values, per-rule arrays), and past ~4 simultaneous tokenizers the Boehm GC's global allocation lock becomes a convoy that slows the WHOLE build (measured: a 5k-page site with 10k unique code blocks at 8 workers built ~25% slower fully unbounded than with this cap, while small and mid-size sites kept their full parallel speedup). A buffered channel acts as a counting semaphore; blocked fibers suspend — the worker thread moves on to other pages meanwhile.
Instance methods
Highlight code as lang, returning HTML-escaped markup with
hljs-class spans — or nil when no lexer exists for the language
(callers fall back to plain client-style output).