Project

rembrandt

0.0
No commit activity in last 3 years
No release in over 3 years
A library for generating and storing syntax-highlighted code
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0
 Project Readme

Rembrandt

Rembrandt is a library for generating and caching syntax-highlighted code. Highlighting is powered by Pygments, either running locally or over a web API. Caching is handled either by Redis or the local file system.

Installation

Add this line to your application's Gemfile:

gem 'rembrandt'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rembrandt

Usage

Pygments Local vs API

The library will run which pygmentize to check if Pygments is installed locally. If it is, the library will use it. If it is not, the library will attempt to use the web-api at http://pygmentize.herokuapp.com

Storage with Redis or the Filesystem

If you'd like to use Redis as the datastore, there are two steps:

  • Add the redis gem to your bundle
  • Define an environment variable with the redis address

The environment variable REMBRANDT_REDIS_URL. On a unix system you can do this with the export instruction at your terminal:

export REMBRANDT_REDIS_URL='redis://my_user_name:my_password@example.com:9352/'

To verify the environment variable, open irb and try to access it:

$ irb
> 2.0.0-p353 :001 > ENV['REMBRANDT_REDIS_URL']
=> "redis://my_user_name:my_password@example.com:9352/" 

If Redis is not required and/or the environment variable is not defined, the library will use the local filesystem.

Basic Highlighting

> require 'rembrandt'
> highlighter = Rembrandt::Highlighter.new
> highlighter.highlight("class Sample\nend", "ruby")

Highlighting Source from a File

> require 'rembrandt'
> highlighter = Rembrandt::Highlighter.new
> highlighter.highlight_file("./my_input_file.rb", "ruby")

Usage with Octopress

Octopress has some really complex things going on in plugins/pygments.rb and plugins/highlight_code.rb and plugins/backtick_code_block.rb. Let's simplify.

  • Delete plugins/highlight_code.rb
  • Delete plugins/pygments_code.rb
  • Replace plugins/backtick_code_block.rb with the following:
module BacktickCodeBlock
  def render_code_block(input)
    formatter.format(strip_context(input))
  end

  def strip_context(block)

  end

  def highlighter
    @highlighter ||= Rembrandt::Highlighter.new
  end

  def formatter
    @formatter ||= Rembrandt::Formatters::Octopress.new
  end

Using a Formatter

> require 'rembrandt'
> highlighter = Rembrandt::Highlighter.new
> formatter = Rembrandt::Formatters::Table.new
> code = highlighter.highlight("class Sample\nend", "ruby")
> table = formatter.format(code, "ruby")

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

When Developing

It's expected that when developing that you have both Redis and Python/Pygmentize available on your machine. If you don't have Pygments installed, Rembrant's tests will try to use the HTTP highlight engine which is blocked by VCR.