Improve syntax highlighting through an external engine #58

gasche opened this Issue Sep 27, 2012 · 6 comments


None yet
4 participants

gasche commented Sep 27, 2012

I think the current highlighting of OCaml code is really subpar. It should be easy to improve it, either by

  • using an highlighting engine on the server side (I have had good results with Pygments for example; would you accept such an external dependency? of course any other highlight engine would be fine)
  • or by inserting Javascript to do the highlighting on client side (there are a lot of such client-side renderers)

agarwal commented Sep 28, 2012

We have thus far avoided anything requiring server side computations
because we haven't settled on a server and wanted to simplify deployment.
Xavier just approved use of the domain, so that moves us one step
closer to finalizing a hosting solution. It should be figured out soon, and
we can then start doing fancier things.

However, in this case a Javascript solution seems better anyway. My
question on StackOverflow [1] was answered with a suggestion of google's
prettify with the lang-ml.js extension. I haven't had a chance to try it
yet. Do you know what's best?



Chris00 commented Sep 28, 2012

Quick notes:

  1. We used an external tool for a short period of time and the output was (even) worse than now — and compiling the site was taking way more time.
  2. Could you be more specific on things that may be improved? I fed the first snippet of to Pygments and I am not so much impressed (the colors do not really help readability):
  3. A javascript engine sounds like a good solution (especially if it is configurable on the user side) but somebody has to do the work of comparing the various possibilities to pick up the better candidate. We're happy to accept help for this!

samoht commented Sep 28, 2012

A quick note as well. There at least 2 OCaml tools to colorize OCaml code:


gasche commented Sep 28, 2012

caml2html's output seems fine to me, and iirc. xmlm is already a dependency of so ocaml-cow would be fine as well (it looks a bit overkill for simply syntax highlighting, but then maybe Cow's escaping capabilities could be useful for

That said, now that I'm looking at it on the website, I agree that there are not much differences between the various rendering (the current one and Pygments for example). The difference I observed are on the rendering of the "minimum viable snippet" that I added in this commit; apparently the current highlighter doesn't cope so well with several non-;;-separated phrases.

Ashish, I'm not sure client-side rendering is the best option. The pros is that it's easy to deploy and keeps the HTML rather clean (you have the non-rendered source, which is nice), but the cons is that it makes me cringe at the idea of CPU time being wasted by all the readers rendering the code each time they visit the page, instead of one static server-side computation (one lesser issue is that the rendering is not done in syndication feed readers, if you publish some of your content in RSS or Atom format; I don't think is concerned with that). I've considered both options for gagallium and finally decided in favor of server-side rendering.


Chris00 commented Sep 28, 2012

@gasche: I am working on integrating your patch. I will fix the highlighting of the snippet at the same time.

The current highlighting engine is a quick hack to remove the slow external program and have something that looks reasonable. There is no doubt that it can be improved. Maybe Cow is a good solution — more details would be welcome (I do not have time to look deeper into it ATM).


Chris00 commented Aug 15, 2014

See also #286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment