Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rack-highlighter

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rack-highlighter

  • 0.2.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Rack::Highlighter Build Status Dependency Status

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.

FAQs

Package last updated on 23 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc