Sunday, August 26, 2007

Formatting Haskell

I've been experimenting with formatting Haskell for this blog and thought I would summarize my conclusions.

My needs are unsurprising: I want to write literate Haskell (LHS), have it look nice on the blog, and have the process be easy (in other words, mostly automated).

One possibility is to take the LHS, wrap it in <pre> tags and post it. This works, but I was curious to try hscolour. The current version (I got mine directly from darcs) supports LHS. A nice feature of hscolour is that it can output HTML with the formatting in the form CSS classes. Thus, I could factor out the appearance of my Haskell code into the embedded stylesheet of my blog template.

This left me with just one minor quibble. I wanted to hide the "> " with which every line of code in LHS starts. Unfortunately the current implementation of hscolor treats these as a regular Haskell token, so they get styled like any other operator. So after a little hacking of the hscolour source, I had a new hscolour that did exactly what I wanted.

The color scheme is nothing to write home about, but while tinkering with it I did discover a small quirk of how browsers interpret CSS (it might not even be a quirk -- I should read the W3C spec). I wanted to style "> " with display:none. However with that property, if you cut and paste the post, you lose the "> "s. So instead I just made them the same color as the backgound.

Finally I used an OMake script to automate compiling the code and converting it into HTML.

If I had a great deal of spare time, the following would be nice.

  • Integrate Pandoc into the process so my LHS can look more like text and yet be mechanically converted to pleasant HTML.
  • Automate the upload process using the blog's XML API. Others have already done something similar.

No comments: