Module: Pygments

Defined in:
lib/rb-pygments.rb

Overview

A wrapper for the Pygments command-line interface.

Class Method Summary (collapse)

Class Method Details

+ (String) highlight(source, lexer, formatter, options = {})

Returns the source code, highlighted using the given lexer and formatter. For example, returns HTML markup surrounding the source code for the :html formatter, or LaTeX markup for the :latex formatter.

Parameters:

  • source (String)

    The source code to be highlighted

  • lexer (#to_s)

    The name of the lexer (that is, language) to use, e.g. :ruby or :python. The full list of lexers can be found by running pygmentize -L lexers, or online.

  • formatter (#to_s)

    The name of the formatter to use, e.g. :html or :latex. The full list of formatters can be found by running pygmentize -L formatters, or online.

  • options ({String => String}) (defaults to: {})

    Options passed to the formatter and lexer. These are specific to the formatter and lexer being used; available options can be found by running pygmentize -H formatter #{formatter} or pygmentize -H lexer #{lexer}.

Returns:

  • (String)


50
51
52
# File 'lib/rb-pygments.rb', line 50

def highlight(source, lexer, formatter, options = {})
  execute(["-l", lexer, "-f", formatter] + convert_options(options), source)
end

+ (Array<String>) languages

Returns a list of all langauges supported by Pygments. This list contains the short names of the languages as documented on the Pygments site.

Returns:

  • (Array<String>)


11
12
13
# File 'lib/rb-pygments.rb', line 11

def languages
  @languages ||= languages!
end

+ (String) style(style, formatter, options = {})

Returns the style definitions for a given style and formatter. For example, returns the CSS definitions for the HTML formatter and the LaTeX style definitions for the LaTeX formatter.

Parameters:

  • style (#to_s)

    The name of the color theme to use, e.g. :default or :colorful. The full list of color themes can be found by running pygmentize -L styles.

  • formatter (#to_s)

    The name of the formatter to use, e.g. :html or :latex. The full list of formatters can be found by running pygmentize -L formatters, or online. At time of writing only :html and :latex support styles.

  • options ({String => String}) (defaults to: {})

    Options passed to the formatter. These are formatter-specific; available options for a formatter can be found by running pygmentize -H formatter #{formatter}.

Returns:

  • (String)


30
31
32
# File 'lib/rb-pygments.rb', line 30

def style(style, formatter, options = {})
  execute(["-S", style, "-f", formatter] + convert_options(options))
end