# -*- ruby encoding: utf-8 -*- require 'rubygems' require 'hoe' Hoe.plugin :doofus Hoe.plugin :gemspec Hoe.plugin :rubyforge Hoe.plugin :git Hoe.plugin :minitest spec = Hoe.spec 'mime-types' do self.rubyforge_name = self.name developer('Austin Ziegler', 'austin@rubyforge.org') self.url = "http://mime-types.rubyforge.org/" self.remote_rdoc_dir = '.' self.rsync_args << ' --exclude=statsvn/' self.history_file = 'History.rdoc' self.readme_file = 'README.rdoc' self.extra_rdoc_files = FileList["*.rdoc"].to_a self.extra_dev_deps << ['nokogiri', '~> 1.5'] self.extra_dev_deps << ['minitest', '~> 2.0'] self.extra_dev_deps << ['hoe-doofus', '~> 1.0'] self.extra_dev_deps << ['hoe-gemspec', '~> 1.0'] self.extra_dev_deps << ['hoe-git', '~> 1.0'] self.extra_dev_deps << ['hoe-seattlerb', '~> 1.0'] end namespace :mime do desc "Download the current MIME type registrations from IANA." task :iana, :save, :destination do |t, args| save_type = (args.save || :text).to_sym case save_type when :text, :both, :html nil else raise "Unknown save type provided. Must be one of text, both, or html." end destination = args.destination || "type-lists" require 'open-uri' require 'nokogiri' require 'cgi' class IANAParser include Comparable INDEX = %q(http://www.iana.org/assignments/media-types/) CONTACT_PEOPLE = %r{http://www.iana.org/assignments/contact-people.html?#(.*)} RFC_EDITOR = %r{http://www.rfc-editor.org/rfc/rfc(\d+).txt} IETF_RFC = %r{http://www.ietf.org/rfc/rfc(\d+).txt} IETF_RFC_TOOLS = %r{http://tools.ietf.org/html/rfc(\d+)} class << self def load_index @types ||= {} Nokogiri::HTML(open(INDEX) { |f| f.read }).xpath('//p/a').each do |tag| href_match = %r{^/assignments/media-types/(.+)/$}.match(tag['href']) next if href_match.nil? type = href_match.captures[0] @types[tag.content] = IANAParser.new(tag.content, type) end end attr_reader :types end def initialize(name, type) @name = name @type = type @url = File.join(INDEX, @type) end attr_reader :name attr_reader :type attr_reader :url attr_reader :html def download(name = nil) @html = Nokogiri::HTML(open(name || @url) { |f| f.read }) end def save_html File.open("#@name.html", "wb") { |w| w.write @html } end def <=>(o) self.name <=> o.name end def parse nodes = html.xpath("//table//table//tr") # How many