coderay 1.0.0.598.pre → 1.0.0.738.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -93,11 +93,10 @@ module CodeRay
93
93
  TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
94
94
  TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
95
95
  TOKEN_COLORS[:begin_group] = TOKEN_COLORS[:end_group] =
96
- TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] =
97
- TOKEN_COLORS[:delimiter]
98
-
96
+ TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
97
+
99
98
  protected
100
-
99
+
101
100
  def setup(options)
102
101
  super
103
102
  @opened = []
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -24,7 +23,7 @@ module Encoders
24
23
  }
25
24
 
26
25
  def text_token text, kind
27
- @out << text
26
+ super
28
27
  @out << @sep if @sep
29
28
  end
30
29
 
@@ -42,24 +41,3 @@ module Encoders
42
41
 
43
42
  end
44
43
  end
45
-
46
- if $0 == __FILE__
47
- $VERBOSE = true
48
- $: << File.join(File.dirname(__FILE__), '..')
49
- eval DATA.read, nil, $0, __LINE__ + 4
50
- end
51
-
52
- __END__
53
- require 'test/unit'
54
-
55
- class CountTest < Test::Unit::TestCase
56
-
57
- def test_count
58
- ruby = <<-RUBY
59
- puts "Hello world!"
60
- RUBY
61
- tokens = CodeRay.scan ruby, :ruby
62
- assert_equal ruby, tokens.encode_with(:text)
63
- end
64
-
65
- end
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -109,60 +108,3 @@ module Encoders
109
108
 
110
109
  end
111
110
  end
112
-
113
- if $0 == __FILE__
114
- $VERBOSE = true
115
- $: << File.join(File.dirname(__FILE__), '..')
116
- eval DATA.read, nil, $0, __LINE__ + 4
117
- end
118
-
119
- __END__
120
- require 'test/unit'
121
-
122
- class TokenKindFilterTest < Test::Unit::TestCase
123
-
124
- def test_creation
125
- assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Encoder
126
- assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Filter
127
- filter = nil
128
- assert_nothing_raised do
129
- filter = CodeRay.encoder :token_kind_filter
130
- end
131
- assert_instance_of CodeRay::Encoders::TokenKindFilter, filter
132
- end
133
-
134
- def test_filtering_text_tokens
135
- tokens = CodeRay::Tokens.new
136
- for i in 1..10
137
- tokens.text_token i.to_s, :index
138
- tokens.text_token ' ', :space if i < 10
139
- end
140
- assert_equal 10, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :space).count
141
- assert_equal 10, tokens.token_kind_filter(:exclude => :space).count
142
- assert_equal 9, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :space).count
143
- assert_equal 9, tokens.token_kind_filter(:include => :space).count
144
- assert_equal 0, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :all).count
145
- assert_equal 0, tokens.token_kind_filter(:exclude => :all).count
146
- end
147
-
148
- def test_filtering_block_tokens
149
- tokens = CodeRay::Tokens.new
150
- 10.times do |i|
151
- tokens.begin_group :index
152
- tokens.text_token i.to_s, :content
153
- tokens.end_group :index
154
- tokens.begin_group :naught if i == 5
155
- tokens.end_group :naught if i == 7
156
- tokens.begin_line :blubb
157
- tokens.text_token i.to_s, :content
158
- tokens.end_line :blubb
159
- end
160
- assert_equal 16, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :blubb).count
161
- assert_equal 16, tokens.token_kind_filter(:include => :blubb).count
162
- assert_equal 24, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => [:blubb, :content]).count
163
- assert_equal 24, tokens.token_kind_filter(:include => [:blubb, :content]).count
164
- assert_equal 32, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :index).count
165
- assert_equal 32, tokens.token_kind_filter(:exclude => :index).count
166
- end
167
-
168
- end
@@ -1,70 +1,70 @@
1
- #!/usr/bin/env ruby
2
1
  module CodeRay
