RadiantCMS Extension for Syntax Highlighting via CodeRay

February 03, 2009

I recently converted this website to using RadiantCMS. Many of my articles include code samples. In the past I had written a plugin for webgen to use CodeRay to provide syntax highlighting. There is an extension that does it using Python’s Pygments, but I didn’t want to have to install Python just for syntax highlighting. So, borrowing that extension’s radiant tags I implemented the same thing using CodeRay.

Here is a sample:

tag 'code' do |tag|
    lang = tag.attributes['language'] || 'plaintext'
    line_numbers = tag.attributes.key?('line_numbers') ? :inline : nil
    return CodeRay.scan(tag.expand.to_s.strip, lang.to_sym).html(:wrap => :div, :line_numbers => line_numbers )
  end

You can get it here.