Ruby gems for highlighting code
Some time ago I rebuilt my blog to use excellent Radiant CMS. So, I met a problem of highlighting code in my blog. Now I’m using a highlight.js library, but thinking for server-side solution.
Therefore, today I’ll tell about three ruby gems for code highlighting:
Also I measured their performance and results may be found at the end of article.
Syntax
Project page: http://syntax.rubyforge.org/
Supported languages: Ruby, XML, YAML
Advantages: Rails plugin and manual on using it in Radiant exists.
Drawbacks: There are too few supported languages and themes.
Installation:
gem install syntax
Usage example:
require 'rubygems'
require 'syntax/convertors/html'
convertor = Syntax::Convertors::HTML.for_syntax “ruby”
html = convertor.convert( File.read( “program.rb” ) )
puts html
Besides code highlighting, this gem may be used for lexical analysis of source code for some further processing:
require 'syntax'
tokenizer = Syntax.load "ruby"
tokenizer.tokenize( File.read( "file.rb" ) ) do |token|
puts "group(#{token.group}, #{token.instruction}) lexeme(#{token})"
end
P.S: Before publication i found a theme for Syntax, which looks like GitHub code highlighting.
UltraViolet
Project page: http://ultraviolet.rubyforge.org/
Supported languages: C, C++, Ruby, Bibtex, Latex, Diff, HTML, CSS and many others
Advantages: It use TextMate syntax files and so supports a lot of different languages.
Drawbacks: Documentation is very poor. It has a lot of dependencies and works slower than others. When rendering line numbers and copying rendered code, line numbers copy with it.
Installation:
gem install ultraviolet
During installation i had one problem: Ultraviolet depends on gem Oniguruma, which requires library Oniguruma installed in your system with headers. In Ubuntu you can install it from package repositories:
sudo apt-get install libonig2 libonig-dev
For other systems there is a good manual here.
Usage example:
require 'rubygems'
require 'uv'
result = Uv.parse( text, "xhtml", "ruby", true, "amy")
Method arguments:
- text – source code
- “xhtml” – output syntax
- “ruby” – input language
- true – render or not line numbers
- “amy” – theme, which to usse
CodeRay
Project page: http://coderay.rubychan.de/
Supported languages: Ruby, C, Delphi, HTML, RHTML (Rails), Nitro-XHTML, CSS, Diff, Java, JavaScript, JSON, YAML
Advantages: It’s fastest gem in my performance measurements. Also, it has a number of options, allowing to customize output.
Drawbacks: Built-in themes looks poor, especially for C and Java.
Installation:
gem install coderay
Usage example:
require 'rubygems'
require 'coderay'
tokens = CodeRay.scan( text, :ruby )
print tokens.div( :line_numbers => :table, :css => :class, :style => :cycnus )
Highlighting methods (div,html,..) take following options:
- :tab_width – tabulation width in spaces. Default: 8
- :css – how to include the styles (:class и :style). Default: :class)
- :wrap – wrap result in html tag :page, :div, :span or not to wrap (nil)
- :line_numbers – how render line numbers (:table, :inline, :list or nil)
- :line_number_start – first line number
- :bold_every – make every n-th line number bold. Default: 10
CodeRay, as Syntax may be used to analyze source code, because object Tokens is a list of tokens with specified types.
Performance comparison
I wrote simple benchmark for these libraries, which may be found at http://gist.github.com/85970.
Below you can see measurement results while processing code fragments in Ruby and Xml:
user system total real
ruby ultraviolet: 10.270000 0.600000 10.870000 ( 11.584088)
ruby coderay: 0.710000 0.030000 0.740000 ( 0.923616)
ruby syntax: 2.030000 0.130000 2.160000 ( 2.830979)
xml ultraviolet: 3.280000 0.190000 3.470000 ( 3.793856)
xml coderay: 0.540000 0.000000 0.540000 ( 0.701140)
xml syntax: 0.660000 0.060000 0.720000 ( 0.854120)
And graph:

You can see, what CodeRay is fastest, and UltraViolet is slowest. However, i think that poor performance of UltraViolet is repaired by huge amount of supported languages and themes.
