Syntax highlighting for terminal output.
Two-tier system:
- Built-in languages (
:elixir,:json,:markdown,:text) use inline tokenizers for Alaja's own CLI. - Registered languages (Python, TypeScript, Rust, etc.) use
Alaja.Syntax.Enginedriven byAlaja.Syntax.Languagedefinitions, typically registered by host applications (e.g. Delfos).
Usage
# Built-in
Alaja.Syntax.highlight_content(code, :elixir)
# Registered by host app
Alaja.Syntax.highlight_content(code, :python)Registering a language
alias Alaja.Syntax.Language
Alaja.Syntax.register_language(:python, %Language{
name: "Python",
line_comment: "#",
keywords: MapSet.new(~w(def class if elif else for while return)),
colors: %{keyword: {:blue, [:bold]}}
})
Summary
Functions
Detects language atom from file path extension.
Retrieves a registered language definition.
Highlights source code and returns ANSI escape sequences.
Highlights source code and returns an Alaja.Buffer.t/0.
Highlights source code for a given language.
Highlights a file by detecting language from extension.
Lists all registered language names.
Registers a language definition under an atom key.
Tokenizes source code into {type, text} tuples.
Types
Functions
Detects language atom from file path extension.
@spec get_language(atom()) :: {:ok, Alaja.Syntax.Language.t()} | :error
Retrieves a registered language definition.
Highlights source code and returns ANSI escape sequences.
Same API as highlight_content/2 but produces terminal-ready output.
@spec highlight_buffer(String.t(), language(), keyword()) :: Alaja.Buffer.t()
Highlights source code and returns an Alaja.Buffer.t/0.
This is the Buffer-first canonical render. Each token becomes one
cell per visible character with the resolved fg colour from the
language/theme chain. Effects (:bold, :italic, ...) are stored
on the cell but currently not visually distinct in the buffer — use
highlight_ansi/2 for full effect rendering.
Options
:theme— overrides the global syntax theme (default:Theme.default()):max_width— wraps long lines; passfalseto disable wrapping
Examples
buf = Alaja.Syntax.highlight_buffer("defmodule Foo do end", :elixir)
Alaja.Printer.print_raw(buf)
Highlights source code for a given language.
Returns [{color_string, text}] tuples. For ANSI-rendered output
use highlight_ansi/2.
Highlights a file by detecting language from extension.
@spec list_languages() :: [atom()]
Lists all registered language names.
@spec register_language(atom(), Alaja.Syntax.Language.t()) :: :ok
Registers a language definition under an atom key.
Tokenizes source code into {type, text} tuples.