smalto
A general-purpose syntax highlighting library for Gleam.
Smalto uses regex-based grammars inspired by Prism.js to tokenize source code for 30 programming languages, with output to structured tokens, ANSI terminal colors, or HTML.
Usage
import smalto
import smalto/languages/python
let html = smalto.to_html("print('hello')", python.grammar())
let ansi = smalto.to_ansi("print('hello')", python.grammar())
let tokens = smalto.to_tokens("print('hello')", python.grammar())
Each language module exports a single grammar() function that returns
the language’s Grammar definition. Pass it directly to to_tokens,
to_html, or to_ansi along with the source code to highlight.
Values
pub fn to_ansi(code: String, grammar: grammar.Grammar) -> String
Render syntax-highlighted text with ANSI terminal color codes using the default color theme.
Each token type is mapped to a specific terminal color.
pub fn to_ansi_with(
code: String,
grammar: grammar.Grammar,
theme: ansi_theme.AnsiTheme,
) -> String
Render syntax-highlighted text with ANSI terminal color codes using a custom color theme.
pub fn to_html(code: String, grammar: grammar.Grammar) -> String
Render syntax-highlighted HTML from source code.
Tokens are wrapped in <span class="smalto-{name}"> elements with
HTML-escaped content. Whitespace and unmatched text are output as-is.
pub fn to_tokens(
code: String,
grammar: grammar.Grammar,
) -> List(token.Token)
Tokenize source code into a list of tokens using the given grammar.
Cross-language LanguageRef references in grammar rules are resolved
automatically using the built-in language registry.