3
-
4
- # = FileType
5
- #
6
- # A simple filetype recognizer.
7
- #
8
- # == Usage
9
- #
10
- # # determine the type of the given
11
- # lang = FileType[ARGV.first]
12
- #
13
- # # return :plaintext if the file type is unknown
14
- # lang = FileType.fetch ARGV.first, :plaintext
15
- #
16
- # # try the shebang line, too
17
- # lang = FileType.fetch ARGV.first, :plaintext, true
18
- module FileType
19
-
20
- UnknownFileType = Class.new Exception
21
-
22
- class << self
23
-
24
- # Try to determine the file type of the file.
25
- #
26
- # +filename+ is a relative or absolute path to a file.
27
- #
28
- # The file itself is only accessed when +read_shebang+ is set to true.
29
- # That means you can get filetypes from files that don't exist.
30
- def [] filename, read_shebang = false
31
- name = File.basename filename
32
- ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
33
- ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
34
-
35
- type =
36
- TypeFromExt[ext] ||
37
- TypeFromExt[ext.downcase] ||
38
- (TypeFromExt[ext2] if ext2) ||
39
- (TypeFromExt[ext2.downcase] if ext2) ||
40
- TypeFromName[name] ||
41
- TypeFromName[name.downcase]
42
- type ||= shebang(filename) if read_shebang
43
-
44
- type
45
- end
46
-
47
- # This works like Hash#fetch.
48
- #
49
- # If the filetype cannot be found, the +default+ value
50
- # is returned.
51
- def fetch filename, default = nil, read_shebang = false
52
- if default and block_given?
53
- warn 'block supersedes default value argument'
54
- end
55
-
56
- unless type = self[filename, read_shebang]
57
- return yield if block_given?
58
- return default if default
59
- raise UnknownFileType, 'Could not determine type of %p.' % filename
60
- end
61
- type
62
- end
2
+
3
+ # = FileType
4
+ #
5
+ # A simple filetype recognizer.
6
+ #
7
+ # == Usage
8
+ #
9
+ # # determine the type of the given
10
+ # lang = FileType[ARGV.first]
11
+ #
12
+ # # return :plaintext if the file type is unknown
13
+ # lang = FileType.fetch ARGV.first, :plaintext
14
+ #
15
+ # # try the shebang line, too
16
+ # lang = FileType.fetch ARGV.first, :plaintext, true
17
+ module FileType
63
18
 
64
- protected
19
+ UnknownFileType = Class.new Exception
65
20
 
66
- def shebang filename
67
- begin
21
+ class << self
22
+
23
+ # Try to determine the file type of the file.
24
+ #
25
+ # +filename+ is a relative or absolute path to a file.
26
+ #
27
+ # The file itself is only accessed when +read_shebang+ is set to true.
28
+ # That means you can get filetypes from files that don't exist.
29
+ def [] filename, read_shebang = false
30
+ name = File.basename filename
31
+ ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
32
+ ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
33
+
34
+ type =
35
+ TypeFromExt[ext] ||
36
+ TypeFromExt[ext.downcase] ||
37
+ (TypeFromExt[ext2] if ext2) ||
38
+ (TypeFromExt[ext2.downcase] if ext2) ||
39
+ TypeFromName[name] ||
40
+ TypeFromName[name.downcase]
41
+ type ||= shebang(filename) if read_shebang
42
+
43
+ type
44
+ end
45
+
46
+ # This works like Hash#fetch.
47
+ #
48
+ # If the filetype cannot be found, the +default+ value
49
+ # is returned.
50
+ def fetch filename, default = nil, read_shebang = false
51
+ if default && block_given?
52
+ warn 'Block supersedes default value argument; use either.'
53
+ end
54
+
55
+ if type = self[filename, read_shebang]
56
+ type
57
+ else
58
+ return yield if block_given?
59
+ return default if default
60
+ raise UnknownFileType, 'Could not determine type of %p.' % filename
61
+ end
62
+ end
63
+
64
+ protected
65
+
66
+ def shebang filename
67
+ return unless File.exist? filename
68
68
  File.open filename, 'r' do |f|
69
69
  if first_line = f.gets
70
70
  if type = first_line[TypeFromShebang]
@@ -72,185 +72,64 @@ module FileType
72
72
  end
73
73
  end
74
74
  end
75
- rescue IOError
76
- nil
77
75
  end
