Module: Rack::Coderay

Defined in:


CodeRay Syntax Highlighter Rack Appliance

Rack::Coderay parses text/html markup and replaces code with output from the CodeRay gem (, producing syntax highlighting. By default, this component looks for <pre lang=“xxxx”>…</pre> blocks, where 'xxxx' is any of the languages supported by CodeRay (e.g. 'ruby'). See

You can re-configure Rack::Coderay to look for a different trigger but it needs to have a 'lang' attribute.


Within a rackup file (or with Rack::Builder):

require 'rack/coderay'
use Rack::Coderay
run app

Rails example:

# above Rails::Initializer block
require 'rack/coderay'

# inside Rails::Initializer block 
config.middleware.use Rack::Coderay

To override the default markup trigger (<pre lang=“xxxx”>), you can pass in a css or xpath selector as the second argument:

config.middleware.use Rack::Coderay, "//div[@lang]"

To set additional CodeRay gem options, pass a hash as the third argument:

config.middleware.use Rack::Coderay, 
  :line_numbers => :table

Defined Under Namespace

Classes: Parser

Class Method Summary collapse

Class Method Details

.new(backend, markup_trigger = "//pre[@lang]", options = {}) ⇒ Object

Create a new Rack::Coderay middleware component that automatically highlights syntax using the CodeRay gem.

# File 'lib/rack/coderay.rb', line 43

def, markup_trigger = "//pre[@lang]", options = {}), markup_trigger, options)