coderay 0.4.3.48
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.
- data/LICENSE +340 -0
- data/README +103 -0
- data/demo/demo_count.rb +10 -0
- data/demo/demo_css.rb +4 -0
- data/demo/demo_div.rb +19 -0
- data/demo/demo_dump.rb +15 -0
- data/demo/demo_encoder.rb +39 -0
- data/demo/demo_global_vars.rb +13 -0
- data/demo/demo_global_vars2.rb +28 -0
- data/demo/demo_html.rb +394 -0
- data/demo/demo_html2.rb +11 -0
- data/demo/demo_load_encoder.rb +17 -0
- data/demo/demo_more.rb +204 -0
- data/demo/demo_scanner.rb +36 -0
- data/demo/demo_server.rb +92 -0
- data/demo/demo_simple.rb +10 -0
- data/demo/demo_stream.rb +25 -0
- data/demo/demo_stream2.rb +8 -0
- data/demo/demo_tokens.rb +3 -0
- data/lib/coderay.rb +284 -0
- data/lib/coderay/encoder.rb +151 -0
- data/lib/coderay/encoders/count.rb +21 -0
- data/lib/coderay/encoders/div.rb +16 -0
- data/lib/coderay/encoders/helpers/html_css.rb +155 -0
- data/lib/coderay/encoders/helpers/html_helper.rb +68 -0
- data/lib/coderay/encoders/helpers/html_output.rb +237 -0
- data/lib/coderay/encoders/html.rb +169 -0
- data/lib/coderay/encoders/null.rb +20 -0
- data/lib/coderay/encoders/span.rb +16 -0
- data/lib/coderay/encoders/statistic.rb +74 -0
- data/lib/coderay/encoders/text.rb +33 -0
- data/lib/coderay/encoders/tokens.rb +44 -0
- data/lib/coderay/encoders/yaml.rb +19 -0
- data/lib/coderay/helpers/filetype.rb +145 -0
- data/lib/coderay/helpers/gzip_simple.rb +123 -0
- data/lib/coderay/helpers/plugin.rb +286 -0
- data/lib/coderay/helpers/scanner_helper.rb +63 -0
- data/lib/coderay/scanner.rb +197 -0
- data/lib/coderay/scanners/c.rb +147 -0
- data/lib/coderay/scanners/delphi.rb +123 -0
- data/lib/coderay/scanners/helpers/ruby_helper.rb +212 -0
- data/lib/coderay/scanners/plaintext.rb +13 -0
- data/lib/coderay/scanners/ruby.rb +337 -0
- data/lib/coderay/tokens.rb +324 -0
- metadata +89 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
# =GZip Simple
|
2
|
+
#
|
3
|
+
# A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
|
4
|
+
#
|
5
|
+
# Author: murphy (mail to murphy cYcnus de)
|
6
|
+
#
|
7
|
+
# Version: 0.2 (2005.may.28)
|
8
|
+
#
|
9
|
+
# ==Documentation
|
10
|
+
#
|
11
|
+
# See +GZip+ module and the +String+ extensions.
|
12
|
+
#
|
13
|
+
module GZip
|
14
|
+
|
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
|
+
#
|
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
|
48
|
+
end
|
49
|
+
|
50
|
+
# String extensions to use the GZip module.
|
51
|
+
#
|
52
|
+
# The methods gzip and gunzip provide an even more simple
|
53
|
+
# interface to the ZLib:
|
54
|
+
#
|
55
|
+
# # create a big string
|
56
|
+
# x = 'a' * 1000
|
57
|
+
#
|
58
|
+
# # zip it
|
59
|
+
# x_gz = x.gzip
|
60
|
+
#
|
61
|
+
# # test the result
|
62
|
+
# puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
|
63
|
+
# #-> Zipped 1000 bytes to 19 bytes.
|
64
|
+
#
|
65
|
+
# # unzipping works
|
66
|
+
# p x_gz.gunzip == x #-> true
|
67
|
+
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
|
89
|
+
end
|
90
|
+
|
91
|
+
if $0 == __FILE__
|
92
|
+
eval DATA.read, nil, $0, __LINE__+4
|
93
|
+
end
|
94
|
+
|
95
|
+
__END__
|
96
|
+
#CODE
|
97
|
+
|
98
|
+
# Testing / Benchmark
|
99
|
+
x = 'a' * 1000
|
100
|
+
x_gz = x.gzip
|
101
|
+
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
|
102
|
+
p x_gz.gunzip == x #-> true
|
103
|
+
|
104
|
+
require 'benchmark'
|
105
|
+
|
106
|
+
INFO = 'packed to %0.3f%%' # :nodoc:
|
107
|
+
|
108
|
+
x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
|
109
|
+
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
|
123
|
+
end
|
@@ -0,0 +1,286 @@
|
|
1
|
+
# = PluginHost
|
2
|
+
#
|
3
|
+
# $Id: plugin.rb 47 2005-10-01 06:04:52Z murphy $
|
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
|
+
# 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
|
+
class << self
|
36
|
+
|
37
|
+
def extended mod
|
38
|
+
PLUGIN_HOSTS << mod
|
39
|
+
end
|
40
|
+
|
41
|
+
def included mod
|
42
|
+
warn "#{name} should not be included. Use extend."
|
43
|
+
end
|
44
|
+
|
45
|
+
# Find the PluginHost for host_id.
|
46
|
+
def host_by_id host_id
|
47
|
+
unless PLUGIN_HOSTS_BY_ID.default_proc
|
48
|
+
ph = Hash.new do |h, a_host_id|
|
49
|
+
for host in PLUGIN_HOSTS
|
50
|
+
h[host.host_id] = host
|
51
|
+
end
|
52
|
+
h.fetch a_host_id, nil
|
53
|
+
end
|
54
|
+
PLUGIN_HOSTS_BY_ID.replace ph
|
55
|
+
end
|
56
|
+
PLUGIN_HOSTS_BY_ID[host_id]
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
def plugin_host_id host_id
|
62
|
+
if host_id.is_a? String
|
63
|
+
raise ArgumentError,
|
64
|
+
"String or Symbol expected, but #{lang.class} given."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# The path where the plugins can be found.
|
69
|
+
def plugin_path *args
|
70
|
+
unless args.empty?
|
71
|
+
@plugin_path = File.join(*args)
|
72
|
+
end
|
73
|
+
@plugin_path
|
74
|
+
end
|
75
|
+
|
76
|
+
# The host's ID.
|
77
|
+
#
|
78
|
+
# If PLUGIN_HOST_ID is not set, it is simply the class name.
|
79
|
+
def host_id
|
80
|
+
if self.const_defined? :PLUGIN_HOST_ID
|
81
|
+
self::PLUGIN_HOST_ID
|
82
|
+
else
|
83
|
+
name
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_plugin_hash
|
88
|
+
@plugin_hash =
|
89
|
+
Hash.new do |h, plugin_id|
|
90
|
+
id = validate_id(plugin_id)
|
91
|
+
path = path_to id
|
92
|
+
begin
|
93
|
+
$stderr.puts 'Loading plugin: ' + path if $DEBUG
|
94
|
+
require path
|
95
|
+
rescue LoadError
|
96
|
+
raise PluginNotFound, "Plugin #{id.inspect} not found."
|
97
|
+
else
|
98
|
+
# Plugin should have registered by now
|
99
|
+
unless h.has_key? id
|
100
|
+
raise PluginNotFound,
|
101
|
+
"No #{self.name} plugin for #{id.inspect} found in #{path}."
|
102
|
+
end
|
103
|
+
end
|
104
|
+
h[id]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def plugin_hash
|
109
|
+
@plugin_hash ||= create_plugin_hash
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
# Every plugin must register itself for one or more
|
114
|
+
# +ids+ by calling register_for, which calls this method.
|
115
|
+
#
|
116
|
+
# See Plugin#register_for.
|
117
|
+
def register plugin, *ids
|
118
|
+
for id in ids
|
119
|
+
unless id.is_a? Symbol
|
120
|
+
raise ArgumentError,
|
121
|
+
"id must be a Symbol, but it was a #{id.class}"
|
122
|
+
end
|
123
|
+
plugin_hash[validate_id(id)] = plugin
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
# Returns an array of all .rb files in the plugin path.
|
129
|
+
#
|
130
|
+
# The extension .rb is not included.
|
131
|
+
def all_plugin_names
|
132
|
+
Dir[path_to('*')].map do |file|
|
133
|
+
File.basename file, '.rb'
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Loads all plugins using all_plugin_names and load.
|
138
|
+
def load_all
|
139
|
+
for plugin in all_plugin_names
|
140
|
+
load_plugin plugin
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
# Returns the Plugin for +id+.
|
146
|
+
#
|
147
|
+
# Example:
|
148
|
+
# yaml_plugin = MyPluginHost[:yaml]
|
149
|
+
def [] id, *args, &blk
|
150
|
+
plugin_hash.[] validate_id(id), *args, &blk
|
151
|
+
end
|
152
|
+
|
153
|
+
# Alias for +[]+.
|
154
|
+
alias load_plugin []
|
155
|
+
|
156
|
+
# Returns the Plugin for +id+.
|
157
|
+
# Use it like Hash#fetch.
|
158
|
+
#
|
159
|
+
# Example:
|
160
|
+
# yaml_plugin = MyPluginHost[:yaml, :default]
|
161
|
+
def fetch id, *args, &blk
|
162
|
+
plugin_hash.fetch validate_id(id), *args, &blk
|
163
|
+
end
|
164
|
+
|
165
|
+
# Returns the path to the encoder for format.
|
166
|
+
def path_to plugin_id
|
167
|
+
File.join plugin_path, "#{plugin_id}.rb"
|
168
|
+
end
|
169
|
+
|
170
|
+
# Converts +id+ to a downcase Symbol if it is a String,
|
171
|
+
# or returns +id+ if it already is a Symbol.
|
172
|
+
#
|
173
|
+
# Raises +ArgumentError+ for all other objects, or if the
|
174
|
+
# given String includes non-alphanumeric characters (\W).
|
175
|
+
def validate_id id
|
176
|
+
if id.is_a? Symbol
|
177
|
+
id
|
178
|
+
elsif id.is_a? String
|
179
|
+
if id[/\w+/] == id
|
180
|
+
id.downcase.to_sym
|
181
|
+
else
|
182
|
+
raise ArgumentError, "Invalid id: '#{id}' given."
|
183
|
+
end
|
184
|
+
else
|
185
|
+
raise ArgumentError,
|
186
|
+
"String or Symbol expected, but #{id.class} given."
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
# = Plugin
|
194
|
+
#
|
195
|
+
# Plugins have to include this module.
|
196
|
+
#
|
197
|
+
# IMPORTANT: use extend for this module.
|
198
|
+
#
|
199
|
+
# Example: see PluginHost.
|
200
|
+
module Plugin
|
201
|
+
|
202
|
+
def included mod
|
203
|
+
warn "#{name} should not be included. Use extend."
|
204
|
+
end
|
205
|
+
|
206
|
+
# Register this class for the given langs.
|
207
|
+
# Example:
|
208
|
+
# class MyPlugin < PluginHost::BaseClass
|
209
|
+
# register_for :my_id
|
210
|
+
# ...
|
211
|
+
# end
|
212
|
+
#
|
213
|
+
# See PluginHost.register.
|
214
|
+
def register_for *ids
|
215
|
+
plugin_host.register self, *ids
|
216
|
+
end
|
217
|
+
|
218
|
+
# The host for this Plugin class.
|
219
|
+
def plugin_host host = nil
|
220
|
+
if host and not host.is_a? PluginHost
|
221
|
+
raise ArgumentError,
|
222
|
+
"PluginHost expected, but #{host.class} given."
|
223
|
+
end
|
224
|
+
self.const_set :PLUGIN_HOST, host if host
|
225
|
+
self::PLUGIN_HOST
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
|
231
|
+
# Convenience method for plugin loading.
|
232
|
+
# The syntax used is:
|
233
|
+
#
|
234
|
+
# require_plugin '<Host ID>/<Plugin ID>'
|
235
|
+
#
|
236
|
+
# Returns the loaded plugin.
|
237
|
+
def require_plugin path
|
238
|
+
host_id, plugin_id = path.split '/', 2
|
239
|
+
host = PluginHost.host_by_id(host_id)
|
240
|
+
raise PluginHost::HostNotFound,
|
241
|
+
"No host for #{host_id.inspect} found." unless host
|
242
|
+
host.load_plugin plugin_id
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
if $0 == __FILE__
|
247
|
+
$VERBOSE = $DEBUG = true
|
248
|
+
eval DATA.read, nil, $0, __LINE__+4
|
249
|
+
end
|
250
|
+
|
251
|
+
__END__
|
252
|
+
|
253
|
+
require 'test/unit'
|
254
|
+
|
255
|
+
class TC_PLUGINS < Test::Unit::TestCase
|
256
|
+
|
257
|
+
class Generators
|
258
|
+
extend PluginHost
|
259
|
+
plugin_path '.'
|
260
|
+
end
|
261
|
+
|
262
|
+
class Generator
|
263
|
+
extend Plugin
|
264
|
+
plugin_host Generators
|
265
|
+
end
|
266
|
+
|
267
|
+
class FancyGenerator < Generator
|
268
|
+
register_for :plugin_host
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_plugin
|
272
|
+
assert_nothing_raised do
|
273
|
+
Generators[:plugin_host]
|
274
|
+
end
|
275
|
+
assert_equal FancyGenerator, Generators[:plugin_host]
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_require
|
279
|
+
assert_nothing_raised do
|
280
|
+
require_plugin('TC_PLUGINS::Generators/plugin_host')
|
281
|
+
end
|
282
|
+
assert_equal FancyGenerator,
|
283
|
+
require_plugin('TC_PLUGINS::Generators/plugin_host')
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module CodeRay
|
2
|
+
module Scanners
|
3
|
+
|
4
|
+
class Scanner
|
5
|
+
|
6
|
+
# A WordList is a Hash with some additional features.
|
7
|
+
# It is intended to be used for keyword recognition.
|
8
|
+
class WordList < Hash
|
9
|
+
|
10
|
+
def initialize default = false, case_mode = :case_match
|
11
|
+
@case_ignore =
|
12
|
+
case case_mode
|
13
|
+
when :case_match then false
|
14
|
+
when :case_ignore then true
|
15
|
+
else
|
16
|
+
raise ArgumentError,
|
17
|
+
"#{self.class.name}.new: second argument must be :case_ignore or :case_match, but #{case_mode} was given."
|
18
|
+
end
|
19
|
+
|
20
|
+
if @case_ignore
|
21
|
+
super() do |h, k|
|
22
|
+
h[k] = h.fetch k.downcase, default
|
23
|
+
end
|
24
|
+
else
|
25
|
+
super default
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def include? word
|
30
|
+
self[word] if @case_ignore
|
31
|
+
has_key? word
|
32
|
+
end
|
33
|
+
|
34
|
+
def add words, kind = true
|
35
|
+
words.each do |word|
|
36
|
+
self[mind_case(word)] = kind
|
37
|
+
end
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
alias words keys
|
42
|
+
|
43
|
+
def case_ignore?
|
44
|
+
@case_mode
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def mind_case word
|
49
|
+
if @case_ignore
|
50
|
+
word.downcase
|
51
|
+
else
|
52
|
+
word.dup
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# vim:sw=2:ts=2:et:tw=78
|