Class: CodeRay::Duo

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/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 collapse

Instance Method Summary collapse

Constructor Details

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

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

simple:

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

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).



37
38
39
40
41
42
43
44
45
46
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 37

def initialize lang = nil, format = nil, options = {}
  if format.nil? && lang.is_a?(Hash) && lang.size == 1
    @lang = lang.keys.first
    @format = lang[@lang]
  else
    @lang = lang
    @format = format
  end
  @options = options
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 19

def format
  @format
end

#langObject

Returns the value of attribute lang.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 19

def lang
  @lang
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 19

def options
  @options
end

Instance Method Details

#encode(code, options = {}) ⇒ Object Also known as: highlight, call

Tokenize and highlight the code using scanner and encoder.



64
65
66
67
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 64

def encode code, options = {}
  options = @options.merge options
  encoder.encode(code, @lang, options)
end

#encoderObject

The encoder of the duo. Only created once.



59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 59

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

#scannerObject

The scanner of the duo. Only created once.



54
55
56
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/duo.rb', line 54

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