package highlexer

All checked builds pass
  1. Overview
  2. Docs
An OCaml syntax highlighter based on the OCaml lexer

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.tar.gz
md5=5e6b2b4fa38383e0b3bc3a96be929a66
sha256=9146a570641406dd8325682f57ce6062ad770f2dbe1fe5180ff607171e69bee2

Description

An OCaml syntax highlighter based on the OCaml lexer

Tags

js syntax highlighting

Added to opam-repository:

README

Highlexer

This library provides a way to highlight OCaml source. It is designed for use with js_of_ocaml, but does not have to be used this way.

Because the jsoo code elimination does not work well with the compiler-libs, this library uses a modified version of the OCaml lexer. This version has worse error messages, and docstrings simplified, because these features required vendoring much more files from the OCaml source code.

Here is an example of how to use highlexer with js_of_ocaml and brr :

open Brr

let ( .%{} ) obj prop = Jv.get obj prop

let ( !% ) = Jstr.of_string

let highlight_snippet el =
  Console.log [!%"highlighting"; el] ;
  let src =
    (El.to_jv el).%{"innerHTML"} |> Jv.to_string |> Highlexer.unescape
  in
  let annotated = Highlexer.of_string src in
  let html =
    List.map
      (fun Highlexer.{tag; content} ->
        match tag with
        | Some tag ->
            El.(span ~at:At.[class' !%tag] [txt !%content])
        | None ->
            El.txt !%content )
      annotated
  in
  El.set_children el html

let () =
  let ocaml_snippets = Brr.El.find_by_class !%"language-ocaml" in
  Console.log ["ocaml snippets"; ocaml_snippets] ;
  List.iter highlight_snippet ocaml_snippets

This example had a size of 168K. This may improve a little bit with new versions of `js_of_ocaml``, but the lexing table itself is 104K, so it will never be super small like a highlighter with just a few regexp could.

If you are bothered by the size, it is perfectly fine to use this in the backend instead.

Dependencies (3)

  1. menhir >= "20230608"
  2. dune >= "3.11"
  3. ocaml >= "4.10"

Dev Dependencies (2)

  1. odoc with-doc
  2. alcotest with-test

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building highlexer with each compiler on check.ci.ocaml.org, run of against opam-repository 7cdc698. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.1 (this version) good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure