Commit 09cb30d1 by minad

support mime type comments

parent e51cc73f
......@@ -16,15 +16,22 @@ class MimeMagic
# Add custom mime type. Arguments:
# * <i>type</i>: Mime type
# * <i>extensions</i>: String list of file extensions
# * <i>parents</i>: String list of parent mime types
# * <i>magics</i>: Mime magic specification array
def self.add(type, extensions, parents, *magics)
TYPES[type] = [extensions, parents, magics]
# * <i>options</i>: Options hash
#
# Option keys:
# * <i>:extensions</i>: String list or single string of file extensions
# * <i>:parents</i>: String list or single string of parent mime types
# * <i>:magic</i>: Mime magic specification
# * <i>:comment</i>: Comment string
def self.add(type, options = {})
extensions = [options[:extensions]].flatten.compact
TYPES[type] = [extensions,
[options[:parents]].flatten.compact,
options[:comment]]
extensions.each do |ext|
EXTENSIONS[ext] = type
end
MAGIC.unshift [type, magics] if magics
MAGIC.unshift [type, [options[:magic]].flatten.compact] if options[:magic]
end
# Returns true if type is a text format
......@@ -52,9 +59,14 @@ class MimeMagic
TYPES.key?(type) ? TYPES[type][0] : []
end
# Get mime comment
def comment
(TYPES.key?(type) ? TYPES[type][2] : nil).to_s
end
# Lookup mime type by file extension
def self.by_extension(ext)
ext = ext.downcase
ext = ext.to_s.downcase
mime = EXTENSIONS[ext] || (ext[0..0] == '.' && EXTENSIONS[ext[1..-1]])
mime ? new(mime) : nil
end
......
......@@ -61,6 +61,7 @@ extensions = {}
types = {}
magics = []
(doc/'mime-info/mime-type').each do |mime|
comments = Hash[*(mime/'comment').map {|comment| [comment['lang'], comment.inner_text] }.flatten]
type = mime['type']
subclass = (mime/'sub-class-of').map{|x| x['type']}
exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
......@@ -73,7 +74,7 @@ magics = []
exts.each{|x|
extensions[x] = type if !extensions.include?(x)
}
types[type] = [exts,subclass]
types[type] = [exts,subclass,comments[nil]]
end
end
......@@ -91,7 +92,8 @@ puts " TYPES = {"
types.keys.sort.each do |key|
exts = types[key][0].sort.join(' ')
parents = types[key][1].sort.join(' ')
puts " '#{key}' => [%w(#{exts}), %w(#{parents})],"
comment = types[key][2].inspect
puts " '#{key}' => [%w(#{exts}), %w(#{parents}), #{comment}],"
end
puts " }"
puts " MAGIC = ["
......
......@@ -23,8 +23,13 @@ describe 'MimeMagic' do
MimeMagic.new('text/html').extensions.should.equal %w(htm html)
end
it 'should have comment' do
MimeMagic.new('text/html').comment.should.equal 'HTML document'
end
it 'should recognize extensions' do
MimeMagic.by_extension('html').to_s.should.equal 'text/html'
MimeMagic.by_extension(:html).to_s.should.equal 'text/html'
MimeMagic.by_extension('rb').to_s.should.equal 'application/x-ruby'
MimeMagic.by_extension('crazy').should.equal nil
MimeMagic.by_extension('').should.equal nil
......@@ -39,9 +44,10 @@ describe 'MimeMagic' do
end
it 'should have add' do
MimeMagic.add('application/mimemagic-test', %w(ext1 ext2), %w(application/xml))
MimeMagic.add('application/mimemagic-test', :extensions => %w(ext1 ext2), :parents => 'application/xml', :comment => 'Comment')
MimeMagic.by_extension('ext1').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_extension('ext2').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_extension('ext2').comment.should.equal 'Comment'
MimeMagic.new('application/mimemagic-test').extensions.should.equal %w(ext1 ext2)
MimeMagic.new('application/mimemagic-test').should.be.child_of 'text/plain'
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment