Class: CodeRay::Duo

Inherits:
Object
  • Object
show all
Defined in:
vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb

Overview

Duo

A Duo is a convenient way to use CodeRay. You just create a Duo, giving it a lang (language of the input code) and a format (desired output format), and call Duo#highlight with the code.

Duo makes it easy to re-use both scanner and encoder for a repetitive task. It also provides a very easy interface syntax:

  require 'coderay'
  CodeRay::Duo[:python, :div].highlight 'import this'

Until you want to do uncommon things with CodeRay, I recommend to use this method, since it takes care of everything.

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Duo) initialize(lang = nil, format = nil, options = {})

Create a new Duo, holding a lang and a format to highlight code.

simple:

  CodeRay::Duo[:ruby, :page].highlight 'bla 42'

streaming:

  CodeRay::Duo[:ruby, :page].highlight 'bar 23', :stream => true

with options:

  CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'

alternative syntax without options:

  CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'

alternative syntax with options:

  CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'

The options are forwarded to scanner and encoder (see CodeRay.get_scanner_options).



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

def initialize lang = nil, format = nil, options = {}

Instance Attribute Details

- (Object) format

Returns the value of attribute format



19
20
21
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb', line 19

def format
  @format
end

- (Object) lang

Returns the value of attribute lang



19
20
21
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb', line 19

def lang
  @lang
end

- (Object) options

Returns the value of attribute options



19
20
21
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb', line 19

def options
  @options
end

Instance Method Details

- (Object) encode(code, options = { :stream => false }) Also known as: highlight

Tokenize and highlight the code using scanner and encoder.

If the :stream option is set, the Duo will go into streaming mode, saving memory for the cost of time.



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

def encode code, options = { :stream => false }

- (Object) encoder

The encoder of the duo. Only created once.



62
63
64
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb', line 62

def encoder
  @encoder ||= CodeRay.encoder @format, @options
end

- (Object) scanner

The scanner of the duo. Only created once.



57
58
59
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/duo.rb', line 57

def scanner
  @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
end