coderay 1.0.0 → 1.0.0.598.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/FOLDERS +49 -0
  2. data/Rakefile +6 -5
  3. data/bin/coderay +74 -190
  4. data/bin/coderay_stylesheet +4 -0
  5. data/{README_INDEX.rdoc → lib/README} +20 -10
  6. data/lib/coderay.rb +60 -62
  7. data/lib/coderay/duo.rb +55 -2
  8. data/lib/coderay/encoder.rb +39 -52
  9. data/lib/coderay/encoders/_map.rb +7 -11
  10. data/lib/coderay/encoders/comment_filter.rb +61 -0
  11. data/lib/coderay/encoders/count.rb +26 -11
  12. data/lib/coderay/encoders/debug.rb +60 -11
  13. data/lib/coderay/encoders/div.rb +8 -9
  14. data/lib/coderay/encoders/filter.rb +52 -12
  15. data/lib/coderay/encoders/html.rb +113 -106
  16. data/lib/coderay/encoders/html/css.rb +7 -2
  17. data/lib/coderay/encoders/html/numbering.rb +27 -24
  18. data/lib/coderay/encoders/html/output.rb +58 -15
  19. data/lib/coderay/encoders/json.rb +44 -37
  20. data/lib/coderay/encoders/lines_of_code.rb +56 -9
  21. data/lib/coderay/encoders/null.rb +13 -6
  22. data/lib/coderay/encoders/page.rb +8 -8
  23. data/lib/coderay/encoders/span.rb +9 -10
  24. data/lib/coderay/encoders/statistic.rb +114 -51
  25. data/lib/coderay/encoders/terminal.rb +10 -7
  26. data/lib/coderay/encoders/text.rb +36 -17
  27. data/lib/coderay/encoders/token_kind_filter.rb +58 -1
  28. data/lib/coderay/encoders/xml.rb +11 -13
  29. data/lib/coderay/encoders/yaml.rb +14 -16
  30. data/lib/coderay/for_redcloth.rb +1 -1
  31. data/lib/coderay/helpers/file_type.rb +240 -125
  32. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  33. data/lib/coderay/helpers/plugin.rb +307 -241
  34. data/lib/coderay/helpers/word_list.rb +126 -65
  35. data/lib/coderay/scanner.rb +103 -153
  36. data/lib/coderay/scanners/_map.rb +16 -18
  37. data/lib/coderay/scanners/c.rb +13 -13
  38. data/lib/coderay/scanners/cpp.rb +6 -6
  39. data/lib/coderay/scanners/css.rb +48 -47
  40. data/lib/coderay/scanners/debug.rb +55 -9
  41. data/lib/coderay/scanners/delphi.rb +4 -4
  42. data/lib/coderay/scanners/diff.rb +25 -43
  43. data/lib/coderay/scanners/groovy.rb +2 -2
  44. data/lib/coderay/scanners/html.rb +30 -107
  45. data/lib/coderay/scanners/java.rb +5 -6
  46. data/lib/coderay/scanners/java/builtin_types.rb +0 -2
  47. data/lib/coderay/scanners/java_script.rb +6 -6
  48. data/lib/coderay/scanners/json.rb +6 -7
  49. data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
  50. data/lib/coderay/scanners/php.rb +12 -13
  51. data/lib/coderay/scanners/plaintext.rb +26 -0
  52. data/lib/coderay/scanners/python.rb +4 -4
  53. data/lib/coderay/scanners/{erb.rb → rhtml.rb} +11 -19
  54. data/lib/coderay/scanners/ruby.rb +208 -219
  55. data/lib/coderay/scanners/ruby/patterns.rb +85 -18
  56. data/lib/coderay/scanners/scheme.rb +136 -0
  57. data/lib/coderay/scanners/sql.rb +22 -29
  58. data/lib/coderay/scanners/yaml.rb +10 -11
  59. data/lib/coderay/styles/_map.rb +2 -2
  60. data/lib/coderay/styles/alpha.rb +104 -102
  61. data/lib/coderay/styles/cycnus.rb +143 -0
  62. data/lib/coderay/styles/murphy.rb +123 -0
  63. data/lib/coderay/token_kinds.rb +86 -87
  64. data/lib/coderay/tokens.rb +169 -26
  65. data/test/functional/basic.rb +14 -200
  66. data/test/functional/examples.rb +14 -20
  67. data/test/functional/for_redcloth.rb +8 -15
  68. data/test/functional/load_plugin_scanner.rb +11 -0
  69. data/test/functional/suite.rb +6 -9
  70. data/test/functional/vhdl.rb +126 -0
  71. data/test/functional/word_list.rb +79 -0
  72. metadata +129 -107
  73. data/lib/coderay/helpers/gzip.rb +0 -41
  74. data/lib/coderay/scanners/clojure.rb +0 -217
  75. data/lib/coderay/scanners/haml.rb +0 -168
  76. data/lib/coderay/scanners/ruby/string_state.rb +0 -71
  77. data/lib/coderay/scanners/text.rb +0 -26
  78. data/lib/coderay/tokens_proxy.rb +0 -55
  79. data/lib/coderay/version.rb +0 -3
@@ -1,40 +1,38 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  # = XML Encoder
5
5
  #
6
6
  # Uses REXML. Very slow.
7
7
  class XML < Encoder
8
-
8
+
9
9
  register_for :xml
10
-
10
+
11
11
  FILE_EXTENSION = 'xml'
12
-
13
- autoload :REXML, 'rexml/document'
14
-
12
+
13
+ require 'rexml/document'
14
+
15
15
  DEFAULT_OPTIONS = {
16
16
  :tab_width => 8,
17
17
  :pretty => -1,
18
18
  :transitive => false,
19
19
  }
20
-
20
+
21
21
  protected
22
+
22
23
  def setup options
23
- super
24
-
25
24
  @doc = REXML::Document.new
26
25
  @doc << REXML::XMLDecl.new
27
26
  @tab_width = options[:tab_width]
28
27
  @root = @node = @doc.add_element('coderay-tokens')
29
28
  end
30
-
29
+
31
30
  def finish options
31
+ @out = ''
32
32
  @doc.write @out, options[:pretty], options[:transitive], true
33
-
34
- super
33
+ @out
35
34
  end
36
35
 
37
- public
38
36
  def text_token text, kind
39
37
  if kind == :space
40
38
  token = @node
@@ -1,50 +1,48 @@
1
- autoload :YAML, 'yaml'
2
-
3
1
  module CodeRay
4
2
  module Encoders
5
-
3
+
6
4
  # = YAML Encoder
7
5
  #
8
6
  # Slow.
9
7
  class YAML < Encoder
10
-
8
+
11
9
  register_for :yaml
12
-
10
+
13
11
  FILE_EXTENSION = 'yaml'
14
-
12
+
15
13
  protected
16
14
  def setup options
17
- super
18
-
19
- @data = []
15
+ require 'yaml'
16
+ @out = []
20
17
  end
21
18
 
22
19
  def finish options
23
- output ::YAML.dump(@data)
20
+ @out.to_a.to_yaml
24
21
  end
25
22
 
26
23
  public
24
+
27
25
  def text_token text, kind
28
- @data << [text, kind]
26
+ @out << [text, kind]
29
27
  end
30
28
 
31
29
  def begin_group kind
32
- @data << [:begin_group, kind]
30
+ @out << [:begin_group, kind]
33
31
  end
34
32
 
35
33
  def end_group kind
36
- @data << [:end_group, kind]
34
+ @out << [:end_group, kind]
37
35
  end
38
36
 
39
37
  def begin_line kind
40
- @data << [:begin_line, kind]
38
+ @out << [:begin_line, kind]
41
39
  end
42
40
 
43
41
  def end_line kind
44
- @data << [:end_line, kind]
42
+ @out << [:end_line, kind]
45
43
  end
