coderay 0.7.1.147 → 0.7.2.165

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/bin/coderay +54 -56
  2. data/demo/suite.rb +54 -54
  3. data/lib/coderay.rb +187 -187
  4. data/lib/coderay/duo.rb +29 -29
  5. data/lib/coderay/encoder.rb +173 -173
  6. data/lib/coderay/encoders/_map.rb +8 -8
  7. data/lib/coderay/encoders/count.rb +21 -21
  8. data/lib/coderay/encoders/debug.rb +46 -46
  9. data/lib/coderay/encoders/div.rb +20 -20
  10. data/lib/coderay/encoders/html.rb +249 -245
  11. data/lib/coderay/encoders/html/classes.rb +73 -73
  12. data/lib/coderay/encoders/html/css.rb +65 -65
  13. data/lib/coderay/encoders/html/numerization.rb +122 -122
  14. data/lib/coderay/encoders/html/output.rb +195 -195
  15. data/lib/coderay/encoders/null.rb +26 -26
  16. data/lib/coderay/encoders/page.rb +21 -21
  17. data/lib/coderay/encoders/span.rb +20 -20
  18. data/lib/coderay/encoders/statistic.rb +81 -81
  19. data/lib/coderay/encoders/text.rb +33 -33
  20. data/lib/coderay/encoders/tokens.rb +44 -44
  21. data/lib/coderay/encoders/xml.rb +71 -71
  22. data/lib/coderay/encoders/yaml.rb +22 -22
  23. data/lib/coderay/helpers/filetype.rb +152 -153
  24. data/lib/coderay/helpers/gzip_simple.rb +67 -68
  25. data/lib/coderay/helpers/plugin.rb +297 -297
  26. data/lib/coderay/helpers/word_list.rb +46 -47
  27. data/lib/coderay/scanner.rb +238 -238
  28. data/lib/coderay/scanners/_map.rb +15 -14
  29. data/lib/coderay/scanners/c.rb +163 -155
  30. data/lib/coderay/scanners/delphi.rb +131 -129
  31. data/lib/coderay/scanners/html.rb +174 -167
  32. data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
  33. data/lib/coderay/scanners/plaintext.rb +15 -15
  34. data/lib/coderay/scanners/rhtml.rb +73 -65
  35. data/lib/coderay/scanners/ruby.rb +404 -397
  36. data/lib/coderay/scanners/ruby/patterns.rb +216 -216
  37. data/lib/coderay/scanners/xml.rb +18 -18
  38. data/lib/coderay/style.rb +20 -20
  39. data/lib/coderay/styles/_map.rb +3 -3
  40. data/lib/coderay/styles/cycnus.rb +18 -18
  41. data/lib/coderay/styles/murphy.rb +18 -18
  42. data/lib/coderay/tokens.rb +322 -322
  43. metadata +86 -86
  44. data/lib/coderay/scanners/nitro_html.rb +0 -125
  45. data/lib/coderay/scanners/yaml.rb +0 -85
@@ -1,22 +1,22 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- # = YAML Encoder
5
- #
6
- # Slow.
7
- class YAML < Encoder
8
-
9
- register_for :yaml
10
-
11
- FILE_EXTENSION = 'yaml'
12
-
13
- protected
14
- def compile tokens, options
15
- require 'yaml'
16
- @out = tokens.to_a.to_yaml
17
- end
18
-
19
- end
20
-
21
- end
22
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = YAML Encoder
5
+ #
6
+ # Slow.
7
+ class YAML < Encoder
8
+
9
+ register_for :yaml
10
+
11
+ FILE_EXTENSION = 'yaml'
12
+
13
+ protected
14
+ def compile tokens, options
15
+ require 'yaml'
16
+ @out = tokens.to_a.to_yaml
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -9,99 +9,98 @@
9
9
  # == Documentation
10
10
  #
11
11
  # # determine the type of the given
12
- # lang = FileType[ARGV.first]
12
+ # lang = FileType[ARGV.first]
13
13
  #
