coderay 1.0.0.598.pre → 1.0.0.738.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/bin/coderay +1 -1
  2. data/lib/coderay.rb +38 -32
  3. data/lib/coderay/duo.rb +1 -54
  4. data/lib/coderay/encoder.rb +31 -33
  5. data/lib/coderay/encoders/_map.rb +4 -2
  6. data/lib/coderay/encoders/comment_filter.rb +0 -61
  7. data/lib/coderay/encoders/count.rb +2 -23
  8. data/lib/coderay/encoders/debug.rb +11 -60
  9. data/lib/coderay/encoders/filter.rb +0 -46
  10. data/lib/coderay/encoders/html.rb +83 -91
  11. data/lib/coderay/encoders/html/css.rb +1 -6
  12. data/lib/coderay/encoders/html/numbering.rb +18 -21
  13. data/lib/coderay/encoders/html/output.rb +10 -52
  14. data/lib/coderay/encoders/json.rb +19 -39
  15. data/lib/coderay/encoders/lines_of_code.rb +7 -52
  16. data/lib/coderay/encoders/null.rb +6 -13
  17. data/lib/coderay/encoders/statistic.rb +30 -93
  18. data/lib/coderay/encoders/terminal.rb +3 -4
  19. data/lib/coderay/encoders/text.rb +1 -23
  20. data/lib/coderay/encoders/token_kind_filter.rb +0 -58
  21. data/lib/coderay/helpers/file_type.rb +119 -240
  22. data/lib/coderay/helpers/gzip.rb +41 -0
  23. data/lib/coderay/helpers/plugin.rb +237 -307
  24. data/lib/coderay/scanner.rb +112 -88
  25. data/lib/coderay/scanners/_map.rb +3 -3
  26. data/lib/coderay/scanners/c.rb +7 -7
  27. data/lib/coderay/scanners/clojure.rb +204 -0
  28. data/lib/coderay/scanners/css.rb +10 -20
  29. data/lib/coderay/scanners/debug.rb +9 -55
  30. data/lib/coderay/scanners/diff.rb +21 -4
  31. data/lib/coderay/scanners/html.rb +65 -18
  32. data/lib/coderay/scanners/java.rb +3 -2
  33. data/lib/coderay/scanners/java_script.rb +3 -3
  34. data/lib/coderay/scanners/json.rb +7 -6
  35. data/lib/coderay/scanners/php.rb +2 -1
  36. data/lib/coderay/scanners/rhtml.rb +6 -2
  37. data/lib/coderay/scanners/ruby.rb +193 -193
  38. data/lib/coderay/scanners/ruby/patterns.rb +15 -82
  39. data/lib/coderay/scanners/ruby/string_state.rb +71 -0
  40. data/lib/coderay/scanners/sql.rb +1 -1
  41. data/lib/coderay/scanners/yaml.rb +4 -2
  42. data/lib/coderay/styles/_map.rb +2 -2
  43. data/lib/coderay/styles/alpha.rb +48 -38
  44. data/lib/coderay/styles/cycnus.rb +2 -1
  45. data/lib/coderay/token_kinds.rb +88 -86
  46. data/lib/coderay/tokens.rb +88 -112
  47. data/test/functional/basic.rb +184 -5
  48. data/test/functional/examples.rb +4 -4
  49. data/test/functional/for_redcloth.rb +3 -2
  50. data/test/functional/suite.rb +7 -6
  51. metadata +11 -24
  52. data/lib/coderay/helpers/gzip_simple.rb +0 -123
  53. data/test/functional/load_plugin_scanner.rb +0 -11
  54. data/test/functional/vhdl.rb +0 -126
  55. data/test/functional/word_list.rb +0 -79
@@ -1,5 +1,8 @@
1
1
  module CodeRay
2
-
2
+
3
+ # GZip library for writing and reading token dumps.
4
+ autoload :GZip, 'coderay/helpers/gzip'
5
+
3
6
  # = Tokens TODO: Rewrite!
4
7
  #
5
8
  # The Tokens class represents a list of tokens returnd from
@@ -62,21 +65,21 @@ module CodeRay
62
65
  # options are passed to the encoder.
63
66
  def encode encoder, options = {}
64
67
  unless encoder.is_a? Encoders::Encoder
65
- unless encoder.is_a? Class
68
+ if encoder.respond_to? :to_sym
66
69
  encoder_class = Encoders[encoder]
67
70
  end
68
71
  encoder = encoder_class.new options
69
72
  end
