Rack Middleware for Code Highlighting

The Codehighlighter gem provides a thin interface over a bunch of exisitng code highlighters to make their usage as generic possible.

To install it, run:

sudo gem install wbzyl-codehighlighter-middleware -s http://gems.github.com

Markup your code with:

<pre><code>:::ruby
...
</code></pre>

Example (incomplete html, needs a layout file with link to css):

# file simple.rb

require 'rubygems'
require 'sinatra'
require 'coderay'    # use the Coderay highlighter

gem 'wbzyl-sinatra-rdiscount'
require 'wbzyl-codehighlighter-middleware'

use Rack::Codehighlighter, :coderay

get "/" do
  erb :hello
end

An example

The Codehighlighter follows the same syntax as regular Markdown code blocks, with one exception. It needs to know what language to use for the code block.

If the first line begins with three colons, the text following the colons identifies the language (ruby in the example). The first line is removed from the code block before processing.

Run the above example with:

ruby simple.rb

The directory examples contains ready to run, simple Sinatra app. Try

rackup -p 4567 config.ru

or better yet (requires the thin gem to be installed):

thin --rackup config.ru -p 4567 start

Finally, visit the following url:

http://localhost:4567

and contemplate the sheer beauty of the rendered code.

Supported Highlighters

These currently include: Syntax (fast), Coderay (very fast), Ultraviolet (slow).

Syntax

Supported languages:

  • xml
  • ruby

I added support for these languages:

  • ansic
  • javascript
  • css21
  • sqlite

Coderay

Supported languages:

  • C
  • CSS
  • Delphi
  • diff
  • HTML
  • RHTML (Rails)
  • Nitro-XHTML
  • Java
  • JavaScript
  • JSON
  • Ruby
  • YAML

Google Code Prettify, pure Javascript

Supported languages:

  • css, lisp, hs, lua, sql, vb, wiki,
  • bsh, c, cc, cpp, cs, csh, cyc, cv, htm, html,
  • java, js, m, mxml, perl, pl, pm, py, rb, sh,
  • xhtml, xml, xsl

Ultraviolet

Needs oniguruma regexp library. Installation instruction for Oniguruma: Carbonica

Supported languages:

  • actionscript
  • active4d
  • active4d_html
  • active4d_ini
  • active4d_library
  • ada
  • antlr
  • apache
  • applescript
  • asp
  • asp_vb.net
  • bibtex
  • blog_html
  • blog_markdown
  • blog_text
  • blog_textile
  • build
  • bulletin_board
  • c
  • c++
  • cake
  • camlp4
  • cm
  • coldfusion
  • context_free
  • cs
  • css
  • css_experimental
  • csv
  • d
  • diff
  • dokuwiki
  • dot
  • doxygen
  • dylan
  • eiffel
  • erlang
  • f-script
  • fortran
  • fxscript
  • greasemonkey
  • gri
  • groovy
  • gtd
  • gtdalt
  • haml
  • haskell
  • html
  • html-asp
  • html_django
  • html_for_asp.net
  • html_mason
  • html_rails
  • html_tcl
  • icalendar
  • inform
  • ini
  • installer_distribution_script
  • io
  • java
  • javaproperties
  • javascript
  • javascript_+_prototype
  • javascript_+_prototype_bracketed
  • jquery_javascript
  • json
  • languagedefinition
  • latex
  • latex_beamer
  • latex_log
  • latex_memoir
  • lexflex
  • lighttpd
  • lilypond
  • lisp
  • literate_haskell
  • logo
  • logtalk
  • lua
  • m
  • macports_portfile
  • mail
  • makefile
  • man
  • markdown
  • mediawiki
  • mel
  • mips
  • mod_perl
  • modula-3
  • moinmoin
  • mootools
  • movable_type
  • multimarkdown
  • objective-c
  • objective-c++
  • ocaml
  • ocamllex
  • ocamlyacc
  • opengl
  • pascal
  • perl
  • php
  • plain_text
  • pmwiki
  • postscript
  • processing
  • prolog
  • property_list
  • python
  • python_django
  • qmake_project
  • qt_c++
  • quake3_config
  • r
  • r_console
  • ragel
  • rd_r_documentation
  • regexp
  • regular_expressions_oniguruma
  • regular_expressions_python
  • release_notes
  • remind
  • restructuredtext
  • rez
  • ruby
  • ruby_experimental
  • ruby_on_rails
  • s5
  • scheme
  • scilab
  • setext
  • shell-unix-generic
  • slate
  • smarty
  • sql
  • sql_rails
  • ssh-config
  • standard_ml
  • strings_file
  • subversion_commit_message
  • sweave
  • swig
  • tcl
  • template_toolkit
  • tex
  • tex_math
  • textile
  • tsv
  • twiki
  • txt2tags
  • vectorscript
  • xhtml_1.0
  • xml
  • xml_strict
  • xsl
  • yaml
  • yui_javascript