Module CodeRay
In: lib/coderay/duo.rb
lib/coderay/encoder.rb
lib/coderay/encoders/_map.rb
lib/coderay/encoders/count.rb
lib/coderay/encoders/debug.rb
lib/coderay/encoders/div.rb
lib/coderay/encoders/html/css.rb
lib/coderay/encoders/html/numerization.rb
lib/coderay/encoders/html/output.rb
lib/coderay/encoders/html.rb
lib/coderay/encoders/null.rb
lib/coderay/encoders/page.rb
lib/coderay/encoders/span.rb
lib/coderay/encoders/statistic.rb
lib/coderay/encoders/text.rb
lib/coderay/encoders/tokens.rb
lib/coderay/encoders/xml.rb
lib/coderay/encoders/yaml.rb
lib/coderay/for_redcloth.rb
lib/coderay/helpers/file_type.rb
lib/coderay/helpers/plugin.rb
lib/coderay/helpers/word_list.rb
lib/coderay/scanner.rb
lib/coderay/scanners/_map.rb
lib/coderay/scanners/c.rb
lib/coderay/scanners/css.rb
lib/coderay/scanners/debug.rb
lib/coderay/scanners/delphi.rb
lib/coderay/scanners/diff.rb
lib/coderay/scanners/html.rb
lib/coderay/scanners/java/builtin_types.rb
lib/coderay/scanners/java.rb
lib/coderay/scanners/java_script.rb
lib/coderay/scanners/json.rb
lib/coderay/scanners/nitro_xhtml.rb
lib/coderay/scanners/plaintext.rb
lib/coderay/scanners/rhtml.rb
lib/coderay/scanners/ruby/patterns.rb
lib/coderay/scanners/ruby.rb
lib/coderay/scanners/scheme.rb
lib/coderay/scanners/sql.Keith.rb
lib/coderay/scanners/sql.rb
lib/coderay/scanners/xml.rb
lib/coderay/scanners/yaml.rb
lib/coderay/style.rb
lib/coderay/styles/_map.rb
lib/coderay/styles/cycnus.rb
lib/coderay/styles/murphy.rb
lib/coderay/token_classes.rb
lib/coderay/tokens.rb
lib/coderay.rb

CodeRay Library

CodeRay is a Ruby library for syntax highlighting.

I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete, fast and efficient.

See README.

It consists mainly of

Here‘s a fancy graphic to light up this gray docu:

Documentation

See CodeRay, Encoders, Scanners, Tokens.

Usage

Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with -rubygems option if required.

Highlight Ruby code in a string as html

  require 'coderay'
  print CodeRay.scan('puts "Hello, world!"', :ruby).html

  # prints something like this:
  puts <span class="s">&quot;Hello, world!&quot;</span>

Highlight C code from a file in a html div

  require 'coderay'
  print CodeRay.scan(File.read('ruby.h'), :c).div
  print CodeRay.scan_file('ruby.h').html.div

You can include this div in your page. The used CSS styles can be printed with

  % coderay_stylesheet

Highlight without typing too much

If you are one of the hasty (or lazy, or extremely curious) people, just run this file:

  % ruby -rubygems /path/to/coderay/coderay.rb > example.html

and look at the file it created in your browser.

CodeRay Module

The CodeRay module provides convenience methods for the engine.

  • The lang and format arguments select Scanner and Encoder to use. These are simply lower-case symbols, like :python or :html.
  • All methods take an optional hash as last parameter, options, that is send to the Encoder / Scanner.
  • Input and language are always sorted in this order: code, lang. (This is in alphabetical order, if you need a mnemonic ;)

You should be able to highlight everything you want just using these methods; so there is no need to dive into CodeRay‘s deep class hierarchy.

The examples in the demo directory demonstrate common cases using this interface.

Basic Access Ways

Read this to get a general view what CodeRay provides.

Scanning

 Scanning means analysing an input string, splitting it up into Tokens.
 Each Token knows about what type it is: string, comment, class name, etc.

 Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is
 handled by CodeRay::Scanners::Ruby.
CodeRay.scan:Scan a string in a given language into Tokens. This is the most common method to use.
CodeRay.scan_file:Scan a file and guess the language using FileType.

The Tokens object you get from these methods can encode itself; see Tokens.

Encoding

Encoding means compiling Tokens into an output. This can be colored HTML or LaTeX, a textual statistic or just the number of non-whitespace tokens.

Each Encoder provides output in a specific format, so you select Encoders via formats like :html or :statistic.

CodeRay.encode:Scan and encode a string in a given language.
CodeRay.encode_tokens:Encode the given tokens.
CodeRay.encode_file:Scan a file, guess the language using FileType and encode it.

Streaming

Streaming saves RAM by running Scanner and Encoder in some sort of pipe mode; see TokenStream.

CodeRay.scan_stream:Scan in stream mode.

All-in-One Encoding

CodeRay.encode:Highlight a string with a given input and output format.

Instanciating

You can use an Encoder instance to highlight multiple inputs. This way, the setup for this Encoder must only be done once.

CodeRay.encoder:Create an Encoder instance with format and options.
CodeRay.scanner:Create an Scanner instance for lang, with ’’ as default code.

To make use of CodeRay.scanner, use CodeRay::Scanner::code=.

The scanning methods provide more flexibility; we recommend to use these.

Reusing Scanners and Encoders

If you want to re-use scanners and encoders (because that is faster), see CodeRay::Duo for the most convenient (and recommended) interface.

Methods

Classes and Modules

Module CodeRay::Encoders
Module CodeRay::FileType
Module CodeRay::ForRedCloth
Module CodeRay::Plugin
Module CodeRay::PluginHost
Module CodeRay::Scanners
Module CodeRay::Streamable
Module CodeRay::Styles
Class CodeRay::CaseIgnoringWordList
Class CodeRay::Duo
Class CodeRay::NotStreamableError
Class CodeRay::TokenStream
Class CodeRay::Tokens
Class CodeRay::WordList

