diff options
author | murphy <murphy@rubychan.de> | 2005-10-01 06:04:52 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2005-10-01 06:04:52 +0000 |
commit | 3d8868dd0b9898d589ecdff5151ed5d47956f937 (patch) | |
tree | 19a1ed947e24145818d483b3c0211a44d10f557f /lib/coderay/tokens.rb | |
parent | c194637b0d9af5ee756c5efa0942fe8cc09769e5 (diff) | |
download | coderay-3d8868dd0b9898d589ecdff5151ed5d47956f937.tar.gz |
Demos updated, rewritten, enhanced, tested.
Some code cleanups.
Bugs fixed, scanner and encoder improved:
count.rb: marked Streamable
html_css.rb: style for inline numbers
html.rb: changed options; :line_numbers_offset is now :line_number_start
html_output.rb: offset for inline numbers fixed
html.rb: token text no longer changed by gsub! while highlighting (this is even faster!)
text.rb, plugin.rb: reindented
ruby.rb: eleminated multiple assignments for speed
tokens.rb: reindented, Tokens#to_s added, #<< returns self
Plugin system: bugs fixed, error messages improved.
Diffstat (limited to 'lib/coderay/tokens.rb')
-rw-r--r-- | lib/coderay/tokens.rb | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/coderay/tokens.rb b/lib/coderay/tokens.rb index 988008e..9318844 100644 --- a/lib/coderay/tokens.rb +++ b/lib/coderay/tokens.rb @@ -1,5 +1,7 @@ module CodeRay
+ # = Tokens
+ #
# The Tokens class represents a list of tokens returnd from
# a Scanner.
#
@@ -137,6 +139,15 @@ module CodeRay encoder.encode_tokens self, options
end
+
+ # Turn into a string using Encoders::Text.
+ #
+ # +options+ are passed to the encoder if given.
+ def to_s options = {}
+ encode :text, options
+ end
+
+
# Redirects unknown methods to encoder calls.
#
# For example, if you call +tokens.html+, the HTML encoder
@@ -152,13 +163,13 @@ module CodeRay # in most Encoders. It basically makes the output smaller.
#
# Combined with dump, it saves space for the cost
- # calculating time.
- #
- # If the scanner is written carefully, this is not required -
- # for example, consecutive //-comment lines can already be
- # joined in one token by the Scanner.
+ # calculating time.
+ #
+ # If the scanner is written carefully, this is not required -
+ # for example, consecutive //-comment lines can already be
+ # joined in one token by the Scanner.
def optimize
- print ' Tokens#optimize: before: %d - ' % size if $DEBUG
+ print ' Tokens#optimize: before: %d - ' % size if $DEBUG
last_kind = last_text = nil
new = self.class.new
each do |text, kind|
@@ -177,8 +188,8 @@ module CodeRay end
end
new << [last_text, last_kind] if last_kind
- print 'after: %d (%d saved = %2.0f%%)' %
- [new.size, size - new.size, 1.0 - (new.size.to_f / size)] if $DEBUG
+ print 'after: %d (%d saved = %2.0f%%)' %
+ [new.size, size - new.size, 1.0 - (new.size.to_f / size)] if $DEBUG
new
end
@@ -241,6 +252,8 @@ module CodeRay end
+ # = TokenStream
+ #
# The TokenStream class is a fake Array without elements.
#
# It redirects the method << to a block given at creation.
@@ -283,9 +296,12 @@ module CodeRay end
# Calls +block+ with +token+ and increments size.
+ #
+ # Returns self.
def << token
@callback.call token
@size += 1
+ self
end
# This method is not implemented due to speed reasons. Use Tokens.
@@ -306,5 +322,3 @@ module CodeRay end
end
-
-# vim:sw=2:ts=2:et:tw=78
|