CodeRay Syntax Highlighter As a Rails Plugin

by Jarrett Colby

This article is outdated. It's preserved just for the sake of having an archive. Rather than exporting all the CodeRay source into your plugins directory, the modern approach is to simply load the CodeRay gem in environment.rb. You can then put the helper in your app/helpers directory.

So, just to be clear: the steps below are no longer recommended.

First, you'll need the CodeRay source. The Gem is supposedly out-of-date, so I'd recommend checking CodeRay out of Subversion instead:

mkdir vendor/plugins/coderay
cd vendor/plugins/coderay
svn export svn://rubyforge.org//var/svn/coderay/trunk/coderay/trunk/lib/

CodeRay now resides in vendor/plugins/coderay/lib—just where we want it. Now create a very simple init.rb in vendor/plugins/coderay:

require 'coderay'

Don't forget to restart your server.

The only thing left is to call CodeRay in your views. For that, I use Jerrett Taylor's helper. Note that this helper uses its fair share of CPU cycles, so you should probably use some form of caching if at all possible.

If you want to integrate it with Mephisto, read Code Syntax Highlighting in Mephisto with CodeRay.