summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes-1.0.textile2
-rw-r--r--lib/coderay.rb3
-rw-r--r--lib/coderay/tokens.rb13
-rw-r--r--lib/coderay/tokens_proxy.rb37
-rwxr-xr-xtest/functional/basic.rb2
-rwxr-xr-xtest/functional/examples.rb8
-rw-r--r--test/unit/count.rb2
-rw-r--r--test/unit/debug.rb3
-rw-r--r--test/unit/lines_of_code.rb13
-rw-r--r--test/unit/null.rb2
-rw-r--r--test/unit/text.rb2
11 files changed, 56 insertions, 31 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.rb b/lib/coderay.rb
index 503de7d..c897220 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -134,6 +134,7 @@ module CodeRay
# Tokens
autoload :Tokens, 'coderay/tokens'
+ autoload :TokensProxy, 'coderay/tokens_proxy'
autoload :TokenKinds, 'coderay/token_kinds'
# Plugin system
@@ -159,7 +160,7 @@ module CodeRay
# See also demo/demo_simple.
def scan code, lang, options = {}, &block
# FIXME: return a proxy for direct-stream encoding
- scanner(lang, options, &block).tokenize code
+ TokensProxy.new code, lang, options, block
end
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
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
new file mode 100644
index 0000000..b333e57
--- /dev/null
+++ b/lib/coderay/tokens_proxy.rb
@@ -0,0 +1,37 @@
+module CodeRay
+
+ 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(input)
+ end
+
+ def each *args, &blk
+ tokens.each(*args, &blk)
+ end
+
+ def count
+ tokens.count
+ end
+
+ def scanner
+ @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/functional/examples.rb b/test/functional/examples.rb
index 44bc47f..d565fcc 100755
--- a/test/functional/examples.rb
+++ b/test/functional/examples.rb
@@ -48,6 +48,8 @@ end
# keep scanned tokens for later use
tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
+ assert_kind_of CodeRay::TokensProxy, tokens
+
assert_equal ["{", :operator, " ", :space, :begin_group, :key,
"\"", :delimiter, "just", :content, "\"", :delimiter,
:end_group, :key, ":", :operator, " ", :space,
@@ -56,8 +58,8 @@ end
" ", :space, :begin_group, :key, "\"", :delimiter,
"example", :content, "\"", :delimiter, :end_group, :key,
":", :operator, " ", :space, "42", :integer,
- " ", :space, "}", :operator], tokens
-
+ " ", :space, "}", :operator], tokens.tokens
+
# produce a token statistic
assert_equal <<-STATISTIC, tokens.statistic
@@ -84,7 +86,7 @@ Token Types (7):
STATISTIC
# count the tokens
- assert_equal 26, tokens.count # => 26
+ assert_equal 26, tokens.count
# produce a HTML div, but with CSS classes
div = tokens.div(:css => :class)
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/debug.rb b/test/unit/debug.rb
index 8bafcf5..f2b80bd 100644
--- a/test/unit/debug.rb
+++ b/test/unit/debug.rb
@@ -70,7 +70,8 @@ method([])]
def test_filtering_text_tokens
assert_equal TEST_OUTPUT, CodeRay::Scanners::Debug.new.tokenize(TEST_INPUT)
- assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug)
+ assert_kind_of CodeRay::TokensProxy, CodeRay.scan(TEST_INPUT, :debug)
+ assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug).tokens
end
end
diff --git a/test/unit/lines_of_code.rb b/test/unit/lines_of_code.rb
index 4231d5a..e2c0caf 100644
--- a/test/unit/lines_of_code.rb
+++ b/test/unit/lines_of_code.rb
@@ -2,6 +2,8 @@ require 'test/unit'
require 'coderay'
$VERBOSE = true
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
class LinesOfCodeTest < Test::Unit::TestCase
def test_creation
@@ -39,17 +41,8 @@ puts "Hello world!"
tokens.concat ["\n", :space]
tokens.concat ["Hello\n", :comment]
- stderr, fake_stderr = $stderr, Object.new
- begin
- $err = ''
- def fake_stderr.write x
- $err << x
- end
- $stderr = fake_stderr
+ assert_warning 'Tokens have no associated scanner, counting all nonempty lines.' do
assert_equal 1, tokens.lines_of_code
- assert_equal "Tokens have no associated scanner, counting all nonempty lines.\n", $err
- ensure
- $stderr = stderr
end
tokens.scanner = ScannerMockup.new
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