XUtils

Rouge

A pure Ruby code highlighter that is compatible with Pygments.


Rouge

Build Status Gem Version YARD Docs

Rouge is a pure Ruby syntax highlighter. It can highlight over 200 different languages, and output HTML or ANSI 256-color text. Its HTML output is compatible with stylesheets designed for Pygments.

Library

Here’s a quick example of using Rouge as you would any other regular Ruby library:

require 'rouge'

# make some nice lexed html
source = File.read('/etc/bashrc')
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::Shell.new
formatter.format(lexer.lex(source))

# Get some CSS
Rouge::Themes::Base16.mode(:light).render(scope: '.highlight')
# Or use Theme#find with string input
Rouge::Theme.find('base16.light').render(scope: '.highlight')

Jekyll

Rouge is Jekyll’s default syntax highlighter. Out of the box, Rouge will be used to highlight text wrapped in the {% highlight %} template tags. The {% highlight %} tag provides minimal options: you can specify the language to use and whether to enable line numbers or not. More information is available in the Jekyll docs.

Command Line

Rouge ships with a rougify command which allows you to easily highlight files in your terminal:

rougify foo.rb
rougify style monokai.sublime > syntax.css

Configuration

Writing your own HTML formatter

If the above formatters are not sufficient, and you wish to customize the layout of the HTML document, we suggest writing your own HTML formatter. This can be accomplished by subclassing Rouge::Formatters::HTML and overriding specific methods:

class MyFormatter < Rouge::Formatters::HTML

  # this is the main entry method. override this to customize the behavior of
  # the HTML blob as a whole. it should receive an Enumerable of (token, value)
  # pairs and yield out fragments of the resulting html string. see the docs
  # for the methods available on Token.
  def stream(tokens, &block)
    yield "<div class='my-outer-div'>"

    tokens.each do |token, value|
      # for every token in the output, we render a span
      yield span(token, value)
    end

    yield "</div>"
  end

  # or, if you need linewise processing, try:
  def stream(tokens, &block)
    token_lines(tokens).each do |line_tokens|
      yield "<div class='my-cool-line'>"
      line_tokens.each do |token, value|
        yield span(token, value)
      end
      yield "</div>"
    end
  end

  # Override this method to control how individual spans are rendered.
  # The value `safe_value` will already be HTML-escaped.
  def safe_span(token, safe_value)
    # in this case, "text" tokens don't get surrounded by a span
    if token == Token::Tokens::Text
      safe_value
    else
      "<span class=\"#{token.shortname}\">#{safe_value}</span>"
    end
  end
end

Lexer Options

  • debug: false will print a trace of the lex on stdout.

  • parent: '' allows you to specify which language the template is inside.

CSS Options

  • scope: '.highlight' sets the CSS selector to which styles are applied, e.g.:
  Rouge::Themes::MonokaiSublime.render(scope: 'code')

Documentation

Rouge’s documentation is available at rouge-ruby.github.io/docs/.

Requirements

Ruby

Rouge is compatible with all versions of Ruby from 2.0.0 onwards. It has no external dependencies.

Integrations

Bug Reports

Rouge uses GitHub’s Issues to report bugs. You can choose from one of our templates or create a custom issue. Issues that have not been active for a year are automatically closed by GitHub’s Probot.

Developing Lexers

NOTE: Please don’t submit lexers that are copy-pasted from other files. These submission will be rejected and we don’t want you to waste your time.

We want to make it as easy as we can for anyone to contribute a lexer to Rouge. To help get you started, we have a shiny new guide on lexer development in the documentation. The best place is to start there.

If you get stuck and need help, submit a pull request with what you have and make it clear in your submission that the lexer isn’t finished yet. We’ll do our best to answer any questions you have and sometimes the best way to do that is with actual code.

Testing Rouge

Once you’ve cloned the repository from GitHub, you can test the core of Rouge simply by running rake (no bundle exec required). You can also run a single test file by setting the TEST environment variable to the path of the desired test. For example, to test just the ruby lexer (located at path spec/lexers/ruby_spec.rb) simply run the following:

TEST=spec/lexers/ruby_spec.rb rake

To test a lexer visually, run rackup from the top-level working directory and you should have a web server running and ready to go. Visit http://localhost:9292 to see the full list of Rouge’s lexers.

Once you’ve selected a particular lexer, you can add ?debug=1 to your URL string to see a lot of helpful debugging info printed on stdout.

Versioning

Rouge uses Semantic Versioning 2.0.0.

Maintainers

Rouge is largely the result of the hard work of unpaid volunteers. It was originally developed by Jeanine Adkisson (@jneen) and is currently maintained by Jeanine Adkisson, Drew Blessing (@dblessing), Goro Fuji (@gfx) and Tan Le (@tancnle).


Articles

  • coming soon...