mime-types 1.16 → 3.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,316 +1,229 @@
1
- #! /usr/bin/env rake
2
- #--
3
- # MIME::Types
4
- # A Ruby implementation of a MIME Types information library. Based in spirit
5
- # on the Perl MIME::Types information library by Mark Overmeer.
6
- # http://rubyforge.org/projects/mime-types/
7
- #
8
- # Licensed under the Ruby disjunctive licence with the GNU GPL or the Perl
9
- # Artistic licence. See Licence.txt for more information.
10
- #
11
- # Copyright 2003 - 2009 Austin Ziegler
12
- #++
13
-
14
- require 'rubygems'
15
- require 'hoe'
16
-
17
- $LOAD_PATH.unshift('lib')
18
-
19
- require 'mime/types'
20
-
21
- PKG_NAME = 'mime-types'
22
- PKG_VERSION = MIME::Types::VERSION
23
- PKG_DIST = "#{PKG_NAME}-#{PKG_VERSION}"
24
- PKG_TAR = "pkg/#{PKG_DIST}.tar.gz"
25
- MANIFEST = File.read("Manifest.txt").split
26
-
27
- hoe = Hoe.new PKG_NAME, PKG_VERSION do |p|
28
- p.rubyforge_name = PKG_NAME
29
- # This is a lie because I will continue to use Archive::Tar::Minitar.
30
- p.need_tar = false
31
- # need_zip - Should package create a zipfile? [default: false]
32
-
33
- p.author = [ "Austin Ziegler" ]
34
- p.email = %W(austin@rubyforge.org)
35
- p.url = "http://mime-types.rubyforge.org/"
36
- p.summary = %q{Manages a MIME Content-Type database that will return the Content-Type for a given filename.}
37
- p.changes = p.paragraphs_of("History.txt", 0..0).join("\n\n")
38
- p.description = p.paragraphs_of("README.txt", 1..1).join("\n\n")
39
-
40
- p.extra_dev_deps << %w(archive-tar-minitar ~>0.5)
41
- p.extra_dev_deps << %w(nokogiri ~>1.2)
42
- p.extra_dev_deps << %w(rcov ~>0.8)
43
-
44
- p.clean_globs << "coverage"
45
-
46
- p.spec_extras[:extra_rdoc_files] = MANIFEST.grep(/txt$/) - ["Manifest.txt"]
1
+ require "rubygems"
2
+ require "hoe"
3
+ require "rake/clean"
4
+
5
+ Hoe.plugin :cov
6
+ Hoe.plugin :doofus
7
+ Hoe.plugin :gemspec2
8
+ Hoe.plugin :git2
9
+ Hoe.plugin :minitest
10
+ Hoe.plugin :rubygems
11
+
12
+ spec = Hoe.spec "mime-types" do
13
+ developer("Austin Ziegler", "halostatue@gmail.com")
14
+
15
+ self.history_file = "History.md"
16
+ self.readme_file = "README.rdoc"
17
+
18
+ license "MIT"
19
+
20
+ require_ruby_version ">= 2.0"
21
+
22
+ spec_extras[:metadata] = ->(val) { val["rubygems_mfa_required"] = "true" }
23
+
24
+ extra_deps << ["mime-types-data", "~> 3.2015"]
25
+
26
+ extra_dev_deps << ["hoe", ">= 3.0", "< 5"]
27
+ extra_dev_deps << ["hoe-doofus", "~> 1.0"]
28
+ extra_dev_deps << ["hoe-gemspec2", "~> 1.1"]
29
+ extra_dev_deps << ["hoe-git2", "~> 1.7"]
30
+ extra_dev_deps << ["hoe-rubygems", "~> 1.0"]
31
+ extra_dev_deps << ["minitest", "~> 5.0"]
32
+ extra_dev_deps << ["minitest-autotest", "~> 1.0"]
33
+ extra_dev_deps << ["minitest-focus", "~> 1.0"]
34
+ extra_dev_deps << ["minitest-hooks", "~> 1.4"]
35
+ extra_dev_deps << ["rake", ">= 10.0", "< 14.0"]
36
+ extra_dev_deps << ["standard", "~> 1.0"]
47
37
  end
48
38
 
49
- begin
50
- require 'rcov/rcovtask'
51
- Rcov::RcovTask.new do |t|
52
- t.libs << 'test'
53
- t.test_files = hoe.test_files
54
- t.verbose = true
39
+ namespace :benchmark do
40
+ task :support do
41
+ %w[lib support].each { |path|
42
+ $LOAD_PATH.unshift(File.join(Rake.application.original_dir, path))
43
+ }
55
44
  end
56
- rescue LoadError
57
- puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
58
- end
59
-
60
- =begin
61
- require 'cucumber/rake/task'
62
- Cucumber::Rake::Task.new(:features)
63
- rescue LoadError
64
- puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
65
- =end
66
-
67
- desc "Build a MIME::Types .tar.gz distribution."
68
- task :tar => [ PKG_TAR ]
69
- file PKG_TAR => [ :test ] do |t|
70
- require 'archive/tar/minitar'
71
- require 'zlib'
72
- files = MANIFEST.map { |f|
73
- fn = File.join(PKG_DIST, f)
74
- tm = File.stat(f).mtime
75
-
76
- if File.directory?(f)
77
- { :name => fn, :mode => 0755, :dir => true, :mtime => tm }
78
- else
79
- mode = if f =~ %r{^bin}
80
- 0755
81
- else
82
- 0644
83
- end
84
- data = File.read(f)
85
- { :name => fn, :mode => mode, :data => data, :size => data.size,
86
- :mtime => tm }
87
- end
88
- }
89
45
 
90
- begin
91
- unless File.directory?(File.dirname(t.name))
92
- require 'fileutils'
93
- FileUtils.mkdir_p File.dirname(t.name)
94
- end
95
- tf = File.open(t.name, 'wb')
96
- gz = Zlib::GzipWriter.new(tf)
97
- tw = Archive::Tar::Minitar::Writer.new(gz)
98
-
99
- files.each do |entry|
100
- if entry[:dir]
101
- tw.mkdir(entry[:name], entry)
102
- else
103
- tw.add_file_simple(entry[:name], entry) { |os|
104
- os.write(entry[:data])
105
- }
106
- end
107
- end
108
- ensure
109
- tw.close if tw
110
- gz.close if gz
46
+ desc "Benchmark Load Times"
47
+ task :load, [:repeats] => "benchmark:support" do |_, args|
48
+ require "benchmarks/load"
49
+ Benchmarks::Load.report(
50
+ File.join(Rake.application.original_dir, "lib"),
51
+ args.repeats
52
+ )
111
53
  end
112
- end
113
- task :package => [ PKG_TAR ]
114
-
115
- desc "Build the manifest file from the current set of files."
116
- task :build_manifest do |t|
117
- require 'find'
118
-
119
- hoerc = File.join(File.dirname(__FILE__), ".hoerc")
120
- hoerc = File.open(hoerc, "rb") { |f| f.read }
121
- hoerc = YAML::load(hoerc)
122
54
 