46
44
 
47
45
  end
48
-
46
+
49
47
  end
50
48
  end
@@ -45,7 +45,7 @@ module CodeRay
45
45
  if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
46
46
  # simulating pre-4.2 behavior
47
47
  if opts[:text].sub!(/\A\[(\w+)\]/, '')
48
- if CodeRay::Scanners[$1].lang == :text
48
+ if CodeRay::Scanners[$1].plugin_id == :plaintext
49
49
  opts[:text] = $& + opts[:text]
50
50
  else
51
51
  opts[:lang] = $1
@@ -1,70 +1,70 @@
1
+ #!/usr/bin/env ruby
1
2
  module CodeRay
2
-
3
- # = FileType
4
- #
5
- # A simple filetype recognizer.
6
- #
7
- # == Usage
8
- #
9
- # # determine the type of the given
10
- # lang = FileType[file_name]
11
- #
12
- # # return :text if the file type is unknown
13
- # lang = FileType.fetch file_name, :text
14
- #
15
- # # try the shebang line, too
16
- # lang = FileType.fetch file_name, :text, true
17
- module FileType
18
-
19
- UnknownFileType = Class.new Exception
20
-
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
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'
44
54
  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
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
62
60
  end
63
-
64
- protected
65
-
66
- def shebang filename
67
- return unless File.exist? filename
61
+ type
62
+ end
63
+
64
+ protected
65
+
66
+ def shebang filename
67
+ begin
68
68
  File.open filename, 'r' do |f|
69
69
  if first_line = f.gets
70
70
  if type = first_line[TypeFromShebang]
@@ -72,70 +72,185 @@ module CodeRay
72
72
  end
73
73
  end
74
74
  end
75
+ rescue IOError
76
+ nil
75
77
  end
76
-
77
78
  end
78
-
79
- TypeFromExt = {
80
- 'c' => :c,
81
- 'cfc' => :xml,
82
- 'cfm' => :xml,
83
- 'clj' => :clojure,
84
- 'css' => :css,
85
- 'diff' => :diff,
86
- 'dpr' => :delphi,
87
- 'gemspec' => :ruby,
88
- 'groovy' => :groovy,
89
- 'gvy' => :groovy,
90
- 'h' => :c,
91
- 'haml' => :haml,
92
- 'htm' => :page,
93
- 'html' => :page,
94
- 'html.erb' => :erb,
95
- 'java' => :java,
96
- 'js' => :java_script,
97
- 'json' => :json,
98
- 'mab' => :ruby,
99
- 'pas' => :delphi,
100
- 'patch' => :diff,
101
- 'php' => :php,
102
- 'php3' => :php,
103
- 'php4' => :php,
104
- 'php5' => :php,
105
- 'prawn' => :ruby,
106
- 'py' => :python,
107
- 'py3' => :python,
108
- 'pyw' => :python,
109
- 'rake' => :ruby,
110
- 'raydebug' => :raydebug,
111
- 'rb' => :ruby,
112
- 'rbw' => :ruby,
113
- 'rhtml' => :erb,
114
- 'rjs' => :ruby,
115
- 'rpdf' => :ruby,
116
- 'ru' => :ruby,
117
- 'rxml' => :ruby,
118
- # 'sch' => :scheme,
119
- 'sql' => :sql,
120
- # 'ss' => :scheme,
121
- 'xhtml' => :page,
122
- 'xml' => :xml,
123
- 'yaml' => :yaml,
124
- 'yml' => :yaml,
125
- }
126
- for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
127
- TypeFromExt[cpp_alias] = :cpp
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 ''
128
154
  end
129
-
130
- TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
131
-
132
- TypeFromName = {
133
- 'Capfile' => :ruby,
134
- 'Rakefile' => :ruby,
135
- 'Rantfile' => :ruby,
136
- 'Gemfile' => :ruby,
137
- }
138
-
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
239
+ end
240
+ end
241
+
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]
139
247
  end
140
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
+
141
256
  end