mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Use Charlock all the time for binary detection
This commit is contained in:
@@ -53,8 +53,7 @@ module Linguist
|
||||
#
|
||||
# Returns a content type String.
|
||||
def content_type
|
||||
@content_type ||= binary? ?
|
||||
mime_type :
|
||||
@content_type ||= binary? ? mime_type :
|
||||
(encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain")
|
||||
end
|
||||
|
||||
@@ -93,11 +92,8 @@ module Linguist
|
||||
#
|
||||
# Return true or false
|
||||
def binary?
|
||||
if mime_type = Mime.lookup_mime_type_for(pathname.extname)
|
||||
mime_type.binary?
|
||||
else
|
||||
encoding.nil? || detect_encoding[:type] == :binary
|
||||
end
|
||||
return false if data.empty?
|
||||
encoding.nil? || detect_encoding[:type] == :binary
|
||||
end
|
||||
|
||||
# Public: Is the blob text?
|
||||
|
||||
@@ -58,26 +58,6 @@ module Linguist
|
||||
mime_type ? mime_type.simplified : 'text/plain'
|
||||
end
|
||||
|
||||
# Internal: Determine if extension or mime type is binary.
|
||||
#
|
||||
# ext_or_mime_type - A file extension ".exe" or
|
||||
# mime type "application/octet-stream".
|
||||
#
|
||||
# Returns true or false
|
||||
def self.binary?(ext_or_mime_type)
|
||||
mime_type = lookup_mime_type_for(ext_or_mime_type)
|
||||
mime_type ? mime_type.binary? : false
|
||||
end
|
||||
|
||||
# Internal: Determine if extension or mime type is plain text.
|
||||
#
|
||||
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
||||
#
|
||||
# Returns true or false
|
||||
def self.text?(ext_or_mime_type)
|
||||
!binary?(ext_or_mime_type)
|
||||
end
|
||||
|
||||
# Internal: Lookup mime type for extension or mime type
|
||||
#
|
||||
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
||||
|
||||
@@ -77,27 +77,6 @@ module Linguist
|
||||
@mime_type ||= Mime.mime_for(extname)
|
||||
end
|
||||
|
||||
# Public: Get the Content-Type header
|
||||
#
|
||||
# This value is used when serving raw blobs.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Pathname.new('file.txt').content_type
|
||||
# # => 'text/plain; charset=utf-8'
|
||||
#
|
||||
# Returns a content type String.
|
||||
def content_type
|
||||
@content_type ||= binary? ? mime_type : 'text/plain; charset=utf8'
|
||||
end
|
||||
|
||||
# Public: Is the path binary?
|
||||
#
|
||||
# Return true or false
|
||||
def binary?
|
||||
@binary ||= Mime.binary?(extname)
|
||||
end
|
||||
|
||||
# Public: Return self as String
|
||||
#
|
||||
# Returns a String
|
||||
|
||||
@@ -66,120 +66,4 @@ class TestMime < Test::Unit::TestCase
|
||||
assert_equal 'text/rust', Mime.mime_for('.rc')
|
||||
assert_equal 'video/quicktime', Mime.mime_for('.mov')
|
||||
end
|
||||
|
||||
def test_binary
|
||||
assert Mime.binary?('application/octet-stream')
|
||||
|
||||
# Add an assertion for any binary mime types added to mimes.yml.
|
||||
# Please keep this list alphabetized.
|
||||
assert Mime.binary?('application/java-archive')
|
||||
assert Mime.binary?('application/ogg')
|
||||
assert Mime.binary?('application/pdf')
|
||||
assert Mime.binary?('application/postscript')
|
||||
assert Mime.binary?('application/x-gzip')
|
||||
assert Mime.binary?('application/x-shockwave-flash')
|
||||
assert Mime.binary?('application/x-silverlight-app')
|
||||
assert Mime.binary?('application/zip')
|
||||
assert Mime.binary?('audio/mp4')
|
||||
assert Mime.binary?('image/gif')
|
||||
assert Mime.binary?('image/jpeg')
|
||||
assert Mime.binary?('image/png')
|
||||
|
||||
# Legacy. Prefer testing mime types instead of extensions.
|
||||
assert Mime.binary?('.a')
|
||||
assert Mime.binary?('.air')
|
||||
assert Mime.binary?('.blend')
|
||||
assert Mime.binary?('.crx')
|
||||
assert Mime.binary?('.deb')
|
||||
assert Mime.binary?('.dmg')
|
||||
assert Mime.binary?('.exe')
|
||||
assert Mime.binary?('.gem')
|
||||
assert Mime.binary?('.graffle')
|
||||
assert Mime.binary?('.gz')
|
||||
assert Mime.binary?('.icns')
|
||||
assert Mime.binary?('.ipa')
|
||||
assert Mime.binary?('.key')
|
||||
assert Mime.binary?('.lib')
|
||||
assert Mime.binary?('.mcz')
|
||||
assert Mime.binary?('.mov')
|
||||
assert Mime.binary?('.mp3')
|
||||
assert Mime.binary?('.nib')
|
||||
assert Mime.binary?('.numbers')
|
||||
assert Mime.binary?('.o')
|
||||
assert Mime.binary?('.odp')
|
||||
assert Mime.binary?('.ods')
|
||||
assert Mime.binary?('.odt')
|
||||
assert Mime.binary?('.ogg')
|
||||
assert Mime.binary?('.ogv')
|
||||
assert Mime.binary?('.otf')
|
||||
assert Mime.binary?('.pages')
|
||||
assert Mime.binary?('.pfx')
|
||||
assert Mime.binary?('.pigx')
|
||||
assert Mime.binary?('.plgx')
|
||||
assert Mime.binary?('.pptx')
|
||||
assert Mime.binary?('.psd')
|
||||
assert Mime.binary?('.sib')
|
||||
assert Mime.binary?('.so')
|
||||
assert Mime.binary?('.spl')
|
||||
assert Mime.binary?('.sqlite3')
|
||||
assert Mime.binary?('.swc')
|
||||
assert Mime.binary?('.swf')
|
||||
assert Mime.binary?('.tar')
|
||||
assert Mime.binary?('.ucode')
|
||||
assert Mime.binary?('.xpi')
|
||||
assert Mime.binary?('.zip')
|
||||
end
|
||||
|
||||
def test_text
|
||||
# By default, assume the plain text.
|
||||
assert Mime.text?(nil)
|
||||
assert Mime.text?('')
|
||||
|
||||
assert Mime.text?('text/plain')
|
||||
|
||||
# Add an assertion for any text mime types added to mimes.yml.
|
||||
# Please keep this list alphabetized.
|
||||
assert Mime.text?('application/atom+xml')
|
||||
assert Mime.text?('application/javascript')
|
||||
assert Mime.text?('application/json')
|
||||
assert Mime.text?('application/perl')
|
||||
assert Mime.text?('application/rdf+xml')
|
||||
assert Mime.text?('application/sh')
|
||||
assert Mime.text?('application/x-ms-xbap')
|
||||
assert Mime.text?('application/x-perl')
|
||||
assert Mime.text?('application/x-python')
|
||||
assert Mime.text?('application/x-ruby')
|
||||
assert Mime.text?('application/xaml+xml')
|
||||
assert Mime.text?('application/xhtml+xml')
|
||||
assert Mime.text?('application/xml')
|
||||
assert Mime.text?('text/cache-manifest')
|
||||
assert Mime.text?('text/css')
|
||||
assert Mime.text?('text/csv')
|
||||
assert Mime.text?('text/html')
|
||||
assert Mime.text?('text/javascript')
|
||||
assert Mime.text?('text/plain')
|
||||
assert Mime.text?('text/x-nimrod')
|
||||
assert Mime.text?('text/x-nemerle')
|
||||
|
||||
# Legacy. Prefer testing mime types instead of extensions.
|
||||
assert Mime.text?('.cu')
|
||||
assert Mime.text?('.js')
|
||||
assert Mime.text?('.latex')
|
||||
assert Mime.text?('.ms')
|
||||
assert Mime.text?('.nc')
|
||||
assert Mime.text?('.pl')
|
||||
assert Mime.text?('.pm')
|
||||
assert Mime.text?('.py')
|
||||
assert Mime.text?('.rb')
|
||||
assert Mime.text?('.sc')
|
||||
assert Mime.text?('.sh')
|
||||
assert Mime.text?('.sig')
|
||||
assert Mime.text?('.sml')
|
||||
assert Mime.text?('.sps')
|
||||
assert Mime.text?('.src')
|
||||
assert Mime.text?('.tcl')
|
||||
assert Mime.text?('.texi')
|
||||
assert Mime.text?('.texinfo')
|
||||
assert Mime.text?('.xul')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -59,10 +59,4 @@ class TestPathname < Test::Unit::TestCase
|
||||
assert_equal 'application/python', Pathname.new("itty.py").mime_type
|
||||
assert_equal 'text/plain', Pathname.new("defun.kt").mime_type
|
||||
end
|
||||
|
||||
def test_content_type
|
||||
assert_equal 'text/plain; charset=utf8', Pathname.new("file.txt").content_type
|
||||
assert_equal 'text/plain; charset=utf8', Pathname.new("file.rb").content_type
|
||||
assert_equal 'image/png', Pathname.new("octocat.png").content_type
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user