123
- paths = []
124
- Find.find(".") do |path|
125
- next if File.directory?(path) || path =~ hoerc["exclude"]
126
- paths << path.sub(%r{^\./}, '')
55
+ desc "Allocation counts"
56
+ task :allocations, [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
57
+ require "benchmarks/load_allocations"
58
+ Benchmarks::LoadAllocations.report(
59
+ top_x: args.top_x,
60
+ mime_types_only: args.mime_types_only
61
+ )
127
62
  end
128
63
 
129
- paths = paths.sort.join("\n")
130
-
131
- File.open("Manifest.txt", "w") do |f|
132
- f.puts paths
64
+ desc "Columnar allocation counts"
65
+ task "allocations:columnar", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
66
+ require "benchmarks/load_allocations"
67
+ Benchmarks::LoadAllocations.report(
68
+ columnar: true,
69
+ top_x: args.top_x,
70
+ mime_types_only: args.mime_types_only
71
+ )
133
72
  end
134
73
 
135
- puts paths
136
- end
137
-
138
- desc "Download the current MIME type registrations from IANA."
139
- task :iana, :save, :destination do |t, args|
140
- save_type = args.save || :text
141
- save_type = save_type.to_sym
142
-
143
- case save_type
144
- when :text, :both, :html
145
- nil
146
- else
147
- raise "Unknown save type provided. Must be one of text, both, or html."
74
+ desc "Columnar allocation counts (full load)"
75
+ task "allocations:columnar:full", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
76
+ require "benchmarks/load_allocations"
77
+ Benchmarks::LoadAllocations.report(
78
+ columnar: true,
79
+ top_x: args.top_x,
80
+ mime_types_only: args.mime_types_only,
81
+ full: true
82
+ )
148
83
  end
149
84
 
150
- destination = args.destination || "type-lists"
151
-
152
- require 'open-uri'
153
- require 'nokogiri'
154
- require 'cgi'
155
-
156
- class IANAParser
157
- include Comparable
158
-
159
- INDEX = %q(http://www.iana.org/assignments/media-types/)
160
- CONTACT_PEOPLE = %r{http://www.iana.org/assignments/contact-people.html?#(.*)}
161
- RFC_EDITOR = %r{http://www.rfc-editor.org/rfc/rfc(\d+).txt}
162
- IETF_RFC = %r{http://www.ietf.org/rfc/rfc(\d+).txt}
163
- IETF_RFC_TOOLS = %r{http://tools.ietf.org/html/rfc(\d+)}
85
+ desc "Memory profiler"
86
+ task :memory, [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
87
+ require "benchmarks/memory_profiler"
88
+ Benchmarks::ProfileMemory.report(
89
+ mime_types_only: args.mime_types_only,
90
+ top_x: args.top_x
91
+ )
92
+ end
164
93
 
165
- class << self
166
- def load_index
167
- @types ||= {}
94
+ desc "Columnar memory profiler"
95
+ task "memory:columnar", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
96
+ require "benchmarks/memory_profiler"
97
+ Benchmarks::ProfileMemory.report(
98
+ columnar: true,
99
+ mime_types_only: args.mime_types_only,
100
+ top_x: args.top_x
101
+ )
102
+ end
168
103
 
169
- Nokogiri::HTML(open(INDEX) { |f| f.read }).xpath('//p/a').each do |tag|
170
- href_match = %r{^/assignments/media-types/(.+)/$}.match(tag['href'])
171
- next if href_match.nil?
172
- type = href_match.captures[0]
173
- @types[tag.content] = IANAParser.new(tag.content, type)
174
- end
175
- end
104
+ desc "Columnar allocation counts (full load)"
105
+ task "memory:columnar:full", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
106
+ require "benchmarks/memory_profiler"
107
+ Benchmarks::ProfileMemory.report(
108
+ columnar: true,
109
+ full: true,
110
+ top_x: args.top_x,
111
+ mime_types_only: args.mime_types_only
112
+ )
113
+ end
176
114
 
177
- attr_reader :types
178
- end
115
+ desc "Object counts"
116
+ task objects: "benchmark:support" do
117
+ require "benchmarks/object_counts"
118
+ Benchmarks::ObjectCounts.report
119
+ end
179
120
 
180
- def initialize(name, type)
181
- @name = name
182
- @type = type
183
- @url = File.join(INDEX, @type)
184
- end
121
+ desc "Columnar object counts"
122
+ task "objects:columnar" => "benchmark:support" do
123
+ require "benchmarks/object_counts"
124
+ Benchmarks::ObjectCounts.report(columnar: true)
125
+ end
185
126
 
186
- attr_reader :name
187
- attr_reader :type
188
- attr_reader :url
189
- attr_reader :html
127
+ desc "Columnar object counts (full load)"
128
+ task "objects:columnar:full" => "benchmark:support" do
129
+ require "benchmarks/object_counts"
130
+ Benchmarks::ObjectCounts.report(columnar: true, full: true)
131
+ end
132
+ end
190
133
 
191
- def download(name = nil)
192
- @html = Nokogiri::HTML(open(name || @url) { |f| f.read })
193
- end
134
+ namespace :profile do
135
+ directory "tmp/profile"
136
+
137
+ CLEAN.add "tmp"
138
+
139
+ def ruby_prof(script)
140
+ require "pathname"
141
+ output = Pathname("tmp/profile").join(script)
142
+ output.mkpath
143
+ script = Pathname("support/profile").join("#{script}.rb")
144
+
145
+ args = [
146
+ "-W0",
147
+ "-Ilib",
148
+ "-S", "ruby-prof",
149
+ "-R", "mime/types",
150
+ "-s", "self",
151
+ "-p", "multi",
152
+ "-f", output.to_s,
153
+ script.to_s
154
+ ]
155
+ ruby args.join(" ")
156
+ end
194
157
 
195
- def save_html
196
- File.open("#@name.html", "wb") { |w| w.write @html }
197
- end
158
+ task full: "tmp/profile" do
159
+ ruby_prof "full"
160
+ end
198
161
 
199
- def <=>(o)
200
- self.name <=> o.name
201
- end
162
+ task columnar: :support do
163
+ ruby_prof "columnar"
164
+ end
202
165
 
203
- def parse
204
- nodes = html.xpath("//table//table//tr")
205
-
206
- # How many <td> children does the first node have?
207
- node_count = nodes.first.children.select { |node| node.elem? }.size
208
-
209
- @mime_types = nodes.map do |node|
210
- next if node == nodes.first
211
- elems = node.children.select { |n| n.elem? }
212
- next if elems.size.zero?
213
- raise "size mismatch #{elems.size} != #{node_count}" if node_count != elems.size
214
-
215
- case elems.size
216
- when 3
217
- subtype_index = 1
218
- refnode_index = 2
219
- when 4
220
- subtype_index = 1
221
- refnode_index = 3
222
- else
223
- raise "Unknown element size."
224
- end
225
-
226
- subtype = elems[subtype_index].content.chomp.strip
227
- refnodes = elems[refnode_index].children.select { |n| n.elem? }.map { |ref|
228
- case ref['href']
229
- when CONTACT_PEOPLE
230
- tag = CGI::unescape($1).chomp.strip
231
- if tag == ref.content
232
- "[#{ref.content}]"
233
- else
234
- "[#{ref.content}=#{tag}]"
235
- end
236
- when RFC_EDITOR, IETF_RFC, IETF_RFC_TOOLS
237
- "RFC#$1"
238
- when %r{(https?://.*)}
239
- "{#{ref.content}=#$1}"
240
- else
241
- ref
242
- end
243
- }
244
- refs = refnodes.join(',')
245
-
246
- "#@type/#{subtype} 'IANA,#{refs}"
247
- end.compact
248
-
249
- @mime_types
250
- end
166
+ task "columnar:full" => :support do
167
+ ruby_prof "columnar_full"
168
+ end
169
+ end
251
170
 
252
- def save_text
253
- File.open("#@name.txt", "wb") { |w| w.write @mime_types.join("\n") }
171
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0")
172
+ namespace :test do
173
+ desc "Run test coverage"
174
+ task :coverage do
175
+ spec.test_prelude = [
176
+ 'require "simplecov"',
177
+ 'SimpleCov.start("test_frameworks") { command_name "Minitest" }',
178
+ 'gem "minitest"'
179
+ ].join("; ")
180
+ Rake::Task["test"].execute
254
181
  end
255
182
  end
183
+ end
256
184
 
257
- puts "Downloading index of MIME types from #{IANAParser::INDEX}."
258
- IANAParser.load_index
185
+ namespace :convert do
186
+ namespace :docs do
187
+ task :setup do
188
+ gem "rdoc"
189
+ require "rdoc/rdoc"
190
+ @doc_converter ||= RDoc::Markup::ToMarkdown.new
191
+ end
259
192
 
260
- require 'fileutils'
261
- FileUtils.mkdir_p destination
262
- Dir.chdir destination do
263
- IANAParser.types.values.sort.each do |parser|
264
- next if parser.name == "example" or parser.name == "mime"
265
- puts "Downloading #{parser.name} from #{parser.url}"
266
- parser.download
193
+ FileList["*.rdoc"].each do |name|
194
+ rdoc = name
195
+ mark = "#{File.basename(name, ".rdoc")}.md"
267
196
 
268
- if :html == save_type || :both == save_type
269
- puts "Saving #{parser.name}.html"
270
- parser.save_html
197
+ file mark => [rdoc, :setup] do |t|
198
+ puts "#{rdoc} => #{mark}"
199
+ File.binwrite(t.name, @doc_converter.convert(IO.read(t.prerequisites.first)))
271
200
  end
272
201
 
273
- puts "Parsing #{parser.name} HTML"
274
- parser.parse
202
+ CLEAN.add mark
275
203
 
276
- if :text == save_type || :both == save_type
277
- puts "Saving #{parser.name}.txt"
278
- parser.save_text
279
- end
204
+ task run: [mark]
280
205
  end
281
206
  end
282
- end
283
207
 
284
- desc "Shows known MIME type sources."
285
- task :mime_type_sources do
286
- puts <<-EOS
287
- http://www.ltsw.se/knbase/internet/mime.htp
288
- http://www.webmaster-toolkit.com/mime-types.shtml
289
- http://plugindoc.mozdev.org/winmime.php
290
- http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
291
- http://www.feedforall.com/mime-types.htm
292
- http://www.iana.org/assignments/media-types/
293
- EOS
208
+ desc "Convert documentation from RDoc to Markdown"
209
+ task docs: "convert:docs:run"
294
210
  end
295
211
 
296
- desc "Validate the RubyGem spec for GitHub."
297
- task :github_validate_spec do |t|
298
- require 'yaml'
299
-
300
- require 'rubygems/specification'
301
- data = File.read("#{PKG_NAME}.gemspec")
302
- spec = nil
303
-
304
- if data !~ %r{!ruby/object:Gem::Specification}
305
- code = "$SAFE = 3\n#{data}"
306
- p code.split($/)[44]
307
- Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
308
- else
309
- spec = YAML.load(data)
212
+ namespace :deps do
213
+ task :top, [:number] => "benchmark:support" do |_, args|
214
+ require "deps"
215
+ Deps.run(args)
310
216
  end
217
+ end
311
218
 
312
- spec.validate
313
-
314
- puts spec
315
- puts "OK"
219
+ task :console do
220
+ arguments = %w[irb]
221
+ arguments.push(*spec.spec.require_paths.map { |dir| "-I#{dir}" })
222
+ arguments.push("-r#{spec.spec.name.gsub("-", File::SEPARATOR)}")
223
+ unless system(*arguments)
224
+ error "Command failed: #{show_command}"
225
+ abort
226
+ end
316
227
  end
228
+
229
+ # vim: syntax=ruby
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mime/type"
4
+
5
+ # A version of MIME::Type that works hand-in-hand with a MIME::Types::Columnar
6
+ # container to load data by columns.
7
+ #
8
+ # When a field is has not yet been loaded, that data will be loaded for all
9
+ # types in the container before forwarding the message to MIME::Type.
10
+ #
11
+ # More information can be found in MIME::Types::Columnar.
12
+ #
13
+ # MIME::Type::Columnar is *not* intended to be created except by
14
+ # MIME::Types::Columnar containers.
15
+ class MIME::Type::Columnar < MIME::Type
16
+ def initialize(container, content_type, extensions) # :nodoc:
17
+ @container = container
18
+ self.content_type = content_type
19
+ self.extensions = extensions
20
+ end
21
+
22
+ def self.column(*methods, file: nil) # :nodoc:
23
+ file ||= methods.first
24
+
25
+ file_method = :"load_#{file}"
26
+ methods.each do |m|
27
+ define_method m do |*args|
28
+ @container.send(file_method)
29
+ super(*args)
30
+ end
31
+ end
32
+ end
33
+
34
+ column :friendly
35
+ column :encoding, :encoding=
36
+ column :docs, :docs=
37
+ column :preferred_extension, :preferred_extension=
38
+ column :obsolete, :obsolete=, :obsolete?, :registered, :registered=, :registered?, :signature, :signature=,
39
+ :signature?, :provisional, :provisional=, :provisional?, file: "flags"
40
+ column :xrefs, :xrefs=, :xref_urls
41
+ column :use_instead, :use_instead=
42
+
43
+ def encode_with(coder) # :nodoc:
44
+ @container.send(:load_friendly)
45
+ @container.send(:load_encoding)
46
+ @container.send(:load_docs)
47
+ @container.send(:load_flags)
48
+ @container.send(:load_use_instead)
49
+ @container.send(:load_xrefs)
50
+ @container.send(:load_preferred_extension)
51
+ super
52
+ end
53
+
54
+ class << self
55
+ undef column
56
+ end
57
+ end