Nullcreations.net | Ramblings & Photography of Jerrett Taylor

CodeRay helper for Rails

May 10th, 2007 | 5 comments

While I'm using Markdown for formatting blog entries, I wanted to have some decent syntax highlighting support as well. After looking around I decided to use CodeRay. It's fast, customizable, has nice output, and is easy to extend.

All I had to do to make it work with Rails was to create a helper. I wanted to have a simple syntax so I could do stuff like:

<code:ruby> ... code here ... </code>

The helper isn't very complicated:

1
2
3
4
5
6
def parse_coderay(text)
  text.scan(/(<code\:([a-z].+?)>(.+?)<\/code>)/m).each do |match|
  text.gsub!(match[0],CodeRay.scan(match[2], match[1].to_sym).div( :line_numbers => :table,:css => :class))
  end
  return text
end

Then to add the CSS, I added this to my main layout:


style { CodeRay::Encoders[:html]::CSS.new(:jerrett).stylesheet }

or for non-markaby users...

1
2
3
<style>
<%= CodeRay::Encoders[:html]::CSS.new(:jerrett).stylesheet %>
</style>

If you call CSS.new without an argument it will use the default style, which is almost identical to the one I'm using here. I just changed a few colors to make it fit the site better. In the coderay/styles/ directory are two ruby files, cycnus.rb and murphy.rb - these define the CSS styles. By default it will use cycnus, and murphy is a black-based theme. Easiest way to make a new theme is just copy one of the existing to a new .rb file, and tweak.

The only thing missing was JavaScript syntax highlighting support, by I found a pastie by Josh Goebel of a JavaScript CodeRay Scanner

All you need to do is add that to a javascript.rb file in the coderay/scanners folder, and viola, JavaScript support!

5 Comments

Posted by August Lilleaas 8 months ago.

Seriously useful stuff! All the suggestions on how to do this I've seen so far has been 50-liners. This rocks.

Posted by Joey Schoblaska 5 months ago.

Great tutorial! If you want to prevent the parser from adding a blank line of code to the beginning of every code block, add the following line to the helper (right before text.gsub!)

match[1] = match[1][2..match[1].length] if match[1][0..1].match(/\n/)

Posted by Joey Schoblaska 5 months ago.

Rather, that's

match[2] = match[2][2..match[2].length] if match[2][0..1].match(/\n/)

forgot that I had modified the helper already ;)

Posted by Matt Coneybeare 5 months ago.

What about blank lines at the end of every code block?

Posted by coneybeare 5 months ago.

An easier way to get rid of the lines is to use match[2].strip on line 3, thats it!

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals