Ruby-chan

CodeRay is a fast syntax highlighting library written completely in Ruby.

It features fast HTML generation and a really advanced Ruby scanner.

Features

input languages

  • C
  • C++*
  • CSS
  • Delphi
  • diff
  • Groovy* (beta)
  • HTML
  • Java
  • JavaScript
  • JSON
  • Nitro-XHTML
  • PHP*
  • Python*
  • RHTML (ERB+HTML)
  • Ruby
  • Scheme (beta)
  • SQL*
  • XML
  • YAML

output formats

  • HTML (span/div/page)
  • JSON*
  • XML
  • YAML
  • Terminal* (beta)

tools

  • comment filter*
  • LoC counter* (beta)
  • token statistiks

* = new in 0.9.1

basic features

  • fast (some 100 KB/s)!
  • safe (doesn't choke on bad code)
  • plugin system for
    • scanners
    • encoders
    • styles
  • extensive test suite
  • reusable representation of tokens
  • reusable scanners and encoders
  • token streaming (to save memory)
  • scanner inheritance and inclusion

More features are planned for 1.0 and 1.1.

Usage & Live Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'rubygems'
require 'coderay'

# output as HTML div (using inline CSS styles)
puts CodeRay.scan('puts "Hello, world!"', :ruby).div

# ...with line numbers
puts CodeRay.scan("5.times do\n  puts 'Hello, world!'\nend", :ruby).div(:line_numbers => :table)

# output as standalone HTML page (using CSS classes)
puts CodeRay.scan('puts "Hello, world!"', :ruby).page

# keep scanned tokens for later use
tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)

# produce a token statistic
puts tokens.statistic

# count the tokens
tokens.count  # => 26

# produce a HTML div, but with CSS classes
puts tokens.div(:css => :class)

# highlight a file (HTML div); guess the file type base on the extension
# puts CodeRay.highlight_file('foo.erb.html')

# get a new scanner for Python
python_scanner = CodeRay.scanner :python

# get a new encoder for terminal
terminal_encoder = CodeRay.encoder :term

# scanning into tokens
tokens = python_scanner.tokenize 'import this;  # The Zen of Python'

# format the tokens
puts terminal_encoder.encode_tokens(tokens)

# re-using scanner and encoder
ruby_highlighter = CodeRay::Duo[:ruby, :div]
puts ruby_highlighter.encode('puts "Hello, world!"')

More documentation is also available.