14
- # # return :plaintext if the file type is unknown
15
- # lang = FileType.fetch ARGV.first, :plaintext
14
+ # # return :plaintext if the file type is unknown
15
+ # lang = FileType.fetch ARGV.first, :plaintext
16
16
  #
17
- # # try the shebang line, too
18
- # lang = FileType.fetch ARGV.first, :plaintext, true
19
- #
17
+ # # try the shebang line, too
18
+ # lang = FileType.fetch ARGV.first, :plaintext, true
20
19
  module FileType
21
-
22
- UnknownFileType = Class.new Exception
23
-
24
- class << self
25
-
26
- # Try to determine the file type of the file.
27
- #
28
- # +filename+ is a relative or absolute path to a file.
29
- #
30
- # The file itself is only accessed when +read_shebang+ is set to true.
31
- # That means you can get filetypes from files that don't exist.
32
- def [] filename, read_shebang = false
33
- name = File.basename filename
34
- ext = File.extname name
35
- ext.sub!(/^\./, '') # delete the leading dot
36
-
37
- type =
38
- TypeFromExt[ext] ||
39
- TypeFromExt[ext.downcase] ||
40
- TypeFromName[name] ||
41
- TypeFromName[name.downcase]
42
- type ||= shebang(filename) if read_shebang
43
-
44
- type
45
- end
46
-
47
- def shebang filename
48
- begin
49
- File.open filename, 'r' do |f|
50
- first_line = f.gets
51
- first_line[TypeFromShebang]
52
- end
53
- rescue IOError
54
- nil
55
- end
56
- end
57
-
58
- # This works like Hash#fetch.
59
- #
60
- # If the filetype cannot be found, the +default+ value
61
- # is returned.
62
- def fetch filename, default = nil, read_shebang = false
63
- if default and block_given?
64
- warn 'block supersedes default value argument'
65
- end
66
-
67
- unless type = self[filename, read_shebang]
68
- return yield if block_given?
69
- return default if default
70
- raise UnknownFileType, 'Could not determine type of %p.' % filename
71
- end
72
- type
73
- end
74
-
75
- end
76
-
77
- TypeFromExt = {
78
- 'rb' => :ruby,
79
- 'rbw' => :ruby,
80
- 'rake' => :ruby,
81
- 'cpp' => :c,
82
- 'c' => :c,
83
- 'h' => :c,
84
- 'xml' => :xml,
85
- 'htm' => :html,
86
- 'html' => :html,
87
- 'xhtml' => :xhtml,
88
- 'rhtml' => :rhtml,
89
- 'yaml' => :yaml,
90
- 'yml' => :yaml,
91
- }
92
-
93
- TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
94
-
95
- TypeFromName = {
96
- 'Rakefile' => :ruby,
97
- 'Rantfile' => :ruby,
98
- }
20
+
21
+ UnknownFileType = Class.new Exception
22
+
23
+ class << self
24
+
25
+ # Try to determine the file type of the file.
26
+ #
27
+ # +filename+ is a relative or absolute path to a file.
28
+ #
29
+ # The file itself is only accessed when +read_shebang+ is set to true.
30
+ # That means you can get filetypes from files that don't exist.
31
+ def [] filename, read_shebang = false
32
+ name = File.basename filename
33
+ ext = File.extname name
34
+ ext.sub!(/^\./, '') # delete the leading dot
35
+
36
+ type =
37
+ TypeFromExt[ext] ||
38
+ TypeFromExt[ext.downcase] ||
39
+ TypeFromName[name] ||
40
+ TypeFromName[name.downcase]
41
+ type ||= shebang(filename) if read_shebang
42
+
43
+ type
44
+ end
45
+
46
+ def shebang filename
47
+ begin
48
+ File.open filename, 'r' do |f|
49
+ first_line = f.gets
50
+ first_line[TypeFromShebang]
51
+ end
52
+ rescue IOError
53
+ nil
54
+ end
55
+ end
56
+
57
+ # This works like Hash#fetch.
58
+ #
59
+ # If the filetype cannot be found, the +default+ value
60
+ # is returned.
61
+ def fetch filename, default = nil, read_shebang = false
62
+ if default and block_given?
63
+ warn 'block supersedes default value argument'
64
+ end
65
+
66
+ unless type = self[filename, read_shebang]
67
+ return yield if block_given?
68
+ return default if default
69
+ raise UnknownFileType, 'Could not determine type of %p.' % filename
70
+ end
71
+ type
72
+ end
73
+
74
+ end
75
+
76
+ TypeFromExt = {
77
+ 'rb' => :ruby,
78
+ 'rbw' => :ruby,
79
+ 'rake' => :ruby,
80
+ 'cpp' => :c,
81
+ 'c' => :c,
82
+ 'h' => :c,
83
+ 'xml' => :xml,
84
+ 'htm' => :html,
85
+ 'html' => :html,
86
+ 'xhtml' => :xhtml,
87
+ 'rhtml' => :rhtml,
88
+ 'yaml' => :yaml,
89
+ 'yml' => :yaml,
90
+ }
91
+
92
+ TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
93
+
94
+ TypeFromName = {
95
+ 'Rakefile' => :ruby,
96
+ 'Rantfile' => :ruby,
97
+ }
99
98
 