70
73
  encoder.encode_tokens self, options
71
74
  end
72
-
75
+
73
76
  # Turn into a string using Encoders::Text.
74
77
  #
75
78
  # +options+ are passed to the encoder if given.
76
- def to_s options = {}
77
- encode :text, options
79
+ def to_s
80
+ encode CodeRay::Encoders::Encoder.new
78
81
  end
79
-
82
+
80
83
  # Redirects unknown methods to encoder calls.
81
84
  #
82
85
  # For example, if you call +tokens.html+, the HTML encoder
@@ -104,27 +107,27 @@ module CodeRay
104
107
  # joined in one comment token by the Scanner.
105
108
  def optimize
106
109
  raise NotImplementedError, 'Tokens#optimize needs to be rewritten.'
107
- last_kind = last_text = nil
108
- new = self.class.new
109
- for text, kind in self
110
- if text.is_a? String
111
- if kind == last_kind
112
- last_text << text
113
- else
114
- new << [last_text, last_kind] if last_kind
115
- last_text = text
116
- last_kind = kind
117
- end
118
- else
119
- new << [last_text, last_kind] if last_kind
120
- last_kind = last_text = nil
121
- new << [text, kind]
122
- end
123
- end
124
- new << [last_text, last_kind] if last_kind
125
- new
110
+ # last_kind = last_text = nil
111
+ # new = self.class.new
112
+ # for text, kind in self
113
+ # if text.is_a? String
114
+ # if kind == last_kind
115
+ # last_text << text
116
+ # else
117
+ # new << [last_text, last_kind] if last_kind
118
+ # last_text = text
119
+ # last_kind = kind
120
+ # end
121
+ # else
122
+ # new << [last_text, last_kind] if last_kind
123
+ # last_kind = last_text = nil
124
+ # new << [text, kind]
125
+ # end
126
+ # end
127
+ # new << [last_text, last_kind] if last_kind
128
+ # new
126
129
  end
127
-
130
+
128
131
  # Compact the object itself; see optimize.
129
132
  def optimize!
130
133
  replace optimize
@@ -135,30 +138,30 @@ module CodeRay
135
138
  # TODO: Test this!
136
139
  def fix
137
140
  raise NotImplementedError, 'Tokens#fix needs to be rewritten.'
138
- tokens = self.class.new
139
- # Check token nesting using a stack of kinds.
140
- opened = []
141
- for type, kind in self
142
- case type
143
- when :begin_group
144
- opened.push [:begin_group, kind]
145
- when :begin_line
146
- opened.push [:end_line, kind]
147
- when :end_group, :end_line
148
- expected = opened.pop
149
- if [type, kind] != expected
150
- # Unexpected end; decide what to do based on the kind:
151
- # - token was never opened: delete the end (just skip it)
152
- next unless opened.rindex expected
153
- # - token was opened earlier: also close tokens in between
154
- tokens << token until (token = opened.pop) == expected
155
- end
156
- end
157
- tokens << [type, kind]
158
- end
159
- # Close remaining opened tokens
160
- tokens << token while token = opened.pop
161
- tokens
141
+ # tokens = self.class.new
142
+ # # Check token nesting using a stack of kinds.
143
+ # opened = []
144
+ # for type, kind in self
145
+ # case type
146
+ # when :begin_group
147
+ # opened.push [:begin_group, kind]
148
+ # when :begin_line
149
+ # opened.push [:end_line, kind]
150
+ # when :end_group, :end_line
151
+ # expected = opened.pop
152
+ # if [type, kind] != expected
153
+ # # Unexpected end; decide what to do based on the kind:
154
+ # # - token was never opened: delete the end (just skip it)
155
+ # next unless opened.rindex expected
156
+ # # - token was opened earlier: also close tokens in between
157
+ # tokens << token until (token = opened.pop) == expected
158
+ # end
159
+ # end
160
+ # tokens << [type, kind]
161
+ # end
162
+ # # Close remaining opened tokens
163
+ # tokens << token while token = opened.pop
164
+ # tokens
162
165
  end
163
166
 
164
167
  def fix!
@@ -177,7 +180,7 @@ module CodeRay
177
180
  def split_into_lines
178
181
  raise NotImplementedError
179
182
  end
180
-
183
+
181
184
  def split_into_lines!
182
185
  replace split_into_lines
183
186
  end
@@ -239,19 +242,19 @@ module CodeRay
239
242
  when :end_group, :end_line
240
243
  opened.pop
