A syntax highlight library and cli using TextMate grammars and custom themes
  • OCaml 81.8%
  • HTML 11.8%
  • Perl 4.2%
  • Raku 1%
  • TeX 0.6%
  • Other 0.6%
Find a file
David Sancho Moreno d914072147 Prepare 1.1.0 release
2026-07-13 21:12:39 +00:00
.github/workflows Check packages build on OCaml 4.14 in CI 2026-07-13 21:12:39 +00:00
cli Read CLI version from build info and add CLI cram tests 2026-07-13 21:12:32 +00:00
docs Fix CLI reference and simplify documentation 2026-07-13 21:12:39 +00:00
examples Align api to load, instead of create and add _exn to all variants 2026-04-14 17:16:50 +00:00
lib Fix CLI reference and simplify documentation 2026-07-13 21:12:39 +00:00
test Deploy API docs and ANSI/tokens/dual-theme previews to Pages 2026-07-13 21:12:39 +00:00
vendor Use ocamlformat.0.29.0 for vendor 2026-04-14 19:19:39 +00:00
.gitignore Push to gh 2026-02-25 16:36:55 +00:00
.ocamlformat Use ocamlformat.0.29.0 and align documentation 2026-04-14 18:29:05 +00:00
AGENTS.md delete useless comments 2026-03-09 15:46:03 +00:00
CHANGES.md Prepare 1.1.0 release 2026-07-13 21:12:39 +00:00
dune Fix ci 2026-04-12 17:30:27 +00:00
dune-project Prepare 1.1.0 release 2026-07-13 21:12:39 +00:00
LICENSE Push to gh 2026-02-25 16:36:55 +00:00
Makefile Vendor oniguruma + textmate-language 2026-04-09 07:59:11 +00:00
ochre-cli.opam Prepare 1.1.0 release 2026-07-13 21:12:39 +00:00
ochre.opam Prepare 1.1.0 release 2026-07-13 21:12:39 +00:00
README.md Fix CLI reference and simplify documentation 2026-07-13 21:12:39 +00:00
README.mld Fix CLI reference and simplify documentation 2026-07-13 21:12:39 +00:00

ochre

A syntax highlighter for OCaml using TextMate grammars and themes, inspired by Shiki.

docs/video/ochre-examples.gif

Installation

opam install ochre -y

Quick start

let theme = Ochre.Theme.nord in
let highlighter = Ochre.load_from_files_exn ["path/to/ocaml.tmLanguage.json"] in
let html = Ochre.to_html highlighter ~theme ~lang:"ocaml" source_code

Load any TextMate grammar with tm-grammars

tm-grammars packages a collection of TextMate grammars for OCaml, so you can load grammars in ochre without managing JSON files on disk.

Install the bundled grammar package:

opam install tm-grammars -y

Then load a grammar from OCaml and create a highlighter:

let theme = Ochre.Theme.nord in
let highlighter = Ochre.load_exn [ ("ocaml", Tm_grammars.ocaml) ] in
let html = Ochre.to_html highlighter ~theme ~lang:"ocaml" "let x = 42"

Output formats

ochre can render highlighted code to several output formats (also called backends):

  • 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. See rendered example.
  • SVG — Standalone <svg> elements with monospace <text> and per-token <tspan> styling. Open preview: SVG rendered example. Source: .svg file.
  • ANSI — 24-bit color escape sequences for terminal display.
  • LaTeX\textcolor commands inside an ochrehighlight environment. Requires the xcolor and soul packages. See rendered PDF (source: .tex file). All backends share the same interface: create a highlighter, pick a theme, and call the corresponding to_* function.

Documentation

Contribute

Set up the development environment:

make init

Build the project:

make build

Run the test suite:

make test

You can also preview highlighted output in a browser with make test-browser, which serves a sample on port 5000.

Credits