summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2011-08-20 17:09:50 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2011-08-20 17:09:50 +0200
commit0c9418057607d6aaf11754978662dfb60b3865fa (patch)
tree558b8ce33309de9ef28ad901459d96ddf1c46bf5
parentc073e4d256354d0d25fa65d1bfb0ab754198ff0a (diff)
downloadcoderay-0c9418057607d6aaf11754978662dfb60b3865fa.tar.gz
cleanup CodeRay.scan* methods
-rw-r--r--etc/CodeRay.tmproj16
-rw-r--r--lib/coderay.rb15
-rw-r--r--lib/coderay/encoders/_map.rb3
-rw-r--r--lib/coderay/scanner.rb8
-rwxr-xr-xtest/functional/basic.rb4
-rw-r--r--test/unit/duo.rb28
6 files changed, 45 insertions, 29 deletions
diff --git a/etc/CodeRay.tmproj b/etc/CodeRay.tmproj
index 1886802..285255b 100644
--- a/etc/CodeRay.tmproj
+++ b/etc/CodeRay.tmproj
@@ -5,6 +5,8 @@
<key>documents</key>
<array>
<dict>
+ <key>expanded</key>
+ <true/>
<key>name</key>
<string>lib</string>
<key>regexFolderFilter</key>
@@ -24,27 +26,25 @@
<key>filename</key>
<string>../coderay.gemspec</string>
<key>lastUsed</key>
- <date>2011-08-19T02:13:23Z</date>
+ <date>2011-08-19T02:43:54Z</date>
</dict>
<dict>
<key>filename</key>
<string>../Changes-1.0.textile</string>
<key>lastUsed</key>
- <date>2011-08-19T01:12:40Z</date>
+ <date>2011-08-19T03:20:53Z</date>
</dict>
<dict>
<key>filename</key>
<string>../README_INDEX.rdoc</string>
<key>lastUsed</key>
- <date>2011-08-19T02:16:06Z</date>
+ <date>2011-08-19T02:40:24Z</date>
</dict>
<dict>
<key>filename</key>
<string>../README.textile</string>
<key>lastUsed</key>
- <date>2011-08-19T02:29:46Z</date>
- <key>selected</key>
- <true/>
+ <date>2011-08-19T02:38:26Z</date>
</dict>
<dict>
<key>filename</key>
@@ -145,7 +145,7 @@
<key>filename</key>
<string>../test/scanners/coderay_suite.rb</string>
<key>lastUsed</key>
- <date>2011-08-19T00:50:30Z</date>
+ <date>2011-08-19T03:16:08Z</date>
</dict>
<dict>
<key>filename</key>
@@ -179,6 +179,6 @@
<key>showFileHierarchyDrawer</key>
<true/>
<key>windowFrame</key>
- <string>{{214, 4}, {1066, 774}}</string>
+ <string>{{0, 4}, {1066, 774}}</string>
</dict>
</plist>
diff --git a/lib/coderay.rb b/lib/coderay.rb
index 9779ff5..2ae58d8 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -159,8 +159,7 @@ module CodeRay
# See also demo/demo_simple.
def scan code, lang, options = {}, &block
# FIXME: return a proxy for direct-stream encoding
- scanner = Scanners[lang].new code, options, &block
- scanner.tokenize
+ scanner(lang, options, &block).tokenize code
end
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
@@ -175,11 +174,9 @@ module CodeRay
# require 'coderay'
# page = CodeRay.scan_file('some_c_code.c').html
def scan_file filename, lang = :auto, options = {}, &block
- file = IO.read filename
- if lang == :auto
- lang = FileType.fetch filename, :text, true
- end
- scan file, lang, options = {}, &block
+ lang = FileType.fetch filename, :text, true if lang == :auto
+ code = File.read filename
+ scan code, lang, options = {}, &block
end
# Encode a string.
@@ -261,8 +258,8 @@ module CodeRay
# +options+ to it.
#
# See Scanner.new.
- def scanner lang, options = {}
- Scanners[lang].new '', options
+ def scanner lang, options = {}, &block
+ Scanners[lang].new '', options, &block
end
# Extract the options for the scanner from the +options+ hash.
diff --git a/lib/coderay/encoders/_map.rb b/lib/coderay/encoders/_map.rb
index e5cbdeb..4cca196 100644
--- a/lib/coderay/encoders/_map.rb
+++ b/lib/coderay/encoders/_map.rb
@@ -8,7 +8,8 @@ module Encoders
:remove_comments => :comment_filter,
:stats => :statistic,
:term => :terminal,
- :tty => :terminal
+ :tty => :terminal,
+ :yml => :yaml
# No default because Tokens#nonsense should raise NoMethodError.
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index ec89b87..27b6153 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -69,6 +69,8 @@ module CodeRay
def normalize code
# original = code
code = code.to_s unless code.is_a? ::String
+ return code if code.empty?
+
if code.respond_to? :encoding
code = encode_with_encoding code, self.encoding
else
@@ -183,14 +185,12 @@ module CodeRay
@tokens = options[:tokens] || @tokens || Tokens.new
@tokens.scanner = self if @tokens.respond_to? :scanner=
case source
- when String
- self.string = source
when Array
- self.string = source.join
+ self.string = self.class.normalize(source.join)
when nil
reset
else
- raise ArgumentError, 'expected String, Array, or nil'
+ self.string = self.class.normalize(source)
end
begin
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
index 1702e4e..2654359 100755
--- a/test/functional/basic.rb
+++ b/test/functional/basic.rb
@@ -213,9 +213,7 @@ more code # and another comment, in-line.
def test_scanner_tokenize
assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo')
assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar'])
- assert_raise ArgumentError do
- CodeRay::Scanners::Plain.new.tokenize 42
- end
+ CodeRay::Scanners::Plain.new.tokenize 42
end
def test_scanner_tokens
diff --git a/test/unit/duo.rb b/test/unit/duo.rb
index e7f3f91..62bd57b 100644
--- a/test/unit/duo.rb
+++ b/test/unit/duo.rb
@@ -16,10 +16,30 @@ class DuoTest < Test::Unit::TestCase
end
def test_call
- duo = CodeRay::Duo[:python => :xml]
- assert_equal <<-'XML'.chomp, duo.call('def test: "pass"')
-<?xml version='1.0'?><coderay-tokens><keyword>def</keyword> <method>test</method><operator>:</operator> <string><delimiter>&quot;</delimiter><content>pass</content><delimiter>&quot;</delimiter></string></coderay-tokens>
- XML
+ duo = CodeRay::Duo[:python => :yml]
+ assert_equal <<-'YAML', duo.call('def test: "pass"')
+---
+- - def
+ - :keyword
+- - " "
+ - :space
+- - test
+ - :method
+- - ":"
+ - :operator
+- - " "
+ - :space
+- - :begin_group
+ - :string
+- - "\""
+ - :delimiter
+- - pass
+ - :content
+- - "\""
+ - :delimiter
+- - :end_group
+ - :string
+ YAML
end
end