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 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.
# 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
Encode the given code after tokenizing it using the Scanner for lang.
# 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.
# 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
Return the default file extension for outputs of this encoder.
# File lib/coderay/encoder.rb, line 115 115: def file_extension 116: self.class::FILE_EXTENSION 117: end
# 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
# File lib/coderay/encoder.rb, line 179 179: def compile tokens, options 180: tokens.each(&self) 181: end
# 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.
# File lib/coderay/encoder.rb, line 166 166: def finish options 167: @out 168: 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.
# 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