Class: CodeRay::Encoders::Encoder

Inherits:
Object
  • Object
show all
Extended by:
Plugin
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb

Overview

Encoder

The Encoder base class. Together with Scanner and Tokens, it forms the highlighting triad.

Encoder instances take a Tokens object and do something with it.

The most common Encoder is surely the HTML encoder (CodeRay::Encoders::HTML). It highlights the code in a colorful html page. If you want the highlighted code in a div or a span instead, use its subclasses Div and Span.

Direct Known Subclasses

Count, Debug, Filter, HTML, JSON, Null, Statistic, Terminal, Text, XML, YAML

Constant Summary collapse

DEFAULT_OPTIONS =

Subclasses are to store their default options in this constant.

{ }

Instance Attribute Summary collapse

Attributes included from Plugin

#plugin_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugin

aliases, plugin_host, register_for, title

Constructor Details

#initialize(options = {}) ⇒ Encoder

Creates a new Encoder. options is saved and used for all encode operations, as long as you don’t overwrite it there by passing additional options.

Encoder objects provide three encode methods:

  • encode simply takes a code string and a lang

  • encode_tokens expects a tokens object instead

Each method has an optional options parameter. These are added to the options you passed at creation.



55
56
57
58
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 55

def initialize options = {}
  @options = self.class::DEFAULT_OPTIONS.merge options
  @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = false
end

Instance Attribute Details

#optionsObject

The options you gave the Encoder at creating.



43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 43

def options
  @options
end

#scannerObject

The options you gave the Encoder at creating.



43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 43

def scanner
  @scanner
end

Class Method Details

.const_missing(sym) ⇒ Object

If FILE_EXTENSION isn’t defined, this method returns the downcase class name instead.



24
25
26
27
28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 24

def const_missing sym
  if sym == :FILE_EXTENSION
    (defined?(@plugin_id) && @plugin_id || name[/\w+$/].downcase).to_s
  else
    super
  end
end

.file_extensionObject

The default file extension for output file of this encoder class.



33
34
35
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 33

def file_extension
  self::FILE_EXTENSION
end

Instance Method Details

#<<(token) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 87

def << token
  unless @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
    warn 'Using old Tokens#<< interface.'
    @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = true
  end
  self.token(*token)
end

#begin_group(kind) ⇒ Object

Starts a token group with the given kind.



123
124
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 123

def begin_group kind
end

#begin_line(kind) ⇒ Object

Starts a new line token group with the given kind.



131
132
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 131

def begin_line kind
end

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

Encode the given code using the Scanner for lang.



70
71
72
73
74
75
76
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 70

def encode code, lang, options = {}
  options = @options.merge options
  @scanner = Scanners[lang].new code, CodeRay.get_scanner_options(options).update(:tokens => self)
  setup options
  @scanner.tokenize
  finish options
end

#encode_tokens(tokens, options = {}) ⇒ Object

Encode a Tokens object.



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

def encode_tokens tokens, options = {}
  options = @options.merge options
  @scanner = tokens.scanner if tokens.respond_to? :scanner
  setup options
  compile tokens, options
  finish options
end

#end_group(kind) ⇒ Object

Ends a token group with the given kind.



127
128
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 127

def end_group kind
end

#end_line(kind) ⇒ Object

Ends a new line token group with the given kind.



135
136
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 135

def end_line kind
end

#file_extensionObject

The default file extension for this encoder.



83
84
85
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 83

def file_extension
  self.class.file_extension
end

#text_token(text, kind) ⇒ Object

Called for each text token ([text, kind]), where text is a String.



118
119
120
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 118

def text_token text, kind
  @out << text
end

#token(content, kind) ⇒ Object

Called with content and kind of the currently scanned token. For simple scanners, it’s enougth to implement this method.

By default, it calls text_token, begin_group, end_group, begin_line, or end_line, depending on the content.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/encoder.rb', line 100

def token content, kind
  case content
  when String
    text_token content, kind
  when :begin_group
    begin_group kind
  when :end_group
    end_group kind
  when :begin_line
    begin_line kind
  when :end_line
    end_line kind
  else
    raise ArgumentError, 'Unknown token content type: %p, kind = %p' % [content, kind]
  end
end