Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

unindented/rack-highlighter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rack Middleware for syntax highlighting.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages