Class CodeRay::Encoders::Encoder
In: lib/coderay/encoder.rb
Parent: Object

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.

Methods

Constants

DEFAULT_OPTIONS = { :stream => false }   Subclasses are to store their default options in this constant.

Attributes

options  [RW]  The options you gave the Encoder at creating.
token_stream  [R] 

Public Class methods

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

[Source]

    # File lib/coderay/encoder.rb, line 43
43:         def const_missing sym
44:           if sym == :FILE_EXTENSION
45:             plugin_id
46:           else
47:             super
48:           end
49:         end

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:

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

[Source]

    # File lib/coderay/encoder.rb, line 70
70:       def initialize options = {}
71:         @options = self.class::DEFAULT_OPTIONS.merge options
72:         raise "I am only the basic Encoder class. I can't encode "\
73:           "anything. :( Use my subclasses." if self.class == Encoder
74:       end

Returns if the Encoder can be used in streaming mode.

[Source]

    # File lib/coderay/encoder.rb, line 37
37:         def streamable?
38:           is_a? Streamable
39:         end

Public Instance methods

Encode the given code after tokenizing it using the Scanner for lang.

[Source]

    # File lib/coderay/encoder.rb, line 86
86:       def encode code, lang, options = {}
87:         options = @options.merge options
88:         scanner_options = CodeRay.get_scanner_options(options)
89:         tokens = CodeRay.scan code, lang, scanner_options
90:         encode_tokens tokens, options
91:       end

Encode the given code using the Scanner for lang in streaming mode.

[Source]

     # File lib/coderay/encoder.rb, line 99
 99:       def encode_stream code, lang, options = {}
100:         raise NotStreamableError, self unless kind_of? Streamable
101:         options = @options.merge options
102:         setup options
103:         scanner_options = CodeRay.get_scanner_options options
104:         @token_stream =
105:           CodeRay.scan_stream code, lang, scanner_options, &self
106:         finish options
107:       end

Encode a Tokens object.

[Source]

    # File lib/coderay/encoder.rb, line 77
77:       def encode_tokens tokens, options = {}
78:         options = @options.merge options
79:         setup options
80:         compile tokens, options
81:         finish options
82:       end

Return the default file extension for outputs of this encoder.

[Source]

     # File lib/coderay/encoder.rb, line 115
115:       def file_extension
116:         self.class::FILE_EXTENSION
117:       end
highlight(code, lang, options = {})

Alias for encode

Behave like a proc. The token method is converted to a proc.

[Source]

     # File lib/coderay/encoder.rb, line 110
110:       def to_proc
111:         method(:token).to_proc
112:       end

Protected Instance methods

[Source]

     # File lib/coderay/encoder.rb, line 149
149:       def block_token action, kind
150:         case action
151:         when :open
152:           open_token kind
153:         when :close
154:           close_token kind
155:         when :begin_line
156:           begin_line kind
157:         when :end_line
158:           end_line kind
159:         else
160:           raise 'unknown block action: %p' % action
161:         end
162:       end

[Source]

     # File lib/coderay/encoder.rb, line 179
179:         def compile tokens, options
180:           tokens.each(&self)
181:         end

[Source]

     # File lib/coderay/encoder.rb, line 175
175:         def compile tokens, options
176:           tokens.each { |text, kind| token text, kind }  # FIXME for Ruby 1.9?
177:         end

Called with merged options after encoding starts. The return value is the result of encoding, typically @out.

[Source]

     # File lib/coderay/encoder.rb, line 166
166:       def finish options
167:         @out
168:       end

Called with merged options before encoding starts. Sets @out to an empty string.

See the HTML Encoder for an example of option caching.

[Source]

     # File lib/coderay/encoder.rb, line 125
125:       def setup options
126:         @out = ''
127:       end

[Source]

     # File lib/coderay/encoder.rb, line 146
146:       def text_token text, kind
147:       end

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

By default, it calls text_token or block_token, depending on whether text is a String.

[Source]

     # File lib/coderay/encoder.rb, line 134
134:       def token text, kind
135:         out =
136:           if text.is_a? ::String
137:             text_token text, kind
138:           elsif text.is_a? ::Symbol
139:             block_token text, kind
140:           else
141:             raise 'Unknown token text type: %p' % text
142:           end
143:         @out << out if defined?(@out) && @out
144:       end

[Validate]