Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
pygments syntax highlighting in ruby
C Ruby Python
tag: v0.2.7

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
ext
lib
test
vendor
.gitignore
Gemfile
README.md
Rakefile
bench.rb
pygments.rb.gemspec

README.md

pygments.rb

A ruby wrapper for the python pygments syntax highlighter.

This library replaces github/albino. Instead of shelling out to pygmentize, it embeds the python interpreter inside ruby via FFI. This avoids the cost of setting up the python VM on every invocation and speeds up code highlighting from ruby by 10-15x.

usage

Pygments.highlight(File.read(__FILE__), :lexer => 'ruby')

Encoding and other lexer/formatter options can be passed in via an options hash:

Pygments.highlight('code', :options => {:encoding => 'utf-8'})

To use a formatter other than html, specify it explicitly:

Pygments.highlight('code', :formatter => 'bbcode')
Pygments.highlight('code', :formatter => 'terminal')

To generate CSS for html formatted code, use the css method:

Pygments.css
Pygments.css('.highlight')

To use a custom python installation (like in ArchLinux), tell RubyPython where python lives:

RubyPython.configure :python_exe => 'python2.7'

To use a custom pygments installation, specify the path to Pygments.start:

Pygments.start("/path/to/pygments")

benchmarks

$ ruby -rubygems bench.rb 50
                               user     system      total        real
albino                     0.050000   0.050000  12.830000 ( 13.180806)
pygments::c                1.000000   0.010000   1.010000 (  1.009348)
pygments::ffi + reload    11.350000   1.240000  12.590000 ( 12.692320)
pygments::ffi              1.130000   0.010000   1.140000 (  1.171589)

To run bench.rb, use a git checkout. The C extension is not included in gem releases.

Something went wrong with that request. Please try again.