241
244
  else
242
- raise 'Unknown token action: %p, kind = %p' % [content, item]
245
+ raise ArgumentError, 'Unknown token action: %p, kind = %p' % [content, item]
243
246
  end
244
247
  part << content << item
245
248
  content = nil
246
249
  else
247
- raise 'else case reached'
250
+ raise ArgumentError, 'Token input junk: %p, kind = %p' % [content, item]
248
251
  end
249
252
  end
250
253
  parts << part
251
254
  parts << Tokens.new while parts.size < sizes.size
252
255
  parts
253
256
  end
254
-
257
+
255
258
  # Dumps the object into a String that can be saved
256
259
  # in files or databases.
257
260
  #
@@ -268,9 +271,8 @@ module CodeRay
268
271
  #
269
272
  # See GZip module.
270
273
  def dump gzip_level = 7
271
- require 'coderay/helpers/gzip_simple'
272
274
  dump = Marshal.dump self
273
- dump = dump.gzip gzip_level
275
+ dump = GZip.gzip dump, gzip_level
274
276
  dump.extend Undumping
275
277
  end
276
278
 
@@ -278,7 +280,7 @@ module CodeRay
278
280
  def count
279
281
  size / 2
280
282
  end
281
-
283
+
282
284
  # Include this module to give an object an #undump
283
285
  # method.
284
286
  #
@@ -289,70 +291,44 @@ module CodeRay
289
291
  Tokens.load self
290
292
  end
291
293
  end
292
-
294
+
293
295
  # Undump the object using Marshal.load, then
294
296
  # unzip it using GZip.gunzip.
295
297
  #
296
298
  # The result is commonly a Tokens object, but
297
299
  # this is not guaranteed.
298
300
  def Tokens.load dump
299
- require 'coderay/helpers/gzip_simple'
300
- dump = dump.gunzip
301
+ dump = GZip.gunzip dump
301
302
  @dump = Marshal.load dump
302
303
  end
303
-
304
- alias text_token push
305
- def begin_group kind; push :begin_group, kind end
306
- def end_group kind; push :end_group, kind end
307
- def begin_line kind; push :begin_line, kind end
308
- def end_line kind; push :end_line, kind end
309
- alias tokens concat
310
304
 
311
- end
312
-
313
- end
314
-
315
- if $0 == __FILE__
316
- $VERBOSE = true
317
- $: << File.join(File.dirname(__FILE__), '..')
318
- eval DATA.read, nil, $0, __LINE__ + 4
319
- end
320
-
321
- __END__
322
- require 'test/unit'
323
-
324
- class TokensTest < Test::Unit::TestCase
325
-
326
- def test_creation
327
- assert CodeRay::Tokens < Array
328
- tokens = nil
329
- assert_nothing_raised do
330
- tokens = CodeRay::Tokens.new
331
- end
332
- assert_kind_of Array, tokens
333
- end
334
-
335
- def test_adding_tokens
336
- tokens = CodeRay::Tokens.new
337
- assert_nothing_raised do
338
- tokens.text_token 'string', :type
339
- tokens.text_token '()', :operator
340
- end
341
- assert_equal tokens.size, 4
342
- assert_equal tokens.count, 2
343
- end
344
-
345
- def test_dump_undump
346
- tokens = CodeRay::Tokens.new
347
- assert_nothing_raised do
348
- tokens.text_token 'string', :type
349
- tokens.text_token '()', :operator
350
- end
351
- tokens2 = nil
352
- assert_nothing_raised do
353
- tokens2 = tokens.dump.undump
305
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE['rbx']
306
+ #:nocov:
307
+ def text_token text, kind
308
+ self << text << kind
309
+ end
310
+ def begin_group kind
311
+ self << :begin_group << kind
312
+ end
313
+ def end_group kind
314
+ self << :end_group << kind
315
+ end
316
+ def begin_line kind
317
+ self << :begin_line << kind
318
+ end
319
+ def end_line kind
320
+ self << :end_line << kind
321
+ end
322
+ #:nocov:
323
+ else
324
+ alias text_token push
325
+ def begin_group kind; push :begin_group, kind end
326
+ def end_group kind; push :end_group, kind end
327
+ def begin_line kind; push :begin_line, kind end
328
+ def end_line kind; push :end_line, kind end
354
329
  end
355
- assert_equal tokens, tokens2
330
+ alias tokens concat
331
+
356
332
  end
357
333
 
358
- end
334
+ end
@@ -1,8 +1,21 @@
1
+ # encoding: utf-8
1
2
  require 'test/unit'
2
3
  require 'coderay'
3
4
 
4
5
  class BasicTest < Test::Unit::TestCase
5
6
 
7
+ def assert_warning expected_warning
8
+ require 'stringio'
9
+ oldstderr = $stderr
10
+ $stderr = StringIO.new
11
+ yield
12
+ $stderr.rewind
13
+ given_warning = $stderr.read.chomp
14
+ assert_equal expected_warning, given_warning
15
+ ensure
16
+ $stderr = oldstderr
17
+ end
18
+
6
19
  def test_version
7
20
  assert_nothing_raised do
8
21
  assert_match(/\A\d\.\d\.\d\z/, CodeRay::VERSION)
@@ -34,16 +47,40 @@ class BasicTest < Test::Unit::TestCase
34
47
  end
35
48
  end
36
49
 
50
+ def test_scan_file
51
+ CodeRay.scan_file __FILE__
52
+ end
53
+
54
+ def test_encode
55
+ assert_equal 1, CodeRay.encode('test', :python, :count)
56
+ end
57
+
58
+ def test_encode_tokens
59
+ assert_equal 1, CodeRay.encode_tokens(CodeRay::Tokens['test', :string], :count)
60
+ end
61
+
62
+ def test_encode_file
63
+ assert_equal File.read(__FILE__), CodeRay.encode_file(__FILE__, :text)
64
+ end
65
+
66
+ def test_highlight
67
+ assert_match '<div class="code"><pre>test</pre></div>', CodeRay.highlight('test', :python)
68
+ end
69
+
70
+ def test_highlight_file
71
+ assert_match "require <span class=\"s\"><span class=\"dl\">'</span><span class=\"k\">test/unit</span><span class=\"dl\">'</span></span>\n", CodeRay.highlight_file(__FILE__)
72
+ end
73
+
37
74
  def test_duo
38
75
  assert_equal(RUBY_TEST_CODE,
39
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE))
76
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE))
40
77
  assert_equal(RUBY_TEST_CODE,
41
- CodeRay::Duo[:plain => :plain].highlight(RUBY_TEST_CODE))
78
+ CodeRay::Duo[:plain => :text].highlight(RUBY_TEST_CODE))
42
79
  end
43
80
 
44
81
  def test_duo_stream
45
82
  assert_equal(RUBY_TEST_CODE,
46
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
83
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE, :stream => true))
47
84
  end
48
85
 
49
86
  def test_comment_filter
@@ -107,12 +144,154 @@ more code # and another comment, in-line.
107
144
 
108
145
  def test_list_of_encoders
109
146
  assert_kind_of(Array, CodeRay::Encoders.list)
110
- assert CodeRay::Encoders.list.include?('count')
147
+ assert CodeRay::Encoders.list.include?(:count)
111
148
  end
112
149
 
113
150
  def test_list_of_scanners
114
151
  assert_kind_of(Array, CodeRay::Scanners.list)
