class CodeRay::Encoders::Encoder
use its subclasses Div and Span.
If you want the highlighted code in a div or a span instead,
html page.
(CodeRay::Encoders::HTML). It highlights the code in a colorful
The most common Encoder is surely the HTML encoder
Encoder instances take a Tokens object and do something with it.
Tokens, it forms the highlighting triad.
The Encoder base class. Together with Scanner and
= Encoder
def << token
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
def begin_group kind
def begin_group kind end
def begin_line kind
def begin_line kind end
def compile tokens, options = {}
The already created +tokens+ object must be used; it must be a
Do the encoding.
def compile tokens, options = {} content = nil for item in tokens if item.is_a? Array raise ArgumentError, 'Two-element array tokens are no longer supported.' end if content token content, item content = nil else content = item end end raise 'odd number list for Tokens' if content end
def const_missing sym
If FILE_EXTENSION isn't defined, this method returns the
def const_missing sym if sym == :FILE_EXTENSION (defined?(@plugin_id) && @plugin_id || name[/\w+$/].downcase).to_s else super end end
def encode code, lang, options = {}
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
def encode_tokens tokens, options = {}
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
def end_group kind
def end_group kind end
def end_line kind
def end_line kind end
def file_extension
def file_extension self::FILE_EXTENSION end
def file_extension
def file_extension self.class.file_extension end
def finish options
Called with merged options after encoding starts.
def finish options @out end
def get_output options
def get_output options options[:out] || '' end
def initialize options = {}
Each method has an optional +options+ parameter. These are
- encode_tokens expects a +tokens+ object instead
- encode simply takes a +code+ string and a +lang+
Encoder objects provide three encode methods:
as you don't overwrite it there by passing additional options.
+options+ is saved and used for all encode operations, as long
Creates a new Encoder.
def initialize options = {} @options = self.class::DEFAULT_OPTIONS.merge options @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = false end
def output data
def output data @out << data.to_s data end
def setup options
Sets @out to an empty string.
Called with merged options before encoding starts.
def setup options @out = get_output(options) end
def text_token text, kind
def text_token text, kind @out << text end
def token content, kind
By default, it calls text_token, begin_group, end_group, begin_line,
For simple scanners, it's enougth to implement this method.
Called with +content+ and +kind+ of the currently scanned token.
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