How do you syntax-highlight Clojure code for display on a website? The best way I can find is SyntaxHighlighter.
Daniel Gómez wrote a brush to give SyntaxHighlighter Clojure support. I tweaked it a bit myself and integrated it into cow-blog. I also converted my favorite color scheme to a SyntaxHighlighter theme. So when I write this code:
(defn- ip "Given a request, return the IP. Looks for an x-forwarded-for header, falls back to :remote-addr on the request." [request] (or (get-in request [:headers "x-forwarded-for"]) (request :remote-addr)))
You should see something like this:
One bad thing about SyntaxHighlighter is that it doesn't play nicely with Markdown. Or rather, Markdown isn't powerful enough to let you specify the class of any markdown-generated HTML tags. If you want the
<pre class="brush: clojure"> that SyntaxHighlighter requires, you have to write out the HTML by hand. But I hacked Showdown a bit to let me specify classes more easily, so I can avoid having to do that.
The code for all of this is on github with the rest of my blog.