100
99
  end
101
100
 
102
101
  if $0 == __FILE__
103
- $VERBOSE = true
104
- eval DATA.read, nil, $0, __LINE__+4
102
+ $VERBOSE = true
103
+ eval DATA.read, nil, $0, __LINE__+4
105
104
  end
106
105
 
107
106
  __END__
@@ -110,72 +109,72 @@ require 'test/unit'
110
109
 
111
110
  class TC_FileType < Test::Unit::TestCase
112
111
 
113
- def test_fetch
114
- assert_raise FileType::UnknownFileType do
115
- FileType.fetch ''
116
- end
117
-
118
- assert_throws :not_found do
119
- FileType.fetch '.' do
120
- throw :not_found
121
- end
122
- end
123
-
124
- assert_equal :default, FileType.fetch('c', :default)
125
-
126
- stderr, fake_stderr = $stderr, Object.new
127
- $err = ''
128
- def fake_stderr.write x
129
- $err << x
130
- end
131
- $stderr = fake_stderr
132
- FileType.fetch('c', :default) { }
133
- assert_equal "block supersedes default value argument\n", $err
134
- $stderr = stderr
135
- end
136
-
137
- def test_ruby
138
- assert_equal :ruby, FileType['test.rb']
139
- assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
140
- assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
141
- assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
142
- assert_equal :ruby, FileType['./lib/tasks\repository.rake']
143
- assert_not_equal :ruby, FileType['test_rb']
144
- assert_not_equal :ruby, FileType['Makefile']
145
- assert_not_equal :ruby, FileType['set.rb/set']
146
- assert_not_equal :ruby, FileType['~/projects/blabla/rb']
147
- end
148
-
149
- def test_c
150
- assert_equal :c, FileType['test.c']
151
- assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
152
- assert_not_equal :c, FileType['test_c']
153
- assert_not_equal :c, FileType['Makefile']
154
- assert_not_equal :c, FileType['set.h/set']
155
- assert_not_equal :c, FileType['~/projects/blabla/c']
156
- end
157
-
158
- def test_html
159
- assert_equal :html, FileType['test.htm']
160
- assert_equal :xhtml, FileType['test.xhtml']
161
- assert_equal :xhtml, FileType['test.html.xhtml']
162
- assert_equal :rhtml, FileType['_form.rhtml']
163
- end
164
-
165
- def test_yaml
166
- assert_equal :yaml, FileType['test.yml']
167
- assert_equal :yaml, FileType['test.yaml']
168
- assert_equal :yaml, FileType['my.html.yaml']
169
- assert_not_equal :yaml, FileType['YAML']
170
- end
171
-
172
- def test_shebang
173
- dir = './test'
174
- if File.directory? dir
175
- Dir.chdir dir do
176
- assert_equal :c, FileType['test.c']
177
- end
178
- end
179
- end
112
+ def test_fetch
113
+ assert_raise FileType::UnknownFileType do
114
+ FileType.fetch ''
115
+ end
116
+
117
+ assert_throws :not_found do
118
+ FileType.fetch '.' do
119
+ throw :not_found
120
+ end
121
+ end
122
+
123
+ assert_equal :default, FileType.fetch('c', :default)
124
+
125
+ stderr, fake_stderr = $stderr, Object.new
126
+ $err = ''
127
+ def fake_stderr.write x
128
+ $err << x
129
+ end
130
+ $stderr = fake_stderr
131
+ FileType.fetch('c', :default) { }
132
+ assert_equal "block supersedes default value argument\n", $err
133
+ $stderr = stderr
134
+ end
135
+
136
+ def test_ruby
137
+ assert_equal :ruby, FileType['test.rb']
138
+ assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
139
+ assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
140
+ assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
141
+ assert_equal :ruby, FileType['./lib/tasks\repository.rake']
142
+ assert_not_equal :ruby, FileType['test_rb']
143
+ assert_not_equal :ruby, FileType['Makefile']
144
+ assert_not_equal :ruby, FileType['set.rb/set']
145
+ assert_not_equal :ruby, FileType['~/projects/blabla/rb']
146
+ end
147
+
148
+ def test_c
149
+ assert_equal :c, FileType['test.c']
150
+ assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
151
+ assert_not_equal :c, FileType['test_c']
152
+ assert_not_equal :c, FileType['Makefile']
153
+ assert_not_equal :c, FileType['set.h/set']
154
+ assert_not_equal :c, FileType['~/projects/blabla/c']
155
+ end
156
+
157
+ def test_html
158
+ assert_equal :html, FileType['test.htm']
159
+ assert_equal :xhtml, FileType['test.xhtml']
160
+ assert_equal :xhtml, FileType['test.html.xhtml']
161
+ assert_equal :rhtml, FileType['_form.rhtml']
162
+ end
163
+
164
+ def test_yaml
165
+ assert_equal :yaml, FileType['test.yml']
166
+ assert_equal :yaml, FileType['test.yaml']
167
+ assert_equal :yaml, FileType['my.html.yaml']
168
+ assert_not_equal :yaml, FileType['YAML']
169
+ end
170
+
171
+ def test_shebang
172
+ dir = './test'
173
+ if File.directory? dir
174
+ Dir.chdir dir do
175
+ assert_equal :c, FileType['test.c']
176
+ end
177
+ end
178
+ end
180
179
 
