Skip to content

Commit 2f97912

Browse files
committed
TokensProxy: wrap up
1 parent 14b4728 commit 2f97912

File tree

7 files changed

+19
-18
lines changed

7 files changed

+19
-18
lines changed

Changes-1.0.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The @coderay@ executable was rewritten and has a few new features:
6161

6262
h3. @Tokens@
6363

64-
* *NEW* methods @encode_with@, @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
64+
* *NEW* methods @count@, @begin_group@, @end_group@, @begin_line@, and @end_line@.
6565
* *REMOVED* methods @#stream?@, @#each_text_token@.
6666
* *REMOVED* @#text@ and @#text_size@ methods. Use the @Text@ encoder instead.
6767
* *REMOVED* special implementation of @#each@ taking a filter parameter. Use @TokenKindFilter@ instead.

lib/coderay/tokens.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ class Tokens < Array
6464
#
6565
# options are passed to the encoder.
6666
def encode encoder, options = {}
67-
unless encoder.is_a? Encoders::Encoder
68-
if encoder.respond_to? :to_sym
69-
encoder_class = Encoders[encoder]
70-
end
71-
encoder = encoder_class.new options
72-
end
67+
encoder = Encoders[encoder].new options if encoder.respond_to? :to_sym
7368
encoder.encode_tokens self, options
7469
end
7570

@@ -83,15 +78,11 @@ def to_s
8378
# For example, if you call +tokens.html+, the HTML encoder
8479
# is used to highlight the tokens.
8580
def method_missing meth, options = {}
86-
encode_with meth, options
81+
encode meth, options
8782
rescue PluginHost::PluginNotFound
8883
super
8984
end
9085

91-
def encode_with encoder, options = {}
92-
Encoders[encoder].new(options).encode_tokens self
93-
end
94-
9586
# Returns the tokens compressed by joining consecutive
9687
# tokens of the same kind.
9788
#

lib/coderay/tokens_proxy.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module CodeRay
22

3-
class TokensProxy < Struct.new :code, :lang, :options, :block
3+
class TokensProxy < Struct.new :input, :lang, :options, :block
44

55
def method_missing method, *args, &blk
6+
encode method, *args
7+
rescue PluginHost::PluginNotFound
68
tokens.send(method, *args, &blk)
79
end
810

911
def tokens
10-
@tokens ||= scanner.tokenize(code)
12+
@tokens ||= scanner.tokenize(input)
1113
end
1214

1315
def each *args, &blk
@@ -22,6 +24,14 @@ def scanner
2224
@scanner ||= CodeRay.scanner(lang, options, &block)
2325
end
2426

27+
def encode encoder, options = {}
28+
if encoder.respond_to? :to_sym
29+
CodeRay.encode(input, lang, encoder, options)
30+
else
31+
encoder.encode_tokens tokens, options
32+
end
33+
end
34+
2535
end
2636

2737
end

test/functional/basic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_version
2626
].flatten
2727
def test_simple_scan
2828
assert_nothing_raised do
29-
assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).to_ary
29+
assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).tokens
3030
end
3131
end
3232

test/unit/count.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_count
99
# a minimal Ruby program
1010
puts "Hello world!"
1111
RUBY
12-
assert_equal 11, tokens.encode_with(:count)
12+
assert_equal 11, tokens.encode(:count)
1313
end
1414

1515
end

test/unit/null.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_null
88
puts "Hello world!"
99
RUBY
1010
tokens = CodeRay.scan ruby, :ruby
11-
assert_equal '', tokens.encode_with(:null)
11+
assert_equal '', tokens.encode(:null)
1212
end
1313

1414
end

test/unit/text.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_count
88
puts "Hello world!"
99
RUBY
1010
tokens = CodeRay.scan ruby, :ruby
11-
assert_equal ruby, tokens.encode_with(:text)
11+
assert_equal ruby, tokens.encode(:text)
1212
end
1313

1414
end

0 commit comments

Comments
 (0)