Installing Ultraviolet and Oniguruma
First install Oniguruma
Oniguruma is a regular expression engine that Ultraviolet uses to parse text; Ruby also uses Oniguruma by the way. If you don’t have Oniguruma on your system you’ll get this error while installing Ultraviolet (at least on Ubuntu Linux):
1 oregexp.c:2:23: error: oniguruma.h: No such file or directory
This tells you that you should download and install Oniguruma. For me version 5.8.0 was the only version that worked, so execute this command to get the right version of Oniguruma:
1 $ wget http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.8.0.tar.gz
You now have the source package on your computer, so decompress it with the following command:
1 $ tar zxvf onig-5.8.0.tar.gz
If everything went fine, change current directory:
1 $ cd onig-5.8.0/
Next, run configure:
1 $ ./configure
Watch the output closely and fix any errors reported, then run make:
1 $ make
To build and install Onigurama run:
1 $ sudo make install
I managed to get the following errors from Ultraviolet with other versions of Oniguruma, but these went away after installing 5.8.0 and re-installing Oniguruma:
1 Parsing error in // ==UserScript==: wrong number of arguments (2 for 0) #<Textpow::SyntaxNode:0xb7c91780>
Installing Ultraviolet and dependencies
Next install Ultraviolet with RubyGems:
1 $ sudo gem install -r ultraviolet --include-dependencies 2 3 Select which gem to install for your platform (i486-linux) 4 1. oniguruma 1.1.0 (mswin32) 5 2. oniguruma 1.1.0 (ruby) 6 3. Skip this gem 7 4. Cancel installation 8 > 2 9 Building native extensions. This could take a while... 10 Successfully installed ultraviolet-0.10.0 11 Successfully installed textpow-0.9.0 12 Successfully installed oniguruma-1.1.0 13 Successfully installed plist-3.0.0
Test that Ultraviolet works by running the following code with irb:
1 $ irb 2 3 require 'rubygems' 4 require 'uv' 5 puts Uv.syntaxes.join( "\n" ) 6 puts Uv.themes.join( "\n" ) 7 input = <<HTML<<HTML 8 <html> 9 <body> 10 </body> 11 </html> 12 HTMLHTML 13 14 puts Uv.parse( input, "xhtml", "html", true, "slush_poppies")
Problems
You might get this error:
1 require 'uv' 2 LoadError: libonig.so.2: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/oniguruma-1.1.0/lib/oregexp.so
This message is a bit confusing. It means Ruby can’t find libonig.so.2, not oregexp.so as you could believe.
To fix this, check if the library has been linked:
1 $ ldconfig -p|grep libonig
If the library is not linked, add the path to the directory where the file is located to /etc/ld.so.conf:
1 /usr/local/lib 2 3 include /etc/ld.so.conf.d/*.conf
Then run:
1 $ ldconfig
Another way of fixing this problem would be to tell the build script to install it to /usr/lib.
Syntax highlighting code with Ruby and Ultraviolet
This site uses Ultraviolet for syntax highlighting. Ultraviolet supports 50 languages and 20 themes, so it’s sometimes difficult to pick the best theme, which is why I created the following code snippet that renders the code in all available themes and creates an HTML page displaying them all:
1 require 'uv' 2 3 language = ARGV[1] 4 input_file = ARGV[0] 5 line_numbers = true 6 output_format = "xhtml" 7 8 input = File.read(input_file) 9 10 # Render input using all available themes 11 syntax_highlighted_code = "" 12 Uv.themes.each do |theme| 13 syntax_highlighted_code << "<h2>#{theme}</h2>" 14 syntax_highlighted_code << Uv.parse( input, output_format, language, line_numbers, theme) 15 end 16 17 # Template for output file 18 page_template = <<HTML_DOC 19 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 20 21 <html> 22 <head> 23 <title>test</title> 24 #{Uv.themes.map{|theme| %Q(<link rel="stylesheet" type="text/css" href="css/#{theme}.css" />\n)}} 25 <script type="text/javascript" src=""></script> 26 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 27 </head> 28 <body> 29 #{syntax_highlighted_code} 30 </body> 31 </html> 32 HTML_DOC 33 34 # Write highlighted text to output 35 File.open("output.html", "w") do |file| 36 file << page_template 37 end
Yes, using ERB templates would be better but interpolated strings work just fine… Save the code in highlight.rb and run it as follows:
1 highlight.rb ruby input_file.rb