coderay 0.7.1.147 → 0.7.2.165
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/bin/coderay +54 -56
- data/demo/suite.rb +54 -54
- data/lib/coderay.rb +187 -187
- data/lib/coderay/duo.rb +29 -29
- data/lib/coderay/encoder.rb +173 -173
- data/lib/coderay/encoders/_map.rb +8 -8
- data/lib/coderay/encoders/count.rb +21 -21
- data/lib/coderay/encoders/debug.rb +46 -46
- data/lib/coderay/encoders/div.rb +20 -20
- data/lib/coderay/encoders/html.rb +249 -245
- data/lib/coderay/encoders/html/classes.rb +73 -73
- data/lib/coderay/encoders/html/css.rb +65 -65
- data/lib/coderay/encoders/html/numerization.rb +122 -122
- data/lib/coderay/encoders/html/output.rb +195 -195
- data/lib/coderay/encoders/null.rb +26 -26
- data/lib/coderay/encoders/page.rb +21 -21
- data/lib/coderay/encoders/span.rb +20 -20
- data/lib/coderay/encoders/statistic.rb +81 -81
- data/lib/coderay/encoders/text.rb +33 -33
- data/lib/coderay/encoders/tokens.rb +44 -44
- data/lib/coderay/encoders/xml.rb +71 -71
- data/lib/coderay/encoders/yaml.rb +22 -22
- data/lib/coderay/helpers/filetype.rb +152 -153
- data/lib/coderay/helpers/gzip_simple.rb +67 -68
- data/lib/coderay/helpers/plugin.rb +297 -297
- data/lib/coderay/helpers/word_list.rb +46 -47
- data/lib/coderay/scanner.rb +238 -238
- data/lib/coderay/scanners/_map.rb +15 -14
- data/lib/coderay/scanners/c.rb +163 -155
- data/lib/coderay/scanners/delphi.rb +131 -129
- data/lib/coderay/scanners/html.rb +174 -167
- data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
- data/lib/coderay/scanners/plaintext.rb +15 -15
- data/lib/coderay/scanners/rhtml.rb +73 -65
- data/lib/coderay/scanners/ruby.rb +404 -397
- data/lib/coderay/scanners/ruby/patterns.rb +216 -216
- data/lib/coderay/scanners/xml.rb +18 -18
- data/lib/coderay/style.rb +20 -20
- data/lib/coderay/styles/_map.rb +3 -3
- data/lib/coderay/styles/cycnus.rb +18 -18
- data/lib/coderay/styles/murphy.rb +18 -18
- data/lib/coderay/tokens.rb +322 -322
- metadata +86 -86
- data/lib/coderay/scanners/nitro_html.rb +0 -125
- data/lib/coderay/scanners/yaml.rb +0 -85
@@ -1,22 +1,22 @@
|
|
1
|
-
module CodeRay
|
2
|
-
module Encoders
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
#
|
12
|
+
# lang = FileType[ARGV.first]
|
13
13
|
#
|
14
|
-
#
|
15
|
-
#
|
14
|
+
# # return :plaintext if the file type is unknown
|
15
|
+
# lang = FileType.fetch ARGV.first, :plaintext
|
16
16
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
17
|
+
# # try the shebang line, too
|
18
|
+
# lang = FileType.fetch ARGV.first, :plaintext, true
|
20
19
|
module FileType
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
104
|
-
|
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
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|