| Class | CodeRay::Encoders::Encoder |
| In: |
lib/coderay/encoder.rb
|
| Parent: | Object |
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.
| DEFAULT_OPTIONS | = | { :stream => false } | Subclasses are to store their default options in this constant. |
| options | [RW] | The options you gave the Encoder at creating. |
| token_stream | [R] |
If FILE_EXTENSION isn‘t defined, this method returns the downcase class name instead.
# File lib/coderay/encoder.rb, line 41
41: def const_missing sym
42: if sym == :FILE_EXTENSION
43: plugin_id
44: else
45: super
46: end
47: 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.
# File lib/coderay/encoder.rb, line 68
68: def initialize options = {}
69: @options = self.class::DEFAULT_OPTIONS.merge options
70: raise "I am only the basic Encoder class. I can't encode "\
71: "anything. :( Use my subclasses." if self.class == Encoder
72: end
Encode the given code after tokenizing it using the Scanner for lang.
# File lib/coderay/encoder.rb, line 84
84: def encode code, lang, options = {}
85: options = @options.merge options
86: scanner_options = CodeRay.get_scanner_options(options)
87: tokens = CodeRay.scan code, lang, scanner_options
88: encode_tokens tokens, options
89: end
Encode the given code using the Scanner for lang in streaming mode.
# File lib/coderay/encoder.rb, line 97
97: def encode_stream code, lang, options = {}
98: raise NotStreamableError, self unless kind_of? Streamable
99: options = @options.merge options
100: setup options
101: scanner_options = CodeRay.get_scanner_options options
102: @token_stream =
103: CodeRay.scan_stream code, lang, scanner_options, &self
104: finish options
105: end
Return the default file extension for outputs of this encoder.
# File lib/coderay/encoder.rb, line 113
113: def file_extension
114: self.class::FILE_EXTENSION
115: end
# File lib/coderay/encoder.rb, line 144
144: def append_encoded_token_to_output encoded_token
145: @out << encoded_token if encoded_token && defined?(@out) && @out
146: end
Called for each line token block at the start of the line ([:begin_line, kind]).
# File lib/coderay/encoder.rb, line 181
181: def begin_line kind
182: end
Called for each block (non-text) token ([action, kind]), where action is a Symbol.
Calls open_token, close_token, begin_line, and end_line according to the value of action.
# File lib/coderay/encoder.rb, line 157
157: def block_token action, kind
158: case action
159: when :open
160: open_token kind
161: when :close
162: close_token kind
163: when :begin_line
164: begin_line kind
165: when :end_line
166: end_line kind
167: else
168: raise 'unknown block action: %p' % action
169: end
170: end
# File lib/coderay/encoder.rb, line 199
199: def compile tokens, options
200: for text, kind in tokens
201: token text, kind
202: end
203: end
# File lib/coderay/encoder.rb, line 205
205: def compile tokens, options
206: tokens.each(&self)
207: end
Called with merged options after encoding starts. The return value is the result of encoding, typically @out.
# File lib/coderay/encoder.rb, line 190
190: def finish options
191: @out
192: end
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 or block_token, depending on whether content is a String.
# File lib/coderay/encoder.rb, line 132
132: def token content, kind
133: encoded_token =
134: if content.is_a? ::String
135: text_token content, kind
136: elsif content.is_a? ::Symbol
137: block_token content, kind
138: else
139: raise 'Unknown token content type: %p' % [content]
140: end
141: append_encoded_token_to_output encoded_token
142: end