$ show alecthomas/chroma
Chroma is a pure-Go syntax highlighting library and toolchain, inspired by Pygments. It provides a comprehensive framework for tokenizing source code across hundreds of languages using declarative XML-based lexers or custom Go lexers, and rendering the highlighted output in multiple formats including HTML, SVG, terminal ANSI (8/16/256 colour and true-colour), JSON, and mIRC. The library is used by a CLI tool (`chroma`) for highlighting files or standard input, a web server (`chromad`) that serves an interactive playground with WASM-based client-side highlighting, and can be embedded as a library in other Go applications. Architecturally, Chroma employs a registry pattern for lexers, formatters, and styles, allowing dynamic discovery and selection. Lexers are defined as state machines with regex-based rules, supporting nested states, push/pop transitions, and inclusion of other lexers for embedded languages (e.g., PHP in HTML). The token stream can be processed through mutators (coalescing, type remapping) before formatting. Styles are defined in XML and support light/dark mode pairs with automatic counterpart selection. Key entry points include the `quick.Quick.Highlight()` convenience function, the CLI in `cmd/chroma/main.go`, the web server in `cmd/chromad/main.go`, and the WASM module in `cmd/libchromawasm/main.go`. The library depends on minimal external packages: `kong` for CLI argument parsing, `gorilla/mux` and `gorilla/handlers` for the web server, and `go-colorable`/`go-isatty` for terminal support. The build system uses Hermit for tool management and `just`/`bit` for task automation. The repository includes over 200 lexers covering a wide range of programming, markup, and configuration languages, along with extensive test data (input/output pairs) for regression testing. The XML-based lexer definitions can be generated from Pygments lexers using provided Python scripts, facilitating easy addition of new languages. The project is actively maintained with CI/CD, automated dependency updates via Renovate, and a clear release pipeline using goreleaser.
The LLM identified 4 files worth highlighting for reuse:
Implements the Colour type with RGB representation, brightness, and redmean-based distance calculation for colour similarity. Provides ANSI colour mapping (8/16/256 colours and true-colour) and colour clamping. This is a self-contained utility that can be extracted for any project requiring colour manipulation and distance metrics, demonstrating careful handling of colour spaces and perceptual distance.
Implements a Coalesce lexer that merges consecutive tokens of the same type into a single token. This is a common post-processing step in syntax highlighting to reduce token fragmentation. The implementation is isolated and can be reused as a decorator around any lexer that produces a token stream, demonstrating a clean filter/decorator pattern.
Implements TypeRemappingLexer, a lexer decorator that remaps token types based on word lists (e.g., marking specific identifiers as built-in functions). It uses a trie-like structure for efficient lookup and supports multiple remapping rules. This component can be extracted and reused to add semantic token type overrides to any lexer, demonstrating a flexible decoration pattern for lexer post-processing.
Provides a generic registry for lexers, formatters, and styles, supporting registration by name, alias, file extension, and MIME type. Includes lazy loading and fallback mechanisms. This pattern is reusable in any application that needs a plugin or service registry, demonstrating a clean implementation of the registry pattern with thread-safe registration and lookup.