exortech.com

Peripatetic thinking

Syntax highlighting Ruby code

February11

Since setting up this blog, I have been experimenting with various ways to display code on this blog. I started out trying some of the Wordpress plugins like SyntaxHighlighter that used the Google code javascript library, but I wasn’t very happy with the result.

Looking around, it seems like the Ruby community generally just handles code formatting using the Syntax module. Here’s the code that I have put together to handle formatting code for this blog:

#!/usr/bin/ruby

require 'rubygems'
require 'syntax/convertors/html'

def convert(code)
  convertor = Syntax::Convertors::HTML.for_syntax "ruby"
  '<pre class="ruby">' + convertor.convert(code, false) + '</pre>'
end

if ARGV.size > 0
  code = convert(ARGF.read)
  File.open(ARGF.path + ".html", "w").write(code)
else	# use clipboard if no file is specified
  code = convert(`pbpaste`)
  IO.popen("pbcopy", "w") { |clipboard| clipboard.print code }
end

One thing that I found that I had to be careful with, however, when pasting code into the WordPress editor is that it has a tendency to remove all carriage returns from pasted markup. Obviously this isn’t a problem normally, but when dealing with preformatted <pre> tags, it tends to break the formatting. It looks like most WordPress users who are comfortable with HTML just disable the visual editor.

posted under technology | No Comments »

RubyCamp Vancouver

January30

Last weekend I attended RubyCamp in Vancouver. The sessions were high quality and, as a ruby n00b, it was a great opportunity to learn from the community.

I started the morning by attending Alexey’s JRuby overview. It was interesting to get some insight into the problems the JRuby team has faced mapping between Ruby types and Java types. This certainly seems to be better addressed by the .NET DLR which should simplify matters for dynamic language implementations in .NET.

I also spent some time chatting some guys working on Rubinius. They had some pretty strong opinions about ZenTest and AutoTest over Test::Unit and CruiseControl.rb respectively. Of course, they were slightly biased, but it deserves a closer look.

I talked with Marc Mayo about Git, distributed teams and his company Joyent. He ran an insightful session on the trials and tribulations of hosting high traffic Rails applications.

The last two sessions that I attended were one on Merb (an alternative web framework to Rails) and Scott Patten’s Rubyize This!