# Rack::Highlighter [![Build Status](https://secure.travis-ci.org/unindented/rack-highlighter.png)](http://travis-ci.org/unindented/rack-highlighter) Rack Middleware that provides syntax highlighting of code blocks, using the [CodeRay](http://rubygems.org/gems/coderay), [Pygments](http://rubygems.org/gems/pygments.rb), [Rouge](https://rubygems.org/gems/rouge) or [Ultraviolet](http://rubygems.org/gems/ultraviolet) gems. ## Installation Add this line to your application's `Gemfile`: ```ruby gem 'rack-highlighter' ``` And then execute: ```sh $ bundle ``` Or install it yourself as: ```sh $ gem install rack-highlighter ``` ## Usage ### Adding highlighting to a Rails application ```ruby require 'rack/highlighter' require 'pygments' class Application < Rails::Application config.middleware.use Rack::Highlighter, :pygments end ``` ### Adding highlighting to a Sinatra application ```ruby require 'sinatra' require 'rack/highlighter' require 'pygments' use Rack::Highlighter, :pygments get('/') do '
puts "Hello world!"
'
end
```
### Adding highlighting to a Rackup application
```ruby
require 'rack'
require 'rack/highlighter'
require 'pygments'
use Rack::Highlighter, :pygments
run lambda { |env|
[200, {'Content-Type' => 'text/html'}, ['puts "Hello world!"
']]
}
```
## Highlighters
You can choose which highlighter to use. Just require the right gem, and specify it when invoking the middleware.
### CodeRay
```ruby
require 'rack/highlighter'
require 'coderay'
use Rack::Highlighter, :coderay
```
### Pygments
```ruby
require 'rack/highlighter'
require 'pygments'
use Rack::Highlighter, :pygments
```
### Rouge
```ruby
require 'rack/highlighter'
require 'rouge'
use Rack::Highlighter, :rouge
```
### Ultraviolet
```ruby
require 'rack/highlighter'
require 'uv'
use Rack::Highlighter, :ultraviolet
```
## Configuration
### Default
With the default options, `Rack::Highlighter` would recognize code blocks like the following:
```html
puts "Hello world!"
```
### Elements
If your code is wrapped in other tags, for example:
```html
puts "Hello world!"``` You can specify them in the `elements` option: ```ruby 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: ```html
puts "Hello world!"
```
You can specify it in the `attribute` option:
```ruby
use Rack::Highlighter, :pygments, { attribute: 'data-lang' }
```
### Pattern
If your block uses a certain pattern to declare the language of the code, for example:
```html
puts "Hello world!"
```
You can specify a regular expression to match it in the `pattern` option:
```ruby
use Rack::Highlighter, :pygments, { pattern: /language-(?