mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Add special mime type overrides
This commit is contained in:
		
							
								
								
									
										22
									
								
								lib/linguist/mime.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/linguist/mime.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										63
									
								
								lib/linguist/special_mime_types.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								lib/linguist/special_mime_types.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
							
								
								
									
										20
									
								
								test/test_mime.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								test/test_mime.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user