summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2011-09-19 01:47:08 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2011-09-19 01:47:08 +0200
commit2f97912d6372b3edde924f83ed283b97f6472273 (patch)
treede7cb89c4f44e03f280ef831295a33339e4d31e8
parent14b4728342af152f865f29e67d583c733e912756 (diff)
downloadcoderay-2f97912d6372b3edde924f83ed283b97f6472273.tar.gz
TokensProxy: wrap up
-rw-r--r--Changes-1.0.textile2
-rw-r--r--lib/coderay/tokens.rb13
-rw-r--r--lib/coderay/tokens_proxy.rb14
-rwxr-xr-xtest/functional/basic.rb2
-rw-r--r--test/unit/count.rb2
-rw-r--r--test/unit/null.rb2
-rw-r--r--test/unit/text.rb2
7 files changed, 19 insertions, 18 deletions
diff --git a/Changes-1.0.textile b/Changes-1.0.textile
index 2ef4130..2503f4a 100644
--- a/Changes-1.0.textile
+++ b/Changes-1.0.textile
@@ -61,7 +61,7 @@ The @coderay@ executable was rewritten and has a few new features:
h3. @Tokens@
-* *NEW* methods @encode_with@, @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
+* *NEW* methods @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
* *REMOVED* methods @#stream?@, @#each_text_token@.
* *REMOVED* @#text@ and @#text_size@ methods. Use the @Text@ encoder instead.
* *REMOVED* special implementation of @#each@ taking a filter parameter. Use @TokenKindFilter@ instead.
diff --git a/lib/coderay/tokens.rb b/lib/coderay/tokens.rb
index b357199..ee28a4e 100644
--- a/lib/coderay/tokens.rb
+++ b/lib/coderay/tokens.rb
@@ -64,12 +64,7 @@ module CodeRay
#
# options are passed to the encoder.
def encode encoder, options = {}
- unless encoder.is_a? Encoders::Encoder
- if encoder.respond_to? :to_sym
- encoder_class = Encoders[encoder]
- end
- encoder = encoder_class.new options
- end
+ encoder = Encoders[encoder].new options if encoder.respond_to? :to_sym
encoder.encode_tokens self, options
end
@@ -83,15 +78,11 @@ module CodeRay
# For example, if you call +tokens.html+, the HTML encoder
# is used to highlight the tokens.
def method_missing meth, options = {}
- encode_with meth, options
+ encode meth, options
rescue PluginHost::PluginNotFound
super
end
- def encode_with encoder, options = {}
- Encoders[encoder].new(options).encode_tokens self
- end
-
# Returns the tokens compressed by joining consecutive
# tokens of the same kind.
#
diff --git a/lib/coderay/tokens_proxy.rb b/lib/coderay/tokens_proxy.rb
index 598ad2e..b333e57 100644
--- a/lib/coderay/tokens_proxy.rb
+++ b/lib/coderay/tokens_proxy.rb
@@ -1,13 +1,15 @@
module CodeRay
- class TokensProxy < Struct.new :code, :lang, :options, :block
+ class TokensProxy < Struct.new :input, :lang, :options, :block
def method_missing method, *args, &blk
+ encode method, *args
+ rescue PluginHost::PluginNotFound
tokens.send(method, *args, &blk)
end
def tokens
- @tokens ||= scanner.tokenize(code)
+ @tokens ||= scanner.tokenize(input)
end
def each *args, &blk
@@ -22,6 +24,14 @@ module CodeRay
@scanner ||= CodeRay.scanner(lang, options, &block)
end
+ def encode encoder, options = {}
+ if encoder.respond_to? :to_sym
+ CodeRay.encode(input, lang, encoder, options)
+ else
+ encoder.encode_tokens tokens, options
+ end
+ end
+
end
end
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
index 2654359..5d03513 100755
--- a/test/functional/basic.rb
+++ b/test/functional/basic.rb
@@ -26,7 +26,7 @@ class BasicTest < Test::Unit::TestCase
].flatten
def test_simple_scan
assert_nothing_raised do
- assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).to_ary
+ assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).tokens
end
end
diff --git a/test/unit/count.rb b/test/unit/count.rb
index ad61291..448e8f1 100644
--- a/test/unit/count.rb
+++ b/test/unit/count.rb
@@ -9,7 +9,7 @@ class CountTest < Test::Unit::TestCase
# a minimal Ruby program
puts "Hello world!"
RUBY
- assert_equal 11, tokens.encode_with(:count)
+ assert_equal 11, tokens.encode(:count)
end
end \ No newline at end of file
diff --git a/test/unit/null.rb b/test/unit/null.rb
index ea516d8..d3a9b0d 100644
--- a/test/unit/null.rb
+++ b/test/unit/null.rb
@@ -8,7 +8,7 @@ class NullTest < Test::Unit::TestCase
puts "Hello world!"
RUBY
tokens = CodeRay.scan ruby, :ruby
- assert_equal '', tokens.encode_with(:null)
+ assert_equal '', tokens.encode(:null)
end
end \ No newline at end of file
diff --git a/test/unit/text.rb b/test/unit/text.rb
index 025881e..db086f5 100644
--- a/test/unit/text.rb
+++ b/test/unit/text.rb
@@ -8,7 +8,7 @@ class TextTest < Test::Unit::TestCase
puts "Hello world!"
RUBY
tokens = CodeRay.scan ruby, :ruby
- assert_equal ruby, tokens.encode_with(:text)
+ assert_equal ruby, tokens.encode(:text)
end
end \ No newline at end of file