GithubHelp home page GithubHelp logo

pombredanne / pygments.rb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pygments/pygments.rb

0.0 1.0 0.0 6.01 MB

pygments syntax highlighting in ruby

License: MIT License

Ruby 19.20% Python 17.02% C 63.78%

pygments.rb's Introduction

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.

system requirements

  • Python 2.5, Python 2.6, or Python 2.7. You can always use Python 2.x from a virtualenv if your default Python install is 3.x.

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

To use a specific pygments style, pass the :style option to the #css method:

Pygments.css(:style => "monokai")

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

If you'd like logging, set the environmental variable MENTOS_LOG to a file path for your logfile.

By default pygments.rb will timeout calls to pygments that take over 8 seconds. You can change this by setting the environmental variable MENTOS_TIMEOUT to a different positive integer value.

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)

license

The MIT License (MIT)

Copyright (c) Ted Nyman and Aman Gupta, 2012-2013

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pygments.rb's People

Contributors

abevoelker avatar adrienlemaire avatar alindeman avatar bootstraponline avatar haileys avatar hoelzro avatar jaxzin avatar josh avatar kayloos avatar ktdreyer avatar nitoyon avatar pombredanne avatar pwnall avatar rodjek avatar tmm1 avatar tnm avatar vmg avatar

Watchers

 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.