diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2011-08-20 17:09:50 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2011-08-20 17:09:50 +0200 |
commit | 0c9418057607d6aaf11754978662dfb60b3865fa (patch) | |
tree | 558b8ce33309de9ef28ad901459d96ddf1c46bf5 /lib | |
parent | c073e4d256354d0d25fa65d1bfb0ab754198ff0a (diff) | |
download | coderay-0c9418057607d6aaf11754978662dfb60b3865fa.tar.gz |
cleanup CodeRay.scan* methods
Diffstat (limited to 'lib')
-rw-r--r-- | lib/coderay.rb | 15 | ||||
-rw-r--r-- | lib/coderay/encoders/_map.rb | 3 | ||||
-rw-r--r-- | lib/coderay/scanner.rb | 8 |
3 files changed, 12 insertions, 14 deletions
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 |