76
+
78
77
  end
79
-
80
- end
81
-
82
- TypeFromExt = {
83
- 'c' => :c,
84
- 'css' => :css,
85
- 'diff' => :diff,
86
- 'dpr' => :delphi,
87
- 'gemspec' => :ruby,
88
- 'groovy' => :groovy,
89
- 'gvy' => :groovy,
90
- 'h' => :c,
91
- 'htm' => :html,
92
- 'html' => :html,
93
- 'html.erb' => :rhtml,
94
- 'java' => :java,
95
- 'js' => :java_script,
96
- 'json' => :json,
97
- 'mab' => :ruby,
98
- 'pas' => :delphi,
99
- 'patch' => :diff,
100
- 'php' => :php,
101
- 'php3' => :php,
102
- 'php4' => :php,
103
- 'php5' => :php,
104
- 'py' => :python,
105
- 'py3' => :python,
106
- 'pyw' => :python,
107
- 'rake' => :ruby,
108
- 'raydebug' => :raydebug,
109
- 'rb' => :ruby,
110
- 'rbw' => :ruby,
111
- 'rhtml' => :rhtml,
112
- 'rjs' => :ruby,
113
- 'rpdf' => :ruby,
114
- 'rxml' => :ruby,
115
- 'sch' => :scheme,
116
- 'sql' => :sql,
117
- 'ss' => :scheme,
118
- 'xhtml' => :xhtml,
119
- 'xml' => :xml,
120
- 'yaml' => :yaml,
121
- 'yml' => :yaml,
122
- }
123
- for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
124
- TypeFromExt[cpp_alias] = :cpp
125
- end
126
-
127
- TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
128
-
129
- TypeFromName = {
130
- 'Capfile' => :ruby,
131
- 'Rakefile' => :ruby,
132
- 'Rantfile' => :ruby,
133
- }
134
-
135
- end
136
-
137
- end
138
-
139
- if $0 == __FILE__
140
- $VERBOSE = true
141
- eval DATA.read, nil, $0, __LINE__ + 4
142
- end
143
-
144
- __END__
145
- require 'test/unit'
146
-
147
- class FileTypeTests < Test::Unit::TestCase
148
-
149
- include CodeRay
150
-
151
- def test_fetch
152
- assert_raise FileType::UnknownFileType do
153
- FileType.fetch ''
154
- end
155
-
156
- assert_throws :not_found do
157
- FileType.fetch '.' do
158
- throw :not_found
159
- end
160
- end
161
-
162
- assert_equal :default, FileType.fetch('c', :default)
163
-
164
- stderr, fake_stderr = $stderr, Object.new
165
- $err = ''
166
- def fake_stderr.write x
167
- $err << x
168
- end
169
- $stderr = fake_stderr
170
- FileType.fetch('c', :default) { }
171
- assert_equal "block supersedes default value argument\n", $err
172
- $stderr = stderr
173
- end
174
-
175
- def test_ruby
176
- assert_equal :ruby, FileType['test.rb']
177
- assert_equal :ruby, FileType['test.java.rb']
178
- assert_equal :java, FileType['test.rb.java']
179
- assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
180
- assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
181
- assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
182
- assert_equal :ruby, FileType['./lib/tasks\repository.rake']
183
- assert_not_equal :ruby, FileType['test_rb']
184
- assert_not_equal :ruby, FileType['Makefile']
185
- assert_not_equal :ruby, FileType['set.rb/set']
186
- assert_not_equal :ruby, FileType['~/projects/blabla/rb']
187
- end
188
-
189
- def test_c
190
- assert_equal :c, FileType['test.c']
191
- assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
192
- assert_not_equal :c, FileType['test_c']
193
- assert_not_equal :c, FileType['Makefile']
194
- assert_not_equal :c, FileType['set.h/set']
195
- assert_not_equal :c, FileType['~/projects/blabla/c']
196
- end
197
-
198
- def test_cpp
199
- assert_equal :cpp, FileType['test.c++']
200
- assert_equal :cpp, FileType['test.cxx']
201
- assert_equal :cpp, FileType['test.hh']
202
- assert_equal :cpp, FileType['test.hpp']
203
- assert_equal :cpp, FileType['test.cu']
204
- assert_equal :cpp, FileType['test.C']
205
- assert_not_equal :cpp, FileType['test.c']
206
- assert_not_equal :cpp, FileType['test.h']
207
- end
208
-
209
- def test_html
210
- assert_equal :html, FileType['test.htm']
211
- assert_equal :xhtml, FileType['test.xhtml']
212
- assert_equal :xhtml, FileType['test.html.xhtml']
213
- assert_equal :rhtml, FileType['_form.rhtml']
214
- assert_equal :rhtml, FileType['_form.html.erb']
215
- end
216
-
217
- def test_yaml
218
- assert_equal :yaml, FileType['test.yml']
219
- assert_equal :yaml, FileType['test.yaml']
220
- assert_equal :yaml, FileType['my.html.yaml']
221
- assert_not_equal :yaml, FileType['YAML']
222
- end
223
-
224
- def test_pathname
225
- require 'pathname'
226
- pn = Pathname.new 'test.rb'
227
- assert_equal :ruby, FileType[pn]
228
- dir = Pathname.new '/etc/var/blubb'
229
- assert_equal :ruby, FileType[dir + pn]
230
- assert_equal :cpp, FileType[dir + 'test.cpp']
231
- end
232
-
233
- def test_no_shebang
234
- dir = './test'
235
- if File.directory? dir
236
- Dir.chdir dir do
237
- assert_equal :c, FileType['test.c']
238
- end
78
+
79
+ TypeFromExt = {
80
+ 'c' => :c,
81
+ 'clj' => :clojure,
82
+ 'css' => :css,
83
+ 'diff' => :diff,
84
+ 'dpr' => :delphi,
85
+ 'gemspec' => :ruby,
86
+ 'groovy' => :groovy,
87
+ 'gvy' => :groovy,
88
+ 'h' => :c,
89
+ 'htm' => :html,
90
+ 'html' => :html,
91
+ 'html.erb' => :rhtml,
92
+ 'java' => :java,
93
+ 'js' => :java_script,
94
+ 'json' => :json,
95
+ 'mab' => :ruby,
96
+ 'pas' => :delphi,
97
+ 'patch' => :diff,
98
+ 'php' => :php,
99
+ 'php3' => :php,
100
+ 'php4' => :php,
101
+ 'php5' => :php,
102
+ 'py' => :python,
103
+ 'py3' => :python,
104
+ 'pyw' => :python,
105
+ 'rake' => :ruby,
106
+ 'raydebug' => :raydebug,
107
+ 'rb' => :ruby,
108
+ 'rbw' => :ruby,
109
+ 'rhtml' => :rhtml,
110
+ 'rjs' => :ruby,
111
+ 'rpdf' => :ruby,
112
+ 'rxml' => :ruby,
113
+ 'sch' => :scheme,
114
+ 'sql' => :sql,
115
+ 'ss' => :scheme,
116
+ 'xhtml' => :xhtml,
117
+ 'xml' => :xml,
118
+ 'yaml' => :yaml,
119
+ 'yml' => :yaml,
120
+ }
121
+ for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
122
+ TypeFromExt[cpp_alias] = :cpp
239
123
  end
124
+
125
+ TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
126
+
127
+ TypeFromName = {
128
+ 'Capfile' => :ruby,
129
+ 'Rakefile' => :ruby,
130
+ 'Rantfile' => :ruby,
131
+ }
132
+
240
133
  end
241
134
 
242
- def test_shebang_empty_file
243
- require 'tmpdir'
244
- tmpfile = File.join(Dir.tmpdir, 'bla')
245
- File.open(tmpfile, 'w') { } # touch
246
- assert_equal nil, FileType[tmpfile]
247
- end
248
-
249
- def test_shebang
250
- require 'tmpdir'
251
- tmpfile = File.join(Dir.tmpdir, 'bla')
252
- File.open(tmpfile, 'w') { |f| f.puts '#!/usr/bin/env ruby' }
253
- assert_equal :ruby, FileType[tmpfile, true]
254
- end
255
-
256
135
  end