Class: CodeRay::Encoders::Encoder
- Inherits:
-
Object
- Object
- CodeRay::Encoders::Encoder
- 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, Filter, HTML, JSON, LinesOfCode, Null, Statistic, Term, Text, XML, YAML
Constant Summary collapse
- DEFAULT_OPTIONS =
Subclasses are to store their default options in this constant.
{ :stream => false }
Instance Attribute Summary collapse
-
#options ⇒ Object
The options you gave the Encoder at creating.
-
#token_stream ⇒ Object
readonly
Returns the value of attribute token_stream.
Class Method Summary collapse
-
.const_missing(sym) ⇒ Object
If FILE_EXTENSION isn’t defined, this method returns the downcase class name instead.
-
.streamable? ⇒ Boolean
Returns if the Encoder can be used in streaming mode.
Instance Method Summary collapse
-
#encode(code, lang, options = {}) ⇒ Object
(also: #highlight)
Encode the given
code
after tokenizing it using the Scanner forlang
. -
#encode_stream(code, lang, options = {}) ⇒ Object
Encode the given
code
using the Scanner forlang
in streaming mode. -
#encode_tokens(tokens, options = {}) ⇒ Object
Encode a Tokens object.
-
#file_extension ⇒ Object
Return the default file extension for outputs of this encoder.
-
#initialize(options = {}) ⇒ Encoder
constructor
Creates a new Encoder.
-
#to_proc ⇒ Object
Behave like a proc.
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 alang
-
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 = self.class::DEFAULT_OPTIONS.merge raise "I am only the basic Encoder class. I can't encode "\ "anything. :( Use my subclasses." if self.class == Encoder end |
Instance Attribute Details
#options ⇒ Object
The options you gave the Encoder at creating.
55 56 57 |
# File 'lib/coderay/encoder.rb', line 55 def @options end |
#token_stream ⇒ Object (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.
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.merge = CodeRay.() tokens = CodeRay.scan code, lang, encode_tokens tokens, end |
#encode_stream(code, lang, options = {}) ⇒ Object
Encode the given code
using the Scanner for lang
in streaming mode.
97 98 99 100 101 102 103 104 105 |
# File 'lib/coderay/encoder.rb', line 97 def encode_stream code, lang, = {} raise NotStreamableError, self unless kind_of? Streamable = @options.merge setup = CodeRay. @token_stream = CodeRay.scan_stream code, lang, , &self finish 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.merge setup compile tokens, finish end |
#file_extension ⇒ Object
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_proc ⇒ Object
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 |