diff options
Diffstat (limited to 'lib/coderay/encoders')
-rw-r--r-- | lib/coderay/encoders/_map.rb | 12 | ||||
-rw-r--r-- | lib/coderay/encoders/count.rb | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/debug.rb | 68 | ||||
-rw-r--r-- | lib/coderay/encoders/div.rb | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/html/classes.rb | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/html/css.rb | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/html/output.rb | 2 | ||||
-rw-r--r-- | lib/coderay/encoders/null.rb | 32 | ||||
-rw-r--r-- | lib/coderay/encoders/span.rb | 9 | ||||
-rw-r--r-- | lib/coderay/encoders/statistic.rb | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/tokens.rb | 70 | ||||
-rw-r--r-- | lib/coderay/encoders/xml.rb | 105 | ||||
-rw-r--r-- | lib/coderay/encoders/yaml.rb | 23 |
13 files changed, 187 insertions, 164 deletions
diff --git a/lib/coderay/encoders/_map.rb b/lib/coderay/encoders/_map.rb index b9fe5af..13c4a9d 100644 --- a/lib/coderay/encoders/_map.rb +++ b/lib/coderay/encoders/_map.rb @@ -1,8 +1,8 @@ module CodeRay
- module Encoders
-
- map :stats => :statistic,
- :plain => :text
-
- end
+module Encoders
+
+ map :stats => :statistic,
+ :plain => :text
+
+end
end
diff --git a/lib/coderay/encoders/count.rb b/lib/coderay/encoders/count.rb index d49e0bc..c662ead 100644 --- a/lib/coderay/encoders/count.rb +++ b/lib/coderay/encoders/count.rb @@ -6,12 +6,12 @@ module Encoders include Streamable
register_for :count
- protected
-
+ protected
+
def setup options
@out = 0
end
-
+
def token text, kind
@out += 1
end
diff --git a/lib/coderay/encoders/debug.rb b/lib/coderay/encoders/debug.rb index 7358e8b..d6aac8b 100644 --- a/lib/coderay/encoders/debug.rb +++ b/lib/coderay/encoders/debug.rb @@ -1,38 +1,46 @@ module CodeRay
- module Encoders
-
- # = Debug Encoder
- class Debug < Encoder
-
- include Streamable
- register_for :debug
-
- FILE_EXTENSION = 'raydebug'
-
- protected
- def text_token text, kind
- @out <<
- if kind == :space
- text
- else
- text = text.gsub(/[)\\]/, '\\\\\0')
- "#{kind}(#{text})"
- end
- end
-
- def block_token action, kind
- @out << super
- end
+module Encoders
+
+ # = Debug Encoder
+ #
+ # Fast encoder producing simple debug output.
+ #
+ # It is readable and diff-able and is used for testing.
+ #
+ # You cannot fully restore the tokens information from the
+ # output, because consecutive :space tokens are merged.
+ # Use Tokens#dump for caching purposes.
+ class Debug < Encoder
+
+ include Streamable
+ register_for :debug
+
+ FILE_EXTENSION = 'raydebug'
+
+ protected
+ def text_token text, kind
+ @out <<
+ if kind == :space
+ text
+ else
+ text = text.gsub(/[)\\]/, '\\\\\0')
+ "#{kind}(#{text})"
+ end
+ end
- def open_token kind
- "#{kind}<"
- end
+ def block_token action, kind
+ @out << super
+ end
- def close_token kind
- ">"
- end
+ def open_token kind
+ "#{kind}<"
+ end
+ def close_token kind
+ ">"
end
end
+
+end
end
diff --git a/lib/coderay/encoders/div.rb b/lib/coderay/encoders/div.rb index 2379a4c..c389a0d 100644 --- a/lib/coderay/encoders/div.rb +++ b/lib/coderay/encoders/div.rb @@ -1,4 +1,5 @@ -module CodeRay module Encoders
+module CodeRay
+module Encoders
load :html
@@ -15,4 +16,5 @@ module CodeRay module Encoders end
-end end
+end
+end
diff --git a/lib/coderay/encoders/html/classes.rb b/lib/coderay/encoders/html/classes.rb index 3d40a42..e21fce8 100644 --- a/lib/coderay/encoders/html/classes.rb +++ b/lib/coderay/encoders/html/classes.rb @@ -1,4 +1,5 @@ -module CodeRay module Encoders
+module CodeRay
+module Encoders
class HTML
@@ -68,4 +69,5 @@ module CodeRay module Encoders end
-end end
+end
+end
diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index ad055aa..fcd9859 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -1,4 +1,5 @@ -module CodeRay module Encoders
+module CodeRay
+module Encoders
class HTML
class CSS
@@ -55,7 +56,8 @@ module CodeRay module Encoders end
end
-end end
+end
+end
if $0 == __FILE__
require 'pp'
diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb index e9a3a41..203caca 100644 --- a/lib/coderay/encoders/html/output.rb +++ b/lib/coderay/encoders/html/output.rb @@ -1,5 +1,5 @@ module CodeRay
- module Encoders
+module Encoders
class HTML
diff --git a/lib/coderay/encoders/null.rb b/lib/coderay/encoders/null.rb index e995cb0..0e3d073 100644 --- a/lib/coderay/encoders/null.rb +++ b/lib/coderay/encoders/null.rb @@ -1,26 +1,26 @@ module CodeRay
- module Encoders
+module Encoders
- # = Null Encoder
- #
- # Does nothing and returns an empty string.
- class Null < Encoder
+ # = Null Encoder
+ #
+ # Does nothing and returns an empty string.
+ class Null < Encoder
- include Streamable
- register_for :null
+ include Streamable
+ register_for :null
- # Defined for faster processing
- def to_proc
- proc {}
- end
-
- protected
+ # Defined for faster processing
+ def to_proc
+ proc {}
+ end
- def token(*)
- # do nothing
- end
+ protected
+ def token(*)
+ # do nothing
end
end
+
+end
end
diff --git a/lib/coderay/encoders/span.rb b/lib/coderay/encoders/span.rb index e222889..4d74277 100644 --- a/lib/coderay/encoders/span.rb +++ b/lib/coderay/encoders/span.rb @@ -1,5 +1,6 @@ -module CodeRay module Encoders
-
+module CodeRay
+module Encoders
+
load :html
class Span < HTML
@@ -12,6 +13,8 @@ module CodeRay module Encoders :css => :style,
:wrap => :span,
})
+
end
-end end
+end
+end
diff --git a/lib/coderay/encoders/statistic.rb b/lib/coderay/encoders/statistic.rb index b8905ff..0cf8831 100644 --- a/lib/coderay/encoders/statistic.rb +++ b/lib/coderay/encoders/statistic.rb @@ -1,4 +1,5 @@ -module CodeRay module Encoders
+module CodeRay
+module Encoders
# Makes a statistic for the given tokens.
class Statistic < Encoder
@@ -76,4 +77,5 @@ Token Types (%d): end
-end end
+end
+end
diff --git a/lib/coderay/encoders/tokens.rb b/lib/coderay/encoders/tokens.rb index 4573307..2bcca40 100644 --- a/lib/coderay/encoders/tokens.rb +++ b/lib/coderay/encoders/tokens.rb @@ -1,44 +1,44 @@ module CodeRay
- module Encoders
+module Encoders
- # The Tokens encoder converts the tokens to a simple
- # readable format. It doesn't use colors and is mainly
- # intended for console output.
- #
- # The tokens are converted with Tokens.write_token.
- #
- # The format is:
- #
- # <token-kind> \t <escaped token-text> \n
- #
- # Example:
- #
- # require 'coderay'
- # puts CodeRay.scan("puts 3 + 4", :ruby).tokens
- #
- # prints:
- #
- # ident puts
- # space
- # integer 3
- # space
- # operator +
- # space
- # integer 4
- #
- class Tokens < Encoder
+ # The Tokens encoder converts the tokens to a simple
+ # readable format. It doesn't use colors and is mainly
+ # intended for console output.
+ #
+ # The tokens are converted with Tokens.write_token.
+ #
+ # The format is:
+ #
+ # <token-kind> \t <escaped token-text> \n
+ #
+ # Example:
+ #
+ # require 'coderay'
+ # puts CodeRay.scan("puts 3 + 4", :ruby).tokens
+ #
+ # prints:
+ #
+ # ident puts
+ # space
+ # integer 3
+ # space
+ # operator +
+ # space
+ # integer 4
+ #
+ class Tokens < Encoder
- include Streamable
- register_for :tokens
+ include Streamable
+ register_for :tokens
- FILE_EXTENSION = 'tok'
-
- protected
- def token *args
- @out << CodeRay::Tokens.write_token(*args)
- end
+ FILE_EXTENSION = 'tok'
+ protected
+ def token *args
+ @out << CodeRay::Tokens.write_token(*args)
end
end
+
+end
end
diff --git a/lib/coderay/encoders/xml.rb b/lib/coderay/encoders/xml.rb index 5596f46..246fe0a 100644 --- a/lib/coderay/encoders/xml.rb +++ b/lib/coderay/encoders/xml.rb @@ -1,70 +1,71 @@ module CodeRay
- module Encoders
+module Encoders
- # = Debug Encoder
- class XML < Encoder
+ # = XML Encoder
+ #
+ # Uses REXML. Very slow.
+ class XML < Encoder
- include Streamable
- register_for :xml
+ include Streamable
+ register_for :xml
- FILE_EXTENSION = 'xml'
+ FILE_EXTENSION = 'xml'
- require 'rexml/document'
+ require 'rexml/document'
- DEFAULT_OPTIONS = {
- :tab_width => 8,
- :pretty => -1,
- :transitive => false,
- }
+ DEFAULT_OPTIONS = {
+ :tab_width => 8,
+ :pretty => -1,
+ :transitive => false,
+ }
- protected
-
- def setup options
- @out = ''
- @doc = REXML::Document.new
- @doc << REXML::XMLDecl.new
- @tab_width = options[:tab_width]
- @root = @node = @doc.add_element('coderay-tokens')
- end
-
- def finish options
- @doc.write @out, options[:pretty], options[:transitive], true
- @out
+ protected
+
+ def setup options
+ @out = ''
+ @doc = REXML::Document.new
+ @doc << REXML::XMLDecl.new
+ @tab_width = options[:tab_width]
+ @root = @node = @doc.add_element('coderay-tokens')
+ end
+
+ def finish options
+ @doc.write @out, options[:pretty], options[:transitive], true
+ @out
+ end
+
+ def text_token text, kind
+ if kind == :space
+ token = @node
+ else
+ token = @node.add_element kind.to_s
end
-
- def text_token text, kind
- if kind == :space
- token = @node
+ text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
+ case
+ when space
+ token << REXML::Text.new(space, true)
+ when tab
+ token << REXML::Text.new(tab, true)
+ when nl
+ token << REXML::Text.new(nl, true)
else
- token = @node.add_element kind.to_s
- end
- text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
- case
- when space
- token << REXML::Text.new(space, true)
- when tab
- token << REXML::Text.new(tab, true)
- when nl
- token << REXML::Text.new(nl, true)
- else
- token << REXML::Text.new($&)
- end
+ token << REXML::Text.new($&)
end
end
+ end
- def open_token kind
- @node = @node.add_element kind.to_s
- end
+ def open_token kind
+ @node = @node.add_element kind.to_s
+ end
- def close_token kind
- if @node == @root
- raise 'no token to close!'
- end
- @node = @node.parent
+ def close_token kind
+ if @node == @root
+ raise 'no token to close!'
end
-
+ @node = @node.parent
end
end
+
+end
end
-
diff --git a/lib/coderay/encoders/yaml.rb b/lib/coderay/encoders/yaml.rb index 4e2b7a1..b842647 100644 --- a/lib/coderay/encoders/yaml.rb +++ b/lib/coderay/encoders/yaml.rb @@ -1,19 +1,22 @@ module CodeRay
- module Encoders
+module Encoders
- class YAML < Encoder
+ # = YAML Encoder
+ #
+ # Slow.
+ class YAML < Encoder
- register_for :yaml
+ register_for :yaml
- FILE_EXTENSION = 'yaml'
-
- protected
- def compile tokens, options
- require 'yaml'
- @out = tokens.to_a.to_yaml
- end
+ FILE_EXTENSION = 'yaml'
+ protected
+ def compile tokens, options
+ require 'yaml'
+ @out = tokens.to_a.to_yaml
end
end
+
+end
end
|