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
@@ -0,0 +1,41 @@
1
+ module CodeRay
2
+
3
+ # A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
4
+ module GZip
5
+
6
+ require 'zlib'
7
+
8
+ # The default zipping level. 7 zips good and fast.
9
+ DEFAULT_GZIP_LEVEL = 7
10
+
11
+ # Unzips the given string +s+.
12
+ #
13
+ # Example:
14
+ # require 'gzip_simple'
15
+ # print GZip.gunzip(File.read('adresses.gz'))
16
+ def GZip.gunzip s
17
+ Zlib::Inflate.inflate s
18
+ end
19
+
20
+ # Zips the given string +s+.
21
+ #
22
+ # Example:
23
+ # require 'gzip_simple'
24
+ # File.open('adresses.gz', 'w') do |file
25
+ # file.write GZip.gzip('Mum: 0123 456 789', 9)
26
+ # end
27
+ #
28
+ # If you provide a +level+, you can control how strong
29
+ # the string is compressed:
30
+ # - 0: no compression, only convert to gzip format
31
+ # - 1: compress fast
32
+ # - 7: compress more, but still fast (default)
33
+ # - 8: compress more, slower
34
+ # - 9: compress best, very slow
35
+ def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
36
+ Zlib::Deflate.new(level).deflate s, Zlib::FINISH
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -1,350 +1,280 @@
1
1
  module CodeRay
2
2
 
3
- # = PluginHost
4
- #
5
- # A simple subclass plugin system.
6
- #
7
- # Example:
8
- # class Generators < PluginHost
9
- # plugin_path 'app/generators'
10
- # end
11
- #
12
- # class Generator
13
- # extend Plugin
14
- # PLUGIN_HOST = Generators
15
- # end
16
- #
17
- # class FancyGenerator < Generator
18
- # register_for :fancy
19
- # end
20
- #
21
- # Generators[:fancy] #-> FancyGenerator
22
- # # or
23
- # CodeRay.require_plugin 'Generators/fancy'
24
- module PluginHost
25
-
26
- # Raised if Encoders::[] fails because:
27
- # * a file could not be found
28
- # * the requested Encoder is not registered
29
- PluginNotFound = Class.new Exception
30
- HostNotFound = Class.new Exception
31
-
32
- PLUGIN_HOSTS = []
33
- PLUGIN_HOSTS_BY_ID = {} # dummy hash
34
-
35
- # Loads all plugins using list and load.
36
- def load_all
37
- for plugin in list
38
- load plugin
39
- end
40
- end
41
-
42
- # Returns the Plugin for +id+.
3
+ # = PluginHost
4
+ #
5
+ # A simple subclass/subfolder plugin system.
43
6
  #
44
7
  # Example:
45
- # yaml_plugin = MyPluginHost[:yaml]
46
- def [] id, *args, &blk
47
- plugin = validate_id(id)
48
- begin
49
- plugin = plugin_hash.[] plugin, *args, &blk
50
- end while plugin.is_a? Symbol
51
- plugin
52
- end
53
-
54
- # Alias for +[]+.
55
- alias load []
56
-
57
- def require_helper plugin_id, helper_name
58
- path = path_to File.join(plugin_id, helper_name)
59
- require path
60
- end
61
-
62
- class << self
63
-
64
- # Adds the module/class to the PLUGIN_HOSTS list.
65
- def extended mod
66
- PLUGIN_HOSTS << mod
8
+ # class Generators
9
+ # extend PluginHost
10
+ # plugin_path 'app/generators'
11
+ # end
12
+ #
13
+ # class Generator
14
+ # extend Plugin
15
+ # PLUGIN_HOST = Generators
16
+ # end
17
+ #
18
+ # class FancyGenerator < Generator
19
+ # register_for :fancy
20
+ # end
21
+ #
22
+ # Generators[:fancy] #-> FancyGenerator
23
+ # # or
24
+ # CodeRay.require_plugin 'Generators/fancy'
25
+ # # or
26
+ # Generators::Fancy
27
+ module PluginHost
28
+
29
+ # Raised if Encoders::[] fails because:
30
+ # * a file could not be found
31
+ # * the requested Plugin is not registered
32
+ PluginNotFound = Class.new LoadError
33
+ HostNotFound = Class.new LoadError
34
+
35
+ PLUGIN_HOSTS = []
36
+ PLUGIN_HOSTS_BY_ID = {} # dummy hash
37
+
38
+ # Loads all plugins using list and load.
39
+ def load_all
40
+ for plugin in list
41
+ load plugin
42
+ end
67
43
  end
68
-
69
- # Warns you that you should not #include this module.
70
- def included mod
71
- warn "#{name} should not be included. Use extend."
44
+
45
+ # Returns the Plugin for +id+.
46
+ #
47
+ # Example:
48
+ # yaml_plugin = MyPluginHost[:yaml]
49
+ def [] id, *args, &blk
50
+ plugin = validate_id(id)
51
+ begin
52
+ plugin = plugin_hash.[] plugin, *args, &blk
53
+ end while plugin.is_a? Symbol
54
+ plugin
72
55
  end
73
-
74
- # Find the PluginHost for host_id.
75
- def host_by_id host_id
76
- unless PLUGIN_HOSTS_BY_ID.default_proc
77
- ph = Hash.new do |h, a_host_id|
78
- for host in PLUGIN_HOSTS
79
- h[host.host_id] = host
80
- end
81
- h.fetch a_host_id, nil
82
- end
83
- PLUGIN_HOSTS_BY_ID.replace ph
56
+
57
+ alias load []
58
+
59
+ # Tries to +load+ the missing plugin by translating +const+ to the
60
+ # underscore form (eg. LinesOfCode becomes lines_of_code).
61
+ def const_missing const
62
+ id = const.to_s.
63
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
64
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
65
+ downcase
66
+ load id
67
+ end
68
+
69
+ class << self
70
+
71
+ # Adds the module/class to the PLUGIN_HOSTS list.
72
+ def extended mod
73
+ PLUGIN_HOSTS << mod
84
74
  end
85
- PLUGIN_HOSTS_BY_ID[host_id]
75
+
86
76
  end
87
-
88
- end
89
-
90
- # The path where the plugins can be found.
91
- def plugin_path *args
92
- unless args.empty?
93
- @plugin_path = File.expand_path File.join(*args)
94
- load_map
77
+
78
+ # The path where the plugins can be found.
79
+ def plugin_path *args
80
+ unless args.empty?
81
+ @plugin_path = File.expand_path File.join(*args)
82
+ end
83
+ @plugin_path ||= ''
95
84
  end
96
- @plugin_path
97
- end
98
-
99
- # The host's ID.
100
- #
101
- # If PLUGIN_HOST_ID is not set, it is simply the class name.
102
- def host_id
103
- if self.const_defined? :PLUGIN_HOST_ID
104
- self::PLUGIN_HOST_ID
105
- else
106
- name
85
+
86
+ # Map a plugin_id to another.
87
+ #
88
+ # Usage: Put this in a file plugin_path/_map.rb.
89
+ #
90
+ # class MyColorHost < PluginHost
91
+ # map :navy => :dark_blue,
92
+ # :maroon => :brown,
93
+ # :luna => :moon
94
+ # end
95
+ def map hash
96
+ for from, to in hash
97
+ from = validate_id from
98
+ to = validate_id to
99
+ plugin_hash[from] = to unless plugin_hash.has_key? from
100
+ end
107
101
  end
108
- end
109
-
110
- # Map a plugin_id to another.
111
- #
112
- # Usage: Put this in a file plugin_path/_map.rb.
113
- #
114
- # class MyColorHost < PluginHost
115
- # map :navy => :dark_blue,
116
- # :maroon => :brown,
117
- # :luna => :moon
118
- # end
119
- def map hash
120
- for from, to in hash
121
- from = validate_id from
122
- to = validate_id to
123
- plugin_hash[from] = to unless plugin_hash.has_key? from
102
+
103
+ # Define the default plugin to use when no plugin is found
104
+ # for a given id, or return the default plugin.
105
+ #
106
+ # See also map.
107
+ #
108
+ # class MyColorHost < PluginHost
109
+ # map :navy => :dark_blue
110
+ # default :gray
111
+ # end
112
+ #
113
+ # MyColorHost.default # loads and returns the Gray plugin
114
+ def default id = nil
115
+ if id
116
+ id = validate_id id
117
+ plugin_hash[nil] = id
118
+ else
119
+ load nil
120
+ end
124
121
  end
125
- end
126
-
127
- # Define the default plugin to use when no plugin is found
128
- # for a given id.
129
- #
130
- # See also map.
131
- #
132
- # class MyColorHost < PluginHost
133
- # map :navy => :dark_blue
134
- # default :gray
135
- # end
136
- def default id = nil
137
- if id
138
- id = validate_id id
139
- plugin_hash[nil] = id
140
- else
141
- plugin_hash[nil]
122
+
123
+ # Every plugin must register itself for one or more
124
+ # +ids+ by calling register_for, which calls this method.
125
+ #
126
+ # See Plugin#register_for.
127
+ def register plugin, *ids
128
+ for id in ids
129
+ plugin_hash[validate_id(id)] = plugin
130
+ end
142
131
  end
143
- end
144
-
145
- # Every plugin must register itself for one or more
146
- # +ids+ by calling register_for, which calls this method.
147
- #
148
- # See Plugin#register_for.
149
- def register plugin, *ids
150
- for id in ids
151
- unless id.is_a? Symbol
152
- raise ArgumentError,
153
- "id must be a Symbol, but it was a #{id.class}"
132
+
133
+ # A Hash of plugion_id => Plugin pairs.
134
+ def plugin_hash
135
+ @plugin_hash ||= make_plugin_hash
136
+ end
137
+
138
+ # Returns an array of all .rb files in the plugin path.
139
+ #
140
+ # The extension .rb is not included.
141
+ def list
142
+ Dir[path_to('*')].select do |file|
143
+ File.basename(file)[/^(?!_)\w+\.rb$/]
144
+ end.map do |file|
145
+ File.basename(file, '.rb').to_sym
154
146
  end
155
- plugin_hash[validate_id(id)] = plugin
156
147
  end
157
- end
158
-
159
- # A Hash of plugion_id => Plugin pairs.
160
- def plugin_hash
161
- @plugin_hash ||= create_plugin_hash
162
- end
163
-
164
- # Returns an array of all .rb files in the plugin path.
165
- #
166
- # The extension .rb is not included.
167
- def list
168
- Dir[path_to('*')].select do |file|
169
- File.basename(file)[/^(?!_)\w+\.rb$/]
170
- end.map do |file|
171
- File.basename file, '.rb'
148
+
149
+ # Returns an array of all Plugins.
150
+ #
151
+ # Note: This loads all plugins using load_all.
152
+ def all_plugins
153
+ load_all
154
+ plugin_hash.values.grep(Class)
172
155
  end
173
- end
174
-
175
- # Makes a map of all loaded plugins.
176
- def inspect
177
- map = plugin_hash.dup
178
- map.each do |id, plugin|
179
- map[id] = plugin.to_s[/(?>\w+)$/]
156
+
157
+ # Returns an array of all plugin titles.
158
+ #
159
+ # Note: This loads all plugins using load_all.
160
+ def all_titles
161
+ all_plugins.map { |plugin| plugin.title }
180
162
  end
181
- "#{name}[#{host_id}]#{map.inspect}"
182
- end
183
-
184
- protected
185
- # Created a new plugin list and stores it to @plugin_hash.
186
- def create_plugin_hash
187
- @plugin_hash =
163
+
164
+ protected
165
+
166
+ # Return a plugin hash that automatically loads plugins.
167
+ def make_plugin_hash
168
+ map_loaded = false
188
169
  Hash.new do |h, plugin_id|
