Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Syntax highlighting and Ocaml/PHP integration
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-10-01 (15:28)
From: Dave Benjamin <dave@r...>
Subject: Re: [Caml-list] Syntax highlighting and Ocaml/PHP integration
Dario Teixeira wrote:
> I'm looking for a GPL-compatible syntax highlighting library with support
> for most common programming languages and markups.  Obviously I would
> prefer a native Ocaml library, though something in C would also be
> acceptable due the relative ease of writing bindings.

I have had decent results opening a pipe to GNU source-highlight. I'm 
mainly using it on JSON, so I can't vouch for its support of other 
languages but it seems pretty comprehensive.

let pipe program input =
   let (in_channel, out_channel) = Unix.open_process program in
   output_string out_channel input;
   close_out out_channel;
   let result = ref [] in
       while true do
         result := input_line in_channel :: !result
     with End_of_file -> ()
   ignore (Unix.close_process (in_channel, out_channel));
   String.concat "\n" (List.rev !result)

let pre_body = Pcre.regexp ~flags:[`DOTALL] ".*<pre>(.*)</pre>.*"

let source_highlight lang code =
   let result = pipe ("source-highlight -s " ^ lang) code in
   Pcre.replace ~rex:pre_body ~templ:"$1" result

Caveat: The "pipe" function above will block on large inputs due to 
buffering deadlock. It should probably be rewritten using