Class: CodeRay::Encoders::Encoder

Inherits:
Object
  • Object
show all
Extended by:
Plugin
Defined in:
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, HTML, JSON, Null, Statistic, Term, Text, Tokens, XML, YAML

Constant Summary collapse

DEFAULT_OPTIONS =

Subclasses are to store their default options in this constant.

{ :stream => false }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugin

helper, included, plugin_host, plugin_id, register_for, title

Constructor Details

#initialize(options = {}) ⇒ Encoder

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
69
70
71
72
# File 'lib/coderay/encoder.rb', line 68

def initialize options = {}
  @options = self.class::DEFAULT_OPTIONS.merge options
  raise "I am only the basic Encoder class. I can't encode "\
    "anything. :( Use my subclasses." if self.class == Encoder
end

Instance Attribute Details

#optionsObject

The options you gave the Encoder at creating.



55
56
57
# File 'lib/coderay/encoder.rb', line 55

def options
  @options
end

#token_streamObject (readonly)

Returns the value of attribute token_stream.



30
31
32
# File 'lib/coderay/encoder.rb', line 30

def token_stream
  @token_stream
end

Class Method Details

.const_missing(sym) ⇒ Object

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



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

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

.streamable?Boolean

Returns if the Encoder can be used in streaming mode.

Returns:

  • (Boolean)


35
36
37
# File 'lib/coderay/encoder.rb', line 35

def streamable?
  is_a? Streamable
end

Instance Method Details

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

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



84
85
86
87
88
89
# File 'lib/coderay/encoder.rb', line 84

def encode code, lang, options = {}
  options = @options.merge options
  scanner_options = CodeRay.get_scanner_options(options)
  tokens = CodeRay.scan code, lang, scanner_options
  encode_tokens tokens, options
end

#encode_stream(code, lang, options = {}) ⇒ Object

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

Raises:



97
98
99
100
101
102
103
104
105
# File 'lib/coderay/encoder.rb', line 97

def encode_stream code, lang, options = {}
  raise NotStreamableError, self unless kind_of? Streamable
  options = @options.merge options
  setup options
  scanner_options = CodeRay.get_scanner_options options
  @token_stream =
    CodeRay.scan_stream code, lang, scanner_options, &self
  finish options
end

#encode_tokens(tokens, options = {}) ⇒ Object

Encode a Tokens object.



75
76
77
78
79
80
# File 'lib/coderay/encoder.rb', line 75

def encode_tokens tokens, options = {}
  options = @options.merge options
  setup options
  compile tokens, options
  finish options
end

#file_extensionObject

Return the default file extension for outputs of this encoder.



113
114
115
# File 'lib/coderay/encoder.rb', line 113

def file_extension
  self.class::FILE_EXTENSION
end

#to_procObject

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



108
109
110
# File 'lib/coderay/encoder.rb', line 108

def to_proc
  method(:token).to_proc
end