coderay 0.7.2.168 → 0.7.2.176
Sign up to get free protection for your applications and to get access to all the features.
- data/demo/demo_server.rb +1 -1
- data/lib/coderay/encoders/html/classes.rb +5 -2
- data/lib/coderay/helpers/filetype.rb +180 -180
- data/lib/coderay/helpers/gzip_simple.rb +122 -122
- data/lib/coderay/helpers/plugin.rb +326 -326
- data/lib/coderay/helpers/word_list.rb +106 -106
- data/lib/coderay/scanner.rb +2 -2
- data/lib/coderay/scanners/delphi.rb +2 -1
- data/lib/coderay/scanners/html.rb +3 -0
- data/lib/coderay/scanners/nitro_xhtml.rb +3 -0
- data/lib/coderay/scanners/ruby.rb +0 -40
- data/lib/coderay/styles/_map.rb +7 -7
- data/lib/coderay/styles/cycnus.rb +125 -125
- data/lib/coderay/styles/murphy.rb +119 -119
- data/lib/coderay/tokens.rb +48 -2
- metadata +37 -37
data/demo/demo_server.rb
CHANGED
@@ -3,7 +3,10 @@ module Encoders
|
|
3
3
|
|
4
4
|
class HTML
|
5
5
|
|
6
|
-
ClassOfKind =
|
6
|
+
ClassOfKind = Hash.new do |h, k|
|
7
|
+
h[k] = k.to_s
|
8
|
+
end
|
9
|
+
ClassOfKind.update with = {
|
7
10
|
:attribute_name => 'an',
|
8
11
|
:attribute_name_fat => 'af',
|
9
12
|
:attribute_value => 'av',
|
@@ -65,7 +68,7 @@ module Encoders
|
|
65
68
|
ClassOfKind[:open] = ClassOfKind[:close] = ClassOfKind[:delimiter]
|
66
69
|
ClassOfKind[:nesting_delimiter] = ClassOfKind[:delimiter]
|
67
70
|
ClassOfKind[:escape] = ClassOfKind[:delimiter]
|
68
|
-
ClassOfKind.default = ClassOfKind[:error] or raise 'no class found for :error!'
|
71
|
+
#ClassOfKind.default = ClassOfKind[:error] or raise 'no class found for :error!'
|
69
72
|
|
70
73
|
end
|
71
74
|
|
@@ -1,180 +1,180 @@
|
|
1
|
-
# =FileType
|
2
|
-
#
|
3
|
-
# A simple filetype recognizer
|
4
|
-
#
|
5
|
-
# Author: murphy (mail to murphy cYcnus de)
|
6
|
-
#
|
7
|
-
# Version: 0.1 (2005.september.1)
|
8
|
-
#
|
9
|
-
# == Documentation
|
10
|
-
#
|
11
|
-
# # determine the type of the given
|
12
|
-
# lang = FileType[ARGV.first]
|
13
|
-
#
|
14
|
-
# # return :plaintext if the file type is unknown
|
15
|
-
# lang = FileType.fetch ARGV.first, :plaintext
|
16
|
-
#
|
17
|
-
# # try the shebang line, too
|
18
|
-
# lang = FileType.fetch ARGV.first, :plaintext, true
|
19
|
-
module FileType
|
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
|
-
}
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
if $0 == __FILE__
|
102
|
-
$VERBOSE = true
|
103
|
-
eval DATA.read, nil, $0, __LINE__+4
|
104
|
-
end
|
105
|
-
|
106
|
-
__END__
|
107
|
-
|
108
|
-
require 'test/unit'
|
109
|
-
|
110
|
-
class TC_FileType < Test::Unit::TestCase
|
111
|
-
|
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
|
179
|
-
|
180
|
-
end
|
1
|
+
# =FileType
|
2
|
+
#
|
3
|
+
# A simple filetype recognizer
|
4
|
+
#
|
5
|
+
# Author: murphy (mail to murphy cYcnus de)
|
6
|
+
#
|
7
|
+
# Version: 0.1 (2005.september.1)
|
8
|
+
#
|
9
|
+
# == Documentation
|
10
|
+
#
|
11
|
+
# # determine the type of the given
|
12
|
+
# lang = FileType[ARGV.first]
|
13
|
+
#
|
14
|
+
# # return :plaintext if the file type is unknown
|
15
|
+
# lang = FileType.fetch ARGV.first, :plaintext
|
16
|
+
#
|
17
|
+
# # try the shebang line, too
|
18
|
+
# lang = FileType.fetch ARGV.first, :plaintext, true
|
19
|
+
module FileType
|
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
|
+
}
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
if $0 == __FILE__
|
102
|
+
$VERBOSE = true
|
103
|
+
eval DATA.read, nil, $0, __LINE__+4
|
104
|
+
end
|
105
|
+
|
106
|
+
__END__
|
107
|
+
|
108
|
+
require 'test/unit'
|
109
|
+
|
110
|
+
class TC_FileType < Test::Unit::TestCase
|
111
|
+
|
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
|
179
|
+
|
180
|
+
end
|
@@ -1,122 +1,122 @@
|
|
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
|
-
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
|
47
|
-
end
|
48
|
-
|
49
|
-
# String extensions to use the GZip module.
|
50
|
-
#
|
51
|
-
# The methods gzip and gunzip provide an even more simple
|
52
|
-
# interface to the ZLib:
|
53
|
-
#
|
54
|
-
# # create a big string
|
55
|
-
# x = 'a' * 1000
|
56
|
-
#
|
57
|
-
# # zip it
|
58
|
-
# x_gz = x.gzip
|
59
|
-
#
|
60
|
-
# # test the result
|
61
|
-
# puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
|
62
|
-
# #-> Zipped 1000 bytes to 19 bytes.
|
63
|
-
#
|
64
|
-
# # unzipping works
|
65
|
-
# p x_gz.gunzip == x #-> true
|
66
|
-
class String
|
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
|
88
|
-
end
|
89
|
-
|
90
|
-
if $0 == __FILE__
|
91
|
-
eval DATA.read, nil, $0, __LINE__+4
|
92
|
-
end
|
93
|
-
|
94
|
-
__END__
|
95
|
-
#CODE
|
96
|
-
|
97
|
-
# Testing / Benchmark
|
98
|
-
x = 'a' * 1000
|
99
|
-
x_gz = x.gzip
|
100
|
-
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
|
101
|
-
p x_gz.gunzip == x #-> true
|
102
|
-
|
103
|
-
require 'benchmark'
|
104
|
-
|
105
|
-
INFO = 'packed to %0.3f%%' # :nodoc:
|
106
|
-
|
107
|
-
x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
|
108
|
-
Benchmark.bm(10) do |bm|
|
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
|
122
|
-
end
|
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
|
+
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
|
47
|
+
end
|
48
|
+
|
49
|
+
# String extensions to use the GZip module.
|
50
|
+
#
|
51
|
+
# The methods gzip and gunzip provide an even more simple
|
52
|
+
# interface to the ZLib:
|
53
|
+
#
|
54
|
+
# # create a big string
|
55
|
+
# x = 'a' * 1000
|
56
|
+
#
|
57
|
+
# # zip it
|
58
|
+
# x_gz = x.gzip
|
59
|
+
#
|
60
|
+
# # test the result
|
61
|
+
# puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
|
62
|
+
# #-> Zipped 1000 bytes to 19 bytes.
|
63
|
+
#
|
64
|
+
# # unzipping works
|
65
|
+
# p x_gz.gunzip == x #-> true
|
66
|
+
class String
|
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
|
88
|
+
end
|
89
|
+
|
90
|
+
if $0 == __FILE__
|
91
|
+
eval DATA.read, nil, $0, __LINE__+4
|
92
|
+
end
|
93
|
+
|
94
|
+
__END__
|
95
|
+
#CODE
|
96
|
+
|
97
|
+
# Testing / Benchmark
|
98
|
+
x = 'a' * 1000
|
99
|
+
x_gz = x.gzip
|
100
|
+
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
|
101
|
+
p x_gz.gunzip == x #-> true
|
102
|
+
|
103
|
+
require 'benchmark'
|
104
|
+
|
105
|
+
INFO = 'packed to %0.3f%%' # :nodoc:
|
106
|
+
|
107
|
+
x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
|
108
|
+
Benchmark.bm(10) do |bm|
|
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
|
122
|
+
end
|