Constants

VERSION = '0.8'   Version: Major.Minor.Teeny[.Revision] Major: 0 for pre-stable, 1 for stable Minor: feature milestone Teeny: development state, 0 for pre-release Revision: Subversion Revision number (generated on rake gem:make)

Public Class methods

Encode a string.

This scans code with the the Scanner for lang and then encodes it with the Encoder for format. options will be passed to the Encoder.

See CodeRay::Encoder.encode

[Source]

     # File lib/coderay.rb, line 206
206:     def encode code, lang, format, options = {}
207:       encoder(format, options).encode code, lang, options
208:     end

Encodes filename (a path to a code file) with the Scanner for lang.

See CodeRay.scan_file. Notice that the second argument is the output format, not the input language.

Example:

 require 'coderay'
 page = CodeRay.encode_file 'some_c_code.c', :html

[Source]

     # File lib/coderay.rb, line 241
241:     def encode_file filename, format, options = {}
242:       tokens = scan_file filename, :auto, get_scanner_options(options)
243:       encode_tokens tokens, format, options
244:     end

Encode a string in Streaming mode.

This starts scanning code with the the Scanner for lang while encodes the output with the Encoder for format. options will be passed to the Encoder.

See CodeRay::Encoder.encode_stream

[Source]

     # File lib/coderay.rb, line 195
195:     def encode_stream code, lang, format, options = {}
196:       encoder(format, options).encode_stream code, lang, options
197:     end

Encode pre-scanned Tokens. Use this together with CodeRay.scan:

 require 'coderay'

 # Highlight a short Ruby code example in a HTML span
 tokens = CodeRay.scan '1 + 2', :ruby
 puts CodeRay.encode_tokens(tokens, :span)

[Source]

     # File lib/coderay.rb, line 229
229:     def encode_tokens tokens, format, options = {}
230:       encoder(format, options).encode_tokens tokens, options
231:     end

Finds the Encoder class for format and creates an instance, passing options to it.

Example:

 require 'coderay'

 stats = CodeRay.encoder(:statistic)
 stats.encode("puts 17 + 4\n", :ruby)

 puts '%d out of %d tokens have the kind :integer.' % [
   stats.type_stats[:integer].count,
   stats.real_token_count
 ]
 #-> 2 out of 4 tokens have the kind :integer.

[Source]

     # File lib/coderay.rb, line 270
270:     def encoder format, options = {}
271:       Encoders[format].new options
272:     end

Extract the options for the scanner from the options hash.

Returns an empty Hash if :scanner_options is not set.

This is used if a method like CodeRay.encode has to provide options for Encoder and scanner.

[Source]

     # File lib/coderay.rb, line 288
288:     def get_scanner_options options
289:       options.fetch :scanner_options, {}
290:     end

Highlight a string into a HTML <div>.

CSS styles use classes, so you have to include a stylesheet in your output.

See encode.

[Source]

     # File lib/coderay.rb, line 216
216:     def highlight code, lang, options = { :css => :class }, format = :div
217:       encode code, lang, format, options
218:     end

Highlight a file into a HTML <div>.

CSS styles use classes, so you have to include a stylesheet in your output.

See encode.

[Source]

     # File lib/coderay.rb, line 252
252:     def highlight_file filename, options = { :css => :class }, format = :div
253:       encode_file filename, format, options
254:     end

Convenience method for plugin loading. The syntax used is:

 CodeRay.require_plugin '<Host ID>/<Plugin ID>'

Returns the loaded plugin.

[Source]

     # File lib/coderay/helpers/plugin.rb, line 323
323: def self.require_plugin path
324:   host_id, plugin_id = path.split '/', 2
325:   host = PluginHost.host_by_id(host_id)
326:   raise PluginHost::HostNotFound,
327:     "No host for #{host_id.inspect} found." unless host
328:   host.load plugin_id
329: end

Scans the given code (a String) with the Scanner for lang.

This is a simple way to use CodeRay. Example:

 require 'coderay'
 page = CodeRay.scan("puts 'Hello, world!'", :ruby).html

See also demo/demo_simple.

[Source]

     # File lib/coderay.rb, line 153
153:     def scan code, lang, options = {}, &block
154:       scanner = Scanners[lang].new code, options, &block
155:       scanner.tokenize
156:     end

Scans filename (a path to a code file) with the Scanner for lang.

If lang is :auto or omitted, the CodeRay::FileType module is used to determine it. If it cannot find out what type it is, it uses CodeRay::Scanners::Plaintext.

Calls CodeRay.scan.

Example:

 require 'coderay'
 page = CodeRay.scan_file('some_c_code.c').html

[Source]

     # File lib/coderay.rb, line 169
169:     def scan_file filename, lang = :auto, options = {}, &block
170:       file = IO.read filename
171:       if lang == :auto
172:         require 'coderay/helpers/file_type'
173:         lang = FileType.fetch filename, :plaintext, true
174:       end
175:       scan file, lang, options = {}, &block
176:     end

Scan the code (a string) with the scanner for lang.

Calls scan.

See CodeRay.scan.

[Source]

     # File lib/coderay.rb, line 183
183:     def scan_stream code, lang, options = {}, &block
184:       options[:stream] = true
185:       scan code, lang, options, &block
186:     end

Finds the Scanner class for lang and creates an instance, passing options to it.

See Scanner.new.

[Source]

     # File lib/coderay.rb, line 278
278:     def scanner lang, options = {}
279:       Scanners[lang].new '', options
280:     end

[Validate]