Class: CodeRay::Encoders::Encoder

Inherits:
Object
  • Object
show all
Extends:
CodeRay::Plugin
Defined in:
vendor/plugins/coderay-0.9.2/lib/coderay/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, LinesOfCode, Null, Statistic, Term, Text, XML, YAML

Constant Summary

DEFAULT_OPTIONS =

Subclasses are to store their default options in this constant.

{ :stream => false }

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods included from CodeRay::Plugin

helper, included, plugin_host, plugin_id, register_for, title

Constructor Details

- (Encoder) initialize(options = {})

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
  • encode_stream is like encode, but uses streaming mode.

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



68
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 68

def initialize options = {}

Instance Attribute Details

- (Object) options

The options you gave the Encoder at creating.



55
56
57
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 55

def options
  @options
end

- (Object) token_stream (readonly)

Returns the value of attribute token_stream



30
31
32
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 30

def token_stream
  @token_stream
end

Class Method Details

+ (Object) const_missing(sym)

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



41
42
43
44
45
46
47
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 41

def const_missing sym
  if sym == :FILE_EXTENSION
    plugin_id
  else
    super
  end
end

+ (Boolean) streamable?

Returns if the Encoder can be used in streaming mode.

Returns:

  • (Boolean)


35
36
37
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 35

def streamable?
  is_a? Streamable
end

Instance Method Details

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

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



84
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 84

def encode code, lang, options = {}

- (Object) encode_stream(code, lang, options = {})

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



97
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 97

def encode_stream code, lang, options = {}

- (Object) encode_tokens(tokens, options = {})

Encode a Tokens object.



75
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 75

def encode_tokens tokens, options = {}

- (Object) file_extension

Return the default file extension for outputs of this encoder.



113
114
115
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 113

def file_extension
  self.class::FILE_EXTENSION
end

- (Object) to_proc

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



108
109
110
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/encoder.rb', line 108

def to_proc
  method(:token).to_proc
end