181
180
  end
@@ -11,40 +11,39 @@
11
11
  # See +GZip+ module and the +String+ extensions.
12
12
  #
13
13
  module GZip
14
-
15
- require 'zlib'
16
14
 
17
- # The default zipping level. 7 zips good and fast.
18
- DEFAULT_GZIP_LEVEL = 7
19
-
20
- # Unzips the given string +s+.
21
- #
22
- # Example:
23
- # require 'gzip_simple'
24
- # print GZip.gunzip(File.read('adresses.gz'))
25
- #
26
- def GZip.gunzip s
27
- Zlib::Inflate.inflate s
28
- end
29
-
30
- # Zips the given string +s+.
31
- #
32
- # Example:
33
- # require 'gzip_simple'
34
- # File.open('adresses.gz', 'w') do |file
35
- # file.write GZip.gzip('Mum: 0123 456 789', 9)
36
- # end
37
- #
38
- # If you provide a +level+, you can control how strong
39
- # the string is compressed:
40
- # - 0: no compression, only convert to gzip format
41
- # - 1: compress fast
42
- # - 7: compress more, but still fast (default)
43
- # - 8: compress more, slower
44
- # - 9: compress best, very slow
45
- def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
46
- Zlib::Deflate.new(level).deflate s, Zlib::FINISH
47
- end
15
+ require 'zlib'
16
+
17
+ # The default zipping level. 7 zips good and fast.
18
+ DEFAULT_GZIP_LEVEL = 7
19
+
20
+ # Unzips the given string +s+.
21
+ #
22
+ # Example:
23
+ # require 'gzip_simple'
24
+ # print GZip.gunzip(File.read('adresses.gz'))
25
+ def GZip.gunzip s
26
+ Zlib::Inflate.inflate s
27
+ end
28
+
29
+ # Zips the given string +s+.
30
+ #
31
+ # Example:
32
+ # require 'gzip_simple'
33
+ # File.open('adresses.gz', 'w') do |file
34
+ # file.write GZip.gzip('Mum: 0123 456 789', 9)
35
+ # end
36
+ #
37
+ # If you provide a +level+, you can control how strong
38
+ # the string is compressed:
39
+ # - 0: no compression, only convert to gzip format
40
+ # - 1: compress fast
41
+ # - 7: compress more, but still fast (default)
42
+ # - 8: compress more, slower
43
+ # - 9: compress best, very slow
44
+ def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
45
+ Zlib::Deflate.new(level).deflate s, Zlib::FINISH
46
+ end
48
47
  end
49
48
 
50
49
  # String extensions to use the GZip module.
@@ -65,31 +64,31 @@ end
65
64
  # # unzipping works
66
65
  # p x_gz.gunzip == x #-> true
67
66
  class String
68
- # Returns the string, unzipped.
69
- # See GZip.gunzip
70
- def gunzip
71
- GZip.gunzip self
72
- end
73
- # Replaces the string with its unzipped value.
74
- # See GZip.gunzip
75
- def gunzip!
76
- replace gunzip
77
- end
78
-
79
- # Returns the string, zipped.
80
- # +level+ is the gzip compression level, see GZip.gzip.
81
- def gzip level = GZip::DEFAULT_GZIP_LEVEL
82
- GZip.gzip self, level
83
- end
84
- # Replaces the string with its zipped value.
85
- # See GZip.gzip.
86
- def gzip!(*args)
87
- replace gzip(*args)
88
- end
67
+ # Returns the string, unzipped.
68
+ # See GZip.gunzip
69
+ def gunzip
70
+ GZip.gunzip self
71
+ end
72
+ # Replaces the string with its unzipped value.
73
+ # See GZip.gunzip
74
+ def gunzip!
75
+ replace gunzip
76
+ end
77
+
78
+ # Returns the string, zipped.
79
+ # +level+ is the gzip compression level, see GZip.gzip.
80
+ def gzip level = GZip::DEFAULT_GZIP_LEVEL
81
+ GZip.gzip self, level
82
+ end
83
+ # Replaces the string with its zipped value.
84
+ # See GZip.gzip.
85
+ def gzip!(*args)
86
+ replace gzip(*args)
87
+ end
89
88
  end
90
89
 
91
90
  if $0 == __FILE__
92
- eval DATA.read, nil, $0, __LINE__+4
91
+ eval DATA.read, nil, $0, __LINE__+4
93
92
  end
94
93
 
95
94
  __END__
@@ -107,17 +106,17 @@ INFO = 'packed to %0.3f%%' # :nodoc:
107
106
 
108
107
  x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
109
108
  Benchmark.bm(10) do |bm|
110
- for level in 0..9
111
- bm.report "zip #{level}" do
112
- $x = x.gzip level
113
- end
114
- puts INFO % [100.0 * $x.size / x.size]
115
- end
116
- bm.report 'zip' do
117
- $x = x.gzip
118
- end
119
- puts INFO % [100.0 * $x.size / x.size]
120
- bm.report 'unzip' do
121
- $x.gunzip
122
- end
109
+ for level in 0..9
110
+ bm.report "zip #{level}" do
111
+ $x = x.gzip level
112
+ end
113
+ puts INFO % [100.0 * $x.size / x.size]
114
+ end
115
+ bm.report 'zip' do
116
+ $x = x.gzip
117
+ end
118
+ puts INFO % [100.0 * $x.size / x.size]
119
+ bm.report 'unzip' do
120
+ $x.gunzip
121
+ end
123
122
  end