Module: CodeRay

Defined in:
vendor/plugins/coderay-0.9.2/lib/coderay.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/style.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/tokens.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanner.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/c.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/styles/_map.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/xml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/css.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/xml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/div.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/for_redcloth.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/php.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/sql.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/cpp.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/diff.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/html.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/html.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/text.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/yaml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/styles/cycnus.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/styles/murphy.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/span.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/json.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/yaml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/ruby.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/page.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/term.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/java.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/null.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/_map.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/_map.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/token_classes.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/debug.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/helpers/plugin.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/debug.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/rhtml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/count.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/filter.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/groovy.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/scheme.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/delphi.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/python.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/helpers/file_type.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/html/css.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/helpers/word_list.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/statistic.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/plaintext.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/nitro_xhtml.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/html/output.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/java_script.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/lines_of_code.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/ruby/patterns.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/comment_filter.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/html/numerization.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/encoders/token_class_filter.rb,
vendor/plugins/coderay-0.9.2/lib/coderay/scanners/java/builtin_types.rb

Overview

encoding: utf-8

Defined Under Namespace

Modules: Encoders, FileType, ForRedCloth, Plugin, PluginHost, Scanners, Streamable, Styles Classes: CaseIgnoringWordList, Duo, NotStreamableError, TokenStream, Tokens, WordList

Constant Summary

VERSION =

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)

'0.9.2'

Class Method Summary

Class Method Details

+ (Object) encode(code, lang, format, options = {})

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



209
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 209

def encode code, lang, format, options = {}

+ (Object) encode_file(filename, format, options = {})

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


244
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 244

def encode_file filename, format, options = {}

+ (Object) encode_stream(code, lang, format, options = {})

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



198
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 198

def encode_stream code, lang, format, options = {}

+ (Object) encode_tokens(tokens, format, options = {})

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)


232
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 232

def encode_tokens tokens, format, options = {}

+ (Object) encoder(format, options = {})

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.


273
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 273

def encoder format, options = {}

+ (Object) get_scanner_options(options)

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.



291
292
293
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 291

def get_scanner_options options
  options.fetch :scanner_options, {}
end

+ (Object) highlight(code, lang, options = { :css => :class }, format = :div)

Highlight a string into a HTML <div>.

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

See encode.



219
220
221
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 219

def highlight code, lang, options = { :css => :class }, format = :div
  encode code, lang, format, options
end

+ (Object) highlight_file(filename, options = { :css => :class }, format = :div)

Highlight a file into a HTML <div>.

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

See encode.



255
256
257
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 255

def highlight_file filename, options = { :css => :class }, format = :div
  encode_file filename, format, options
end

+ (Object) require_plugin(path)

Convenience method for plugin loading. The syntax used is:

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

Returns the loaded plugin.



341
342
343
344
345
346
347
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/helpers/plugin.rb', line 341

def self.require_plugin path
  host_id, plugin_id = path.split '/', 2
  host = PluginHost.host_by_id(host_id)
  raise PluginHost::HostNotFound,
    "No host for #{host_id.inspect} found." unless host
  host.load plugin_id
end

+ (Object) scan(code, lang, options = {}, &block)

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.



156
157
158
159
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 156

def scan code, lang, options = {}, &block
  scanner = Scanners[lang].new code, options, &block
  scanner.tokenize
end

+ (Object) scan_file(filename, lang = :auto, options = {}, &block)

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


172
173
174
175
176
177
178
179
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 172

def scan_file filename, lang = :auto, options = {}, &block
  file = IO.read filename
  if lang == :auto
    require 'coderay/helpers/file_type'
    lang = FileType.fetch filename, :plaintext, true
  end
  scan file, lang, options = {}, &block
end

+ (Object) scan_stream(code, lang, options = {}, &block)

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

Calls scan.

See CodeRay.scan.



186
187
188
189
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 186

def scan_stream code, lang, options = {}, &block
  options[:stream] = true
  scan code, lang, options, &block
end

+ (Object) scanner(lang, options = {})

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

See Scanner.new.



281
# File 'vendor/plugins/coderay-0.9.2/lib/coderay.rb', line 281

def scanner lang, options = {}