package ochre
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=a8e38fbcd22fbb68b0f7d93b36a2ba1670e8bd061b43a0177226738307183a32
sha512=7b344c3812be283b0101078564e564732f878ad226c7ce0212c9e9139fb41a291e4d31ad52c949f2978c48e04240d4d22b5b4ae63808e15a21c44a2fbeed8a22
doc/ochre/Ochre/index.html
Module OchreSource
Syntax highlighter using TextMate grammars and themes.
Ochre turns source code into highlighted output using TextMate grammars (the same tokenizer that powers VS Code) and TextMate/VS Code themes. It supports HTML, ANSI terminal colors, LaTeX, and SVG output formats.
let highlighter = Ochre.load_exn [ ("ocaml", ocaml_grammar_json) ] in
let html =
Ochre.to_html highlighter ~theme:Ochre.Theme.nord ~lang:"ocaml"
"let x = 42"Highlighter
The highlighter holds loaded grammars and drives tokenization. Load one with load or load_from_files, then pass it to any backend function.
The language identifiers "plaintext", "text", and "txt" are always available, even with no grammar loaded: they produce unstyled tokens using the theme's default colors.
Highlighter instance. Holds loaded grammars and tokenization state.
load
Load a highlighter from grammar JSON strings.
Each pair is (lang_id, json_content) where lang_id is the language identifier and json_content is the raw TextMate grammar JSON.
Returns Error msg when a grammar fails to parse.
match Ochre.load [ ("ocaml", Tm_grammar_ocaml.json) ] with
| Ok hl ->
Ochre.to_html hl ~theme:Ochre.Theme.nord ~lang:"ocaml" code
| Error msg ->
failwith msgload_exn
Like load but raises on failure.
let hl = Ochre.load_exn [ ("ocaml", Tm_grammar_ocaml.json) ]load_from_files
Load a highlighter from grammar files on disk.
Each grammar is a path to a .tmLanguage.json file. The language identifier is derived from the filename (e.g. "ocaml.tmLanguage.json" registers as "ocaml").
Returns Error msg when a file cannot be read or a grammar fails to parse.
match
Ochre.load_from_files [ "/usr/share/grammars/ocaml.tmLanguage.json" ]
with
| Ok hl ->
Ochre.to_html hl ~theme:Ochre.Theme.nord ~lang:"ocaml" code
| Error msg ->
failwith msgload_from_files_exn
Like load_from_files but raises on failure.
let hl =
Ochre.load_from_files_exn [ "/usr/share/grammars/ocaml.tmLanguage.json" ]Tokens
Tokens are the basic unit of highlighted code. After tokenization, each fragment of source code becomes a Token.styled_token carrying colors, font styles, and scope information resolved from a theme.
Themes
A theme maps TextMate scopes to colors and font styles. Ochre ships with several built-in themes and can load any VS Code / TextMate theme from JSON.
Backends
Ochre can render highlighted code to several output formats. All backends share the same pattern: pass a highlighter, a theme, a language, and the source code.
- HTML — Self-contained
<pre><code>blocks with inline styles or CSS classes. Supports multi-theme via CSS custom properties, line numbers, and configurable class prefixes. - ANSI — 24-bit color escape sequences for terminal display.
- LaTeX —
\textcolorcommands inside anochrehighlightenvironment. Requires thexcolorandsoulpackages. - SVG — Standalone
<svg>elements with monospace<text>and per-token<tspan>styling. - Tokens — Structured token output for full rendering control.
- Debug tokens — Raw token text for debugging grammar and scope matching.
to_tokens
val to_tokens :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
theme:Theme.theme ->
lang:string ->
string ->
Token.highlighted_codeHighlight source code and return structured tokens. Use this when you need full control over rendering.
When ~decorations or ~transforms are provided, decorations are applied after tokenization and transforms run after decorations.
Raises Failure if the grammar for lang cannot be found. The languages "plaintext", "text", and "txt" never raise: they yield unstyled tokens.
let tokens = Ochre.to_tokens hl ~theme ~lang:"ocaml" code in
List.iter
(fun line ->
List.iter
(fun (tok : Ochre.Token.styled_token) -> Printf.printf "%s" tok.text)
line
)
tokensHTML options
to_html
val to_html :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
?options:Html_options.t ->
?theme:Theme.theme ->
?extra_themes:(string * Theme.theme) list ->
lang:string ->
string ->
stringHighlight source code to HTML.
Single theme
Pass ~theme for a self-contained block with inline styles:
Ochre.to_html hl ~theme:Ochre.Theme.nord ~lang:"ocaml" "let x = 42"Multiple themes
Pass ~extra_themes with labelled extra themes. Each label becomes a CSS custom property prefix (--ochre-<label>). The default theme is emitted as base fallback variables (var(--ochre-bg,<bg>), var(--ochre,<fg>)), while extra themes are emitted as label-scoped variables.
Ochre.to_html hl ~theme:Ochre.Theme.light
~extra_themes:[ ("dark", Ochre.Theme.nord) ]
~lang:"ocaml" "let x = 42"Options
Pass ~options to control rendering behaviour:
let opts =
Ochre.Html_options.make ~line_numbers:true ~default_color:No_default_color
()
in
Ochre.to_html hl ~options:opts ~theme ~lang:"ocaml" codeWhen ~theme is omitted but ~extra_themes is provided, the first entry becomes the default. When neither ~theme nor ~extra_themes is provided, Theme.dark is used as the default.
Pair with html_theme_css to activate alternate themes via CSS.
html_theme_css
html_theme_css label returns a CSS rule that maps base variables (--ochre-*) to the label-scoped variables (--ochre-<label>-*).
Wrap this in your own selector (a media query, a .dark class, a data-theme attribute selector, etc.) to control when the theme activates.
Printf.sprintf ".dark {\n %s\n}" (Ochre.html_theme_css "dark")html_render_theme_css
val html_render_theme_css :
class_prefix:string ->
Theme.theme ->
Token.highlighted_code ->
stringhtml_render_theme_css ~class_prefix theme code generates a complete CSS stylesheet for the given theme and highlighted code when using Html_options.style_mode.Css_classes mode. Walks all tokens to discover unique styles and maps each to a deterministic class name prefixed with class_prefix.
let tokens = Ochre.to_tokens hl ~theme ~lang:"ocaml" code in
let css = Ochre.html_render_theme_css ~class_prefix:"oc-" theme tokens in
Printf.printf "<style>%s</style>" cssto_ansi
val to_ansi :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
theme:Theme.theme ->
lang:string ->
string ->
stringHighlight source code to ANSI terminal escape sequences.
Produces text with embedded 24-bit ANSI color codes for terminal display. Raises Failure if the grammar for lang cannot be found.
let ansi = Ochre.to_ansi hl ~theme:Ochre.Theme.nord ~lang:"ocaml" code in
print_string ansito_latex
val to_latex :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
theme:Theme.theme ->
lang:string ->
string ->
stringHighlight source code to LaTeX with \textcolor commands.
Produces a block wrapped in an ochrehighlight environment. Requires the xcolor and soul LaTeX packages. Raises Failure if the grammar for lang cannot be found.
let latex = Ochre.to_latex hl ~theme:Ochre.Theme.nord ~lang:"ocaml" code in
Printf.printf "\\begin{document}\n%s\n\\end{document}" latexto_svg
val to_svg :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
theme:Theme.theme ->
lang:string ->
string ->
stringHighlight source code to a self-contained SVG element.
Produces an <svg> with monospace <text> elements and per-token <tspan> styling. Suitable for embedding in documents or rendering as an image. Raises Failure if the grammar for lang cannot be found.
let svg = Ochre.to_svg hl ~theme:Ochre.Theme.nord ~lang:"ocaml" codeto_debug_tokens
val to_debug_tokens :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
theme:Theme.theme ->
lang:string ->
string ->
stringHighlight source code and render each token as {text}[scope1,scope2,...].
Useful for debugging grammar and scope matching. Raises Failure if the grammar for lang cannot be found.
let debug =
Ochre.to_debug_tokens hl ~theme:Ochre.Theme.nord ~lang:"ocaml" code
in
print_string debugoutput_format
output_formats
Mapping of format names to typed variants.
string_of_output_format
Convert a format variant to its canonical lowercase name.
output_format_of_string
Parse a format name into a variant. Returns None for unrecognised names.
to_string
val to_string :
t ->
?decorations:Decoration.t list ->
?transforms:Transform.t list ->
?options:Html_options.t ->
?extra_themes:(string * Theme.theme) list ->
format:output_format ->
theme:Theme.theme ->
lang:string ->
string ->
stringTransforms
Transforms run after tokenization and theming, but before rendering. They can modify tokens, lines, or the entire document.
Built-in transforms
Ready-to-use transforms for common highlighting patterns.
Decorations
Decorations attach format-agnostic properties (CSS class, inline style, data attributes) to ranges of source code identified by line/character position. They are applied after tokenization but before transforms.