diff --git a/lib/linguist/mime.rb b/lib/linguist/mime.rb new file mode 100644 index 00000000..a7ce257e --- /dev/null +++ b/lib/linguist/mime.rb @@ -0,0 +1,22 @@ +require 'mime/types' + +module Linguist + module Mime + Special = YAML.load_file(File.expand_path("../special_mime_types.yml", __FILE__)) + + def self.lookup(ext) + ext ||= '' + + guesses = ::MIME::Types.type_for(ext) + orginal_type = guesses.first ? guesses.first.simplified : 'text/plain' + + type = Special[orginal_type] || + Special[ext.sub(/^\./, '')] || + orginal_type + + type += '; charset=utf-8' if type =~ /^text\// + + type + end + end +end diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb index 40618184..4dabed20 100644 --- a/lib/linguist/pathname.rb +++ b/lib/linguist/pathname.rb @@ -1,5 +1,5 @@ require 'linguist/language' -require 'mime/types' +require 'linguist/mime' module Linguist class Pathname @@ -30,10 +30,7 @@ module Linguist end def mime_type - @mime_type ||= begin - guesses = MIME::Types.type_for(extname) - guesses.first ? guesses.first.simplified : 'text/plain' - end + @mime_type ||= Mime.lookup(extname) end def to_s diff --git a/lib/linguist/special_mime_types.yml b/lib/linguist/special_mime_types.yml new file mode 100644 index 00000000..391578a9 --- /dev/null +++ b/lib/linguist/special_mime_types.yml @@ -0,0 +1,63 @@ +# mime types +text/html: text/plain +application/sh: text/plain +application/rdf+xml: text/plain +application/xml: text/plain +application/xhtml+xml: text/plain +application/tex: text/plain + +# binary files +a: application/octet-stream +air: application/octet-stream +blend: application/octet-stream +crx: application/octet-stream +deb: application/octet-stream +dll: application/octet-stream +dmg: application/octet-stream +exe: application/octet-stream +gem: application/octet-stream +graffle: application/octet-stream +gz: application/octet-stream +icns: application/octet-stream +ipa: application/octet-stream +lib: application/octet-stream +mcz: application/octet-stream +mov: application/octet-stream +mp3: application/octet-stream +nib: application/octet-stream +o: application/octet-stream +odp: application/octet-stream +ods: application/octet-stream +odt: application/octet-stream +ogg: application/octet-stream +ogv: application/octet-stream +otf: application/octet-stream +pfx: application/octet-stream +pigx: application/octet-stream +plgx: application/octet-stream +pptx: application/octet-stream +psd: application/octet-stream +sib: application/octet-stream +so: application/octet-stream +spl: application/octet-stream +sqlite3: application/octet-stream +swc: application/octet-stream +swf: application/octet-stream +tar: application/octet-stream +ucode: application/octet-stream +xpi: application/octet-stream +zip: application/octet-stream + +# plaintext +latex: text/plain +ms: text/plain +nc: text/plain +nim: text/plain +ps: text/plain +sc: text/plain +sh: text/plain +src: text/plain +tcl: text/plain +texi: text/plain +texinfo: text/plain +xul: text/plain diff --git a/test/test_mime.rb b/test/test_mime.rb new file mode 100644 index 00000000..b32e137e --- /dev/null +++ b/test/test_mime.rb @@ -0,0 +1,20 @@ +require 'linguist/mime' + +require 'test/unit' + +class TestMime < Test::Unit::TestCase + include Linguist + + def test_lookup + assert_equal 'application/ruby', Mime.lookup(".rb") + assert_equal 'application/javascript', Mime.lookup(".js") + assert_equal 'application/python', Mime.lookup(".py") + + assert_equal 'text/plain; charset=utf-8', Mime.lookup(".kt") + assert_equal 'text/plain; charset=utf-8', Mime.lookup(".html") + assert_equal 'text/plain; charset=utf-8', Mime.lookup(".sh") + assert_equal 'text/plain; charset=utf-8', Mime.lookup(".latex") + + assert_equal 'application/octet-stream', Mime.lookup(".dmg") + end +end diff --git a/test/test_pathname.rb b/test/test_pathname.rb index 525811c5..66aaa80b 100644 --- a/test/test_pathname.rb +++ b/test/test_pathname.rb @@ -55,7 +55,6 @@ class TestPathname < Test::Unit::TestCase assert_equal 'application/ruby', Pathname.new("file.rb").mime_type assert_equal 'application/javascript', Pathname.new("file.js").mime_type assert_equal 'application/python', Pathname.new("itty.py").mime_type - assert_equal 'text/plain', Pathname.new("itty.nu").mime_type - assert_equal 'text/plain', Pathname.new("defun.kt").mime_type + assert_equal 'text/plain; charset=utf-8', Pathname.new("defun.kt").mime_type end end