CodeRay diff

Diff code posted by murphy
created at 21 Aug 11:22

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
6.08 KB in 14 ms with coderay