Coder Social home page Coder Social logo

pygments.rb's Introduction

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.

pygments.rb's People

Contributors

tmm1 avatar josh avatar vmg avatar abevoelker avatar jaxzin avatar rodjek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.