Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
pygments wrapper for ruby via embedded python
C Ruby Python
branch: lexer_for_cont…

This branch is 106 commits behind tmm1:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

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

README.md

pygments.rb

A Ruby wrapper for the Python pygments syntax highlighter.

pygments.rb works by talking over a simple pipe to a long-lived Python child process. This library replaces github/albino, as well as a version of pygments.rb that used an embedded Python interpreter.

Each Ruby process that runs has its own 'personal Python'; for example, 4 Unicorn workers will have one Python process each. If a Python process dies, a new one will be spawned on the next pygments.rb request.

usage

require 'pygments'
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'})

pygments.rb defaults to using an HTML formatter. To use a formatter other than html, specify it explicitly like so:

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')

Other Pygments high-level API methods are also available. These methods return arrays detailing all the available lexers, formatters, and styles.

Pygments.lexers
Pygments.formatters
Pygments.styles

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

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

benchmarks

$ ruby bench.rb 50
   Benchmarking....
   Size: 698 bytes
   Iterations: 50
                                                  user     system      total        real
   pygments popen                                0.010000   0.010000   0.020000 (  0.460370)
   pygments popen (process already started)      0.010000   0.000000   0.010000 (  0.272975)
   pygments popen (process already started 2)    0.000000   0.000000   0.000000 (  0.273589)

$ ruby bench.rb 10
   Benchmarking....
   Size: 15523 bytes
   Iterations: 10
                                                  user     system      total        real
   pygments popen                               0.000000   0.000000   0.000000 (  0.819419)
   pygments popen (process already started)     0.010000   0.000000   0.010000 (  0.676515)
   pygments popen (process already started 2)   0.000000   0.010000   0.010000 (  0.674189)
Something went wrong with that request. Please try again.