Rack::Codehighlighter
About
The Rack::Codehighlighter is a middleware which allows for easy connecting a code highlighter of somebody's choice to an HTML page containing pieces of programming code. Parameters to Rack::Codehighlighter are: the name of a highlighter and a specification of how to find the pieces of code in the page.
See also Github for this middleware + more usage examples + a gem.
Usage
For example, to colorize code in pre elements with well known coderay highlighter use the following:
require 'coderay'
use Rack::Codehighlighter, :coderay,
:element => "pre",
:pattern => /\A:::(\w+)\s*\n/
The first parenthesized expression from the pattern /\A:::(\w+)\s*\n/ will be used to match the language name. For example, from the pre element below
<pre>:::ruby
printf "hello world"
</pre>
the ruby name will be extracted and the coderay Ruby scanner will be used to highlight the second line.
The code below implements three most popular Ruby code highlighters of today: coderay, syntax and ultraviolet.
I also included a simple sinatra application (app.rb) and css stylesheet (coderay.css). Run the application with:
ruby app.rb
and check the results at:
http://localhost:4567/
