Main Software Blog Other

Caml2html [difficulty = 1 camel]


The Caml2html package provides an executable and a library that highlight OCaml code by adding some color. With the caml2html program, you can produce ready-to-go HTML files such as this one.

You can also use the library in a program that makes web pages. See that:

(* This small program is inserted verbatim in the source code of this
   web page *)

open Printf

let rec fac = function 
    0 -> 1 
  | n -> n * fac (n - 1)

let () =
  for i = 0 to 10 do
    printf "%i -> %i\n" i (fac i)

See also this cool example by the authors of MLPost. It embeds arbitrary HTML (requires version > 1.3.0) with images and javascript.

Quick reference guide

See the help file which comes with the distribution.

More about custom comment handlers

Consider a file containing the following OCaml code:

(*include disclaimer.html *)
let f x = 2 * x + 1

File disclaimer.html contains this:

Warning, this is a test!

Rendered with caml2html without using the -ext option it gives:

(*include disclaimer.html *)
let f x = 2 * x + 1

We can define a custom comment expander that reads the contents of the comment from stdin and outputs some HTML to stdout. In our example we want to insert the contents of a file named disclaimer.html. This is done by defining a command associated with "include", as follows:

$ caml2html -ext "include: xargs cat"

The result is:

Warning, this is a test!
let f x = 2 * x + 1


Caml2html was written by Sébastien Ailleret and is now maintained by Martin Jambon (me). It is distributed under a GPL license. Download it here.

The development version of Caml2html is hosted on GitHub.

LaTeX output

Caml2html provides LaTeX output since version 1.4.0 (-latex) and works very similarly to the default HTML mode. It uses LaTeX packages alltt and color.

Exporting to other formats

To PostScript, HTML, RTF or ANSI terminal: Ocaml support for Enscript ( by Janne Hellsten)

To PostScript: Ocaml support for a2ps (ocaml.ssh by Markus Mottl)