115
- assert CodeRay::Scanners.list.include?('plaintext')
152
+ assert CodeRay::Scanners.list.include?(:plaintext)
153
+ end
154
+
155
+ def test_token_kinds
156
+ assert_kind_of Hash, CodeRay::TokenKinds
157
+ for kind, css_class in CodeRay::TokenKinds
158
+ assert_kind_of Symbol, kind
159
+ if css_class != false
160
+ assert_kind_of String, css_class, "TokenKinds[%p] == %p" % [kind, css_class]
161
+ end
162
+ end
163
+ assert_equal 'r', CodeRay::TokenKinds[:reserved]
164
+ assert_equal false, CodeRay::TokenKinds[:shibboleet]
165
+ end
166
+
167
+ class Milk < CodeRay::Encoders::Encoder
168
+ FILE_EXTENSION = 'cocoa'
169
+ end
170
+
171
+ class HoneyBee < CodeRay::Encoders::Encoder
172
+ end
173
+
174
+ def test_encoder_file_extension
175
+ assert_nothing_raised do
176
+ assert_equal 'html', CodeRay::Encoders::HTML::FILE_EXTENSION
177
+ assert_equal 'cocoa', Milk::FILE_EXTENSION
178
+ assert_equal 'cocoa', Milk.new.file_extension
179
+ assert_equal 'honeybee', HoneyBee::FILE_EXTENSION
180
+ assert_equal 'honeybee', HoneyBee.new.file_extension
181
+ end
182
+ assert_raise NameError do
183
+ HoneyBee::MISSING_CONSTANT
184
+ end
185
+ end
186
+
187
+ def test_encoder_tokens
188
+ encoder = CodeRay::Encoders::Encoder.new
189
+ encoder.send :setup, {}
190
+ assert_raise(ArgumentError) { encoder.token :strange, '' }
191
+ encoder.token 'test', :debug
192
+ end
193
+
194
+ def test_encoder_deprecated_interface
195
+ encoder = CodeRay::Encoders::Encoder.new
196
+ encoder.send :setup, {}
197
+ assert_warning 'Using old Tokens#<< interface.' do
198
+ encoder << ['test', :content]
199
+ end
200
+ assert_raise ArgumentError do
201
+ encoder << [:strange, :input]
202
+ end
203
+ assert_raise ArgumentError do
204
+ encoder.encode_tokens [['test', :token]]
205
+ end
206
+ end
207
+
208
+ def encoder_token_interface_deprecation_warning_given
209
+ CodeRay::Encoders::Encoder.send :class_variable_get, :@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
210
+ end
211
+
212
+ def test_scanner_file_extension
213
+ assert_equal 'rb', CodeRay::Scanners::Ruby.file_extension
214
+ assert_equal 'rb', CodeRay::Scanners::Ruby.new.file_extension
215
+ assert_equal 'java', CodeRay::Scanners::Java.file_extension
216
+ assert_equal 'java', CodeRay::Scanners::Java.new.file_extension
217
+ end
218
+
219
+ def test_scanner_lang
220
+ assert_equal :ruby, CodeRay::Scanners::Ruby.lang
221
+ assert_equal :ruby, CodeRay::Scanners::Ruby.new.lang
222
+ assert_equal :java, CodeRay::Scanners::Java.lang
223
+ assert_equal :java, CodeRay::Scanners::Java.new.lang
224
+ end
225
+
226
+ def test_scanner_tokenize
227
+ assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo')
228
+ assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar'])
229
+ assert_raise ArgumentError do
230
+ CodeRay::Scanners::Plain.new.tokenize 42
231
+ end
232
+ end
233
+
234
+ def test_scanner_tokens
235
+ scanner = CodeRay::Scanners::Plain.new
236
+ scanner.tokenize('foo')
237
+ assert_equal ['foo', :plain], scanner.tokens
238
+ scanner.string = ''
239
+ assert_equal ['', :plain], scanner.tokens
240
+ end
241
+
242
+ def test_scanner_line_and_column
243
+ scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux"
244
+ assert_equal 0, scanner.pos
245
+ assert_equal 1, scanner.line
246
+ assert_equal 0, scanner.column
247
+ scanner.scan(/foo\nbär/)
248
+ assert_equal 8, scanner.pos
249
+ assert_equal 2, scanner.line
250
+ assert_equal 5, scanner.column
251
+ end
252
+
253
+ def test_scanner_use_subclasses
254
+ assert_raise NotImplementedError do
255
+ CodeRay::Scanners::Scanner.new
256
+ end
257
+ end
258
+
259
+ class InvalidScanner < CodeRay::Scanners::Scanner
260
+ end
261
+
262
+ def test_scanner_scan_tokens
263
+ assert_raise NotImplementedError do
264
+ InvalidScanner.new.tokenize ''
265
+ end
266
+ end
267
+
268
+ class RaisingScanner < CodeRay::Scanners::Scanner
269
+ def scan_tokens encoder, options
270
+ raise_inspect 'message', [], :initial
271
+ end
272
+ end
273
+
274
+ def test_scanner_raise_inspect
275
+ assert_raise CodeRay::Scanners::Scanner::ScanError do
276
+ RaisingScanner.new.tokenize ''
277
+ end
278
+ end
279
+
280
+ def test_scan_a_frozen_string
281
+ assert_nothing_raised do
282
+ CodeRay.scan RUBY_VERSION, :ruby
283
+ CodeRay.scan RUBY_VERSION, :plain
284
+ end
285
+ end
286
+
287
+ def test_scan_a_non_string
288
+ assert_nothing_raised do
289
+ CodeRay.scan 42, :ruby
290
+ CodeRay.scan nil, :ruby
291
+ CodeRay.scan self, :ruby
292
+ CodeRay.encode ENV.to_hash, :ruby, :page
293
+ CodeRay.highlight CodeRay, :plain
294
+ end
116
295
  end
117
296
 
118
297
  end