Clojure syntax highlighting via SyntaxHighlighter

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:

Syntax highlighting example

...unless you're reading this via RSS, or in a browser without Javascript enabled, in which case you'll see plain, depressing black and white. But that's one nice thing about SyntaxHighlighter. It degrades nicely.

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.

July 21, 2010 @ 8:17 AM PDT
Cateogory: Programming

3 Comments

Ronnie
Quoth Ronnie on July 22, 2010 @ 8:21 AM PDT

There is also pygments support for clojure (and pretty much every language under the sun), or you could use htmlize from emacs.Im personally leaning towards one of those for a blog engine that im planning on writing (no offense, just looks like a decent language intro). The other idea (naturally stolen) is to just gist's, which use pygments under the hood as well

Brian
Quoth Brian on July 22, 2010 @ 11:08 AM PDT

htmlize in Emacs was one thing I definitely considered, but right now I have a clean separation between nice-looking Markdown and ugly-looking generated HTML, which would be ruined if I was copy/pasting code from Emacs. Plus it'd be impossible to change later. With a JS plugin I can easily drop in a different library later.

pygments is Python-only, right? I looked at it a long while back, and it seemed like it'd be pretty clunky to integrate into my site. Admittedly using JS from Rhino in Clojure is also clunky.

Writing a blog is definitely a good intro to a language. Blogs are the "Hello, World" of the modern age, in my opinion. Good luck. Let me know once you get it running, I'd like to see another approach to writing a Clojure site.

Joshua Ayson
Quoth Joshua Ayson on December 01, 2011 @ 1:11 PM PST

Thanks Brian, your article helped me get the Clojure code on my blog looking spiffy. Props to you!

Speak your Mind

You can use Markdown in your comment.
Email/URL are optional. Email is only used for Gravatar.

Preview