2005-12-08

 

Colourising code

If anyone is interesting in colourising Haskell code to paste into HTML on your blog (as in my previous entry below), it was done with a simple Haskell program (of course). HsColour is a little script I knocked up a few years back. It uses an intermediate mini-language of highlighting styles, and currently has two backends, one for HTML and another for ANSI terminal screen codes. I'm sure it would be simple to add other backend rendering formats - maybe CSS - with a tiny bit of effort. If you do modify it, or otherwise find it useful, please let me know.

The history of HsColour is vaguely interesting, because it was essentially a spin-off of my (winning) entry in the 0th International Obfuscated Haskell Contest - remorse. If you haven't seen remorse before, I'll try not to spoil the fun of discovering it, but suffice to say, it requires a mini-lexer for Haskell code, which I ripped out and re-used in HsColour. Don't worry - that isn't the obfuscated bit.

Comments:
It's not Windows compliant, since you hardcode /tmp into the program! Since Run.hs only works on Unix, and is only required for ANSI escape codes (which Windows doesn't have) commenting this bit out was sufficient to get it working. If you put up a darcs repo i'll send a patch :)

I was going to modify the documentation for Yhc to put up colourised sources, so I'll probably use this for that. I was hoping someone would have done something like http://lxr.mozilla.org/mozilla1.8/source/security/manager/ssl/src/nsCrypto.cpp for Haskell, ideally integrated with Haddock, but it seems no one has :(
 
btw, if its in darcs i'll also add a CSS mode, since outputting HTML with colour information is just wrong. It also allows you to drop the .hscolour when using CSS, and you can do more fancy feature - like graphics and watermarks if you wanted to.
 
A darcs repository is now available at
http://www.cs.york.ac.uk/fp/darcs/hscolour
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?