Repository is archived
No commit activity in last 3 years
No release in over 3 years
Rack Middleware for syntax highlighting.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

>= 4.0.0
>= 1.4.0
>= 1.0.0
 Project Readme

Rack::Highlighter Version Build Status Abandoned

Rack Middleware that provides syntax highlighting of code blocks, using the CodeRay, Pygments, Rouge or Ultraviolet gems.

Installation

Add this line to your application's Gemfile:

gem 'rack-highlighter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-highlighter

Usage

Adding highlighting to a Rails application

require 'rack/highlighter'
require 'pygments'

class Application < Rails::Application
  config.middleware.use Rack::Highlighter, :pygments
end

Adding highlighting to a Sinatra application

require 'sinatra'
require 'rack/highlighter'
require 'pygments'

use Rack::Highlighter, :pygments

get('/') do
  '<pre><code class="ruby">puts "Hello world!"</code></pre>'
end

Adding highlighting to a Rackup application

require 'rack'
require 'rack/highlighter'
require 'pygments'

use Rack::Highlighter, :pygments

run lambda { |env|
  [200, {'Content-Type' => 'text/html'}, ['<pre><code class="ruby">puts "Hello world!"</code></pre>']]
}

Highlighters

You can choose which highlighter to use. Just require the right gem, and specify it when invoking the middleware.

CodeRay

require 'rack/highlighter'
require 'coderay'

use Rack::Highlighter, :coderay

Pygments

require 'rack/highlighter'
require 'pygments'

use Rack::Highlighter, :pygments

Rouge

require 'rack/highlighter'
require 'rouge'

use Rack::Highlighter, :rouge

Ultraviolet

require 'rack/highlighter'
require 'uv'

use Rack::Highlighter, :ultraviolet

Configuration

Default

With the default options, Rack::Highlighter would recognize code blocks like the following:

<pre>
  <code class="ruby">puts "Hello world!"</code>
</pre>

Elements

If your code is wrapped in other tags, for example:

<pre class="ruby">puts "Hello world!"</pre>

You can specify them in the elements option:

use Rack::Highlighter, :pygments, { elements: ['pre'] }

Attribute

If your block doesn't use the class attribute to declare the language of the code, for example:

<pre>
  <code data-lang="ruby">puts "Hello world!"</code>
</pre>

You can specify it in the attribute option:

use Rack::Highlighter, :pygments, { attribute: 'data-lang' }

Pattern

If your block uses a certain pattern to declare the language of the code, for example:

<pre>
  <code class="language-ruby">puts "Hello world!"</code>
</pre>

You can specify a regular expression to match it in the pattern option:

use Rack::Highlighter, :pygments, { pattern: /language-(?<lang>\w+)/ }

The regular expression must have a capture group named lang for this to work.

Additional parameters

You can pass additional parameters to the highlighter via the misc option:

use Rack::Highlighter, :pygments, { misc: {linenos: 'inline'} }

Meta

Contributors

License

Copyright (c) 2012 Daniel Perez Alvarez (unindented.org). This is free software, and may be redistributed under the terms specified in the LICENSE file.