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.
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
codeafter tokenizing it using the Scanner forlang. -
#encode_stream(code, lang, options = {}) ⇒ Object
Encode the given
codeusing the Scanner forlangin 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
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
codestring and alang -
encode_tokens expects a
tokensobject 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.
70 71 72 73 74 |
# File 'lib/coderay/encoder.rb', line 70 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.
57 58 59 |
# File 'lib/coderay/encoder.rb', line 57 def @options end |
#token_stream ⇒ Object (readonly)
Returns the value of attribute token_stream.
32 33 34 |
# File 'lib/coderay/encoder.rb', line 32 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.
43 44 45 46 47 48 49 |
# File 'lib/coderay/encoder.rb', line 43 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.
37 38 39 |
# File 'lib/coderay/encoder.rb', line 37 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.
86 87 88 89 90 91 |
# File 'lib/coderay/encoder.rb', line 86 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.
99 100 101 102 103 104 105 106 107 |
# File 'lib/coderay/encoder.rb', line 99 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.
77 78 79 80 81 82 |
# File 'lib/coderay/encoder.rb', line 77 def encode_tokens tokens, = {} = @options.merge setup compile tokens, finish end |
#file_extension ⇒ Object
Return the default file extension for outputs of this encoder.
115 116 117 |
# File 'lib/coderay/encoder.rb', line 115 def file_extension self.class::FILE_EXTENSION end |
#to_proc ⇒ Object
Behave like a proc. The token method is converted to a proc.
110 111 112 |
# File 'lib/coderay/encoder.rb', line 110 def to_proc method(:token).to_proc end |