Alaja.Syntax.Renderer (Alaja v2.0.0)

Copy Markdown View Source

Converts token lists to ANSI-formatted terminal output.

Uses ChunkText as intermediate representation so colors and effects are resolved through Pote's theme system — same pipeline that Alaja.print_success, Alaja.Components.Table etc. already use.

Summary

Functions

Renders a token list into display-ready {color, text} tuples.

Renders tokens to ANSI escape sequences ready for terminal output.

Renders token list as a flat string (no ANSI escapes). Useful for testing or non-terminal output.

Functions

render(tokens, lang, global_theme \\ nil)

@spec render(
  [{atom(), String.t()}],
  Alaja.Syntax.Language.t(),
  Alaja.Syntax.Theme.t() | nil
) :: [
  {String.t(), String.t()}
]

Renders a token list into display-ready {color, text} tuples.

Each tuple's color is an ANSI color string resolved through the theme chain:

  1. Language-level colors (lang.colors)
  2. Global syntax theme
  3. Hardcoded defaults

Examples

iex> lang = %Alaja.Syntax.Language{
...>   name: "test",
...>   colors: %{keyword: {:blue, [:bold]}}
...> }
iex> Renderer.render([{:keyword, "def"}, {:plain, " x"}], lang)
[{"blue", "def"}, {"white", " x"}]

render_ansi(tokens, lang, global_theme \\ nil)

@spec render_ansi(
  [{atom(), String.t()}],
  Alaja.Syntax.Language.t(),
  Alaja.Syntax.Theme.t() | nil
) ::
  IO.iodata()

Renders tokens to ANSI escape sequences ready for terminal output.

Uses ChunkText.render/1 under the hood, which resolves colors through Pote's resolver bridge (supports atoms, hex, "theme:key", etc.) and applies effects (bold, italic, etc.).

render_plain(tokens)

@spec render_plain([{atom(), String.t()}]) :: String.t()

Renders token list as a flat string (no ANSI escapes). Useful for testing or non-terminal output.