From 0c9418057607d6aaf11754978662dfb60b3865fa Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Sat, 20 Aug 2011 17:09:50 +0200 Subject: cleanup CodeRay.scan* methods --- etc/CodeRay.tmproj | 16 ++++++++-------- lib/coderay.rb | 15 ++++++--------- lib/coderay/encoders/_map.rb | 3 ++- lib/coderay/scanner.rb | 8 ++++---- test/functional/basic.rb | 4 +--- test/unit/duo.rb | 28 ++++++++++++++++++++++++---- 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 @@ documents + expanded + name lib regexFolderFilter @@ -24,27 +26,25 @@ filename ../coderay.gemspec lastUsed - 2011-08-19T02:13:23Z + 2011-08-19T02:43:54Z filename ../Changes-1.0.textile lastUsed - 2011-08-19T01:12:40Z + 2011-08-19T03:20:53Z filename ../README_INDEX.rdoc lastUsed - 2011-08-19T02:16:06Z + 2011-08-19T02:40:24Z filename ../README.textile lastUsed - 2011-08-19T02:29:46Z - selected - + 2011-08-19T02:38:26Z filename @@ -145,7 +145,7 @@ filename ../test/scanners/coderay_suite.rb lastUsed - 2011-08-19T00:50:30Z + 2011-08-19T03:16:08Z filename @@ -179,6 +179,6 @@ showFileHierarchyDrawer windowFrame - {{214, 4}, {1066, 774}} + {{0, 4}, {1066, 774}} 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"') -def test: "pass" - 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 -- cgit v1.2.1