Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

💎 Ruby wrapper for Pygments syntax highlighter

License

NotificationsYou must be signed in to change notification settings

pygments/pygments.rb

Repository files navigation

Latest ReleaseBuild Status

Introduction

pygments.rb is a Ruby wrapper forPygments syntax highlighter.

pygments.rb works by talking over a simple pipe to a long-lived Python child process.This library replacesgithub/albino, as well as an older 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 >= 3.8

  • Ruby >= 2.7

Installation

Add this line to your application’s Gemfile:

gem'pygments.rb'

And then execute:

$ bundle install

Or install pygments.rb gem globally:

$ gem install pygments.rb

Usage

Require pygments.rb module:

require'pygments'

Highlight a file:

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

Optionally, pass encoding and other lexer/formatter options via an:options hash:

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

pygments.rb uses HTML formatter by default.To use a different formatter, specify it via:formatter parameter:

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

To generate CSS for HTML formatted code, use thePygments.css method:

Pygments.cssPygments.css('.highlight')

To use a specific pygments style, pass the:style option to thePygments.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.lexersPygments.formattersPygments.styles

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

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

If you’d like logging, set the environmental variableMENTOS_LOG to a file path for your logfile.

You can apply a timeout to pygments.rb calls by specifying number of seconds inMENTOS_TIMEOUT environmental variable or by passing the:timeout argument (takes precedence overMENTOS_TIMEOUT):

Pygments.highlight('code',timeout:4)

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)

Development

After checking out the repo, runbundle install to install dependencies.Then, runbundle exec rake test to run the tests.

Copyright

Copyright © Ted Nyman, Aman Gupta, Marat Radchenko, 2012-2021.Free use of this software is granted under the terms of the MIT License.

For the full text of the license, see theLICENSE file.

About

💎 Ruby wrapper for Pygments syntax highlighter

Topics

Resources

License

Stars

Watchers

Forks

Contributors34


[8]ページ先頭

©2009-2025 Movatter.jp