189
170
  id = validate_id(plugin_id)
190
171
  path = path_to id
191
172
  begin
173
+ raise LoadError, "#{path} not found" unless File.exist? path
192
174
  require path
193
175
  rescue LoadError => boom
194
- if h.has_key? nil # default plugin
195
- h[id] = h[nil]
176
+ if map_loaded
177
+ if h.has_key?(nil) # default plugin
178
+ h[nil]
179
+ else
180
+ raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
181
+ end
196
182
  else
197
- raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
183
+ load_map
184
+ map_loaded = true
185
+ h[plugin_id]
198
186
  end
199
187
  else
200
188
  # Plugin should have registered by now
201
- unless h.has_key? id
202
- raise PluginNotFound,
203
- "No #{self.name} plugin for #{id.inspect} found in #{path}."
189
+ if h.has_key? id
190
+ h[id]
191
+ else
192
+ raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
204
193
  end
205
194
  end
206
- h[id]
207
195
  end
208
- end
209
-
210
- # Loads the map file (see map).
211
- #
212
- # This is done automatically when plugin_path is called.
213
- def load_map
214
- mapfile = path_to '_map'
215
- if File.exist? mapfile
216
- require mapfile
217
- elsif $VERBOSE
218
- warn 'no _map.rb found for %s' % name
219
196
  end
220
- end
221
-
222
- # Returns the Plugin for +id+.
223
- # Use it like Hash#fetch.
224
- #
225
- # Example:
226
- # yaml_plugin = MyPluginHost[:yaml, :default]
227
- def fetch id, *args, &blk
228
- plugin_hash.fetch validate_id(id), *args, &blk
229
- end
230
-
231
- # Returns the expected path to the plugin file for the given id.
232
- def path_to plugin_id
233
- File.join plugin_path, "#{plugin_id}.rb"
234
- end
235
-
236
- # Converts +id+ to a Symbol if it is a String,
237
- # or returns +id+ if it already is a Symbol.
238
- #
239
- # Raises +ArgumentError+ for all other objects, or if the
240
- # given String includes non-alphanumeric characters (\W).
241
- def validate_id id
242
- if id.is_a? Symbol or id.nil?
243
- id
244
- elsif id.is_a? String
245
- if id[/\w+/] == id
246
- id.downcase.to_sym
197
+
198
+ # Loads the map file (see map).
199
+ #
200
+ # This is done automatically when plugin_path is called.
201
+ def load_map
202
+ mapfile = path_to '_map'
203
+ require mapfile if File.exist? mapfile
204
+ end
205
+
206
+ # Returns the expected path to the plugin file for the given id.
207
+ def path_to plugin_id
208
+ File.join plugin_path, "#{plugin_id}.rb"
209
+ end
210
+
211
+ # Converts +id+ to a Symbol if it is a String,
212
+ # or returns +id+ if it already is a Symbol.
213
+ #
214
+ # Raises +ArgumentError+ for all other objects, or if the
215
+ # given String includes non-alphanumeric characters (\W).
216
+ def validate_id id
217
+ if id.is_a? Symbol or id.nil?
218
+ id
219
+ elsif id.is_a? String
220
+ if id[/\w+/] == id
221
+ id.downcase.to_sym
222
+ else
223
+ raise ArgumentError, "Invalid id given: #{id}"
224
+ end
247
225
  else
248
- raise ArgumentError, "Invalid id: '#{id}' given."
226
+ raise ArgumentError, "String or Symbol expected, but #{id.class} given."
249
227
  end
250
- else
251
- raise ArgumentError,
252
- "String or Symbol expected, but #{id.class} given."
253
228
  end
254
- end
255
-
256
- end
257
-
258
-
259
- # = Plugin
260
- #
261
- # Plugins have to include this module.
262
- #
263
- # IMPORTANT: use extend for this module.
264
- #
265
- # Example: see PluginHost.
266
- module Plugin
267
-
268
- def included mod
269
- warn "#{name} should not be included. Use extend."
270
- end
271
-
272
- # Register this class for the given langs.
273
- # Example:
274
- # class MyPlugin < PluginHost::BaseClass
275
- # register_for :my_id
276
- # ...
277
- # end
278
- #
279
- # See PluginHost.register.
280
- def register_for *ids
281
- @plugin_id = ids.first
282
- plugin_host.register self, *ids
229
+
283
230
  end
284
231
 
285
- # Returns the title of the plugin, or sets it to the
286
- # optional argument +title+.
287
- def title title = nil
288
- if title
289
- @title = title.to_s
290
- else
291
- @title ||= name[/([^:]+)$/, 1]
292
- end
293
- end
294
-
295
- # The host for this Plugin class.
296
- def plugin_host host = nil
297
- if host and not host.is_a? PluginHost
298
- raise ArgumentError,
299
- "PluginHost expected, but #{host.class} given."
300
- end
301
- self.const_set :PLUGIN_HOST, host if host
302
- self::PLUGIN_HOST
303
- end
304
-
305
- # Require some helper files.
232
+
233
+ # = Plugin
306
234
  #
307
- # Example:
235
+ # Plugins have to include this module.
308
236
  #
309
- # class MyPlugin < PluginHost::BaseClass
310
- # register_for :my_id
311
- # helper :my_helper
237
+ # IMPORTANT: Use extend for this module.
312
238
  #
313
- # The above example loads the file myplugin/my_helper.rb relative to the
314
- # file in which MyPlugin was defined.
315
- #
316
- # You can also load a helper from a different plugin:
317
- #
318
- # helper 'other_plugin/helper_name'
319
- def helper *helpers
320
- for helper in helpers
321
- if helper.is_a?(String) && helper[/\//]
322
- self::PLUGIN_HOST.require_helper $`, $'
239
+ # See CodeRay::PluginHost for examples.
240
+ module Plugin
241
+
242
+ # Register this class for the given langs.
243
+ # Example:
244
+ # class MyPlugin < PluginHost::BaseClass
245
+ # register_for :my_id
246
+ # ...
247
+ # end
248
+ #
249
+ # See PluginHost.register.
250
+ def register_for *ids
251
+ @plugin_id = ids.first
252
+ plugin_host.register self, *ids
253
+ end
254
+
255
+ # Returns the title of the plugin, or sets it to the
256
+ # optional argument +title+.
257
+ def title title = nil
258
+ if title
259
+ @title = title.to_s
323
260
  else
324
- self::PLUGIN_HOST.require_helper plugin_id.to_s, helper.to_s
261
+ @title ||= name[/([^:]+)$/, 1]
325
262
  end
326
263
  end
264
+
265
+ # The PluginHost for this Plugin class.
266
+ def plugin_host host = nil
267
+ if host.is_a? PluginHost
268
+ const_set :PLUGIN_HOST, host
269
+ end
270
+ self::PLUGIN_HOST
271
+ end
272
+
273
+ # Returns the plugin id used by the engine.
274
+ def plugin_id
275
+ @plugin_id ||= name[/\w+$/].downcase
276
+ end
277
+
327
278
  end
328
-
329
- # Returns the pulgin id used by the engine.
330
- def plugin_id
331
- @plugin_id || name[/\w+$/].downcase
332
- end
333
-
334
- end
335
-
336
- # Convenience method for plugin loading.
337
- # The syntax used is:
338
- #
339
- # CodeRay.require_plugin '<Host ID>/<Plugin ID>'
340
- #
341
- # Returns the loaded plugin.
342
- def self.require_plugin path
343
- host_id, plugin_id = path.split '/', 2
344
- host = PluginHost.host_by_id(host_id)
345
- raise PluginHost::HostNotFound,
346
- "No host for #{host_id.inspect} found." unless host
347
- host.load plugin_id
279
+
348
280
  end
349
-
350
- end