diff --git a/lib/linguist.rb b/lib/linguist.rb index 0f1b0f83..e717fb67 100644 --- a/lib/linguist.rb +++ b/lib/linguist.rb @@ -1,6 +1,5 @@ require 'linguist/blob_helper' require 'linguist/generated' require 'linguist/language' -require 'linguist/mime' require 'linguist/repository' require 'linguist/samples' diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 75bee673..b2d72f11 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -1,9 +1,9 @@ require 'linguist/generated' require 'linguist/language' -require 'linguist/mime' require 'charlock_holmes' require 'escape_utils' +require 'mime/types' require 'pygments' require 'yaml' @@ -23,6 +23,22 @@ module Linguist File.extname(name.to_s) end + # Internal: Lookup mime type for extension. + # + # Returns a MIME::Type + def _mime_type + if defined? @_mime_type + @_mime_type + else + guesses = ::MIME::Types.type_for(extname.to_s) + + # Prefer text mime types over binary + @_mime_type = guesses.detect { |type| type.ascii? } || + # Otherwise use the first guess + guesses.first + end + end + # Public: Get the actual blob mime type # # Examples @@ -32,10 +48,14 @@ module Linguist # # Returns a mime type String. def mime_type - @mime_type ||= begin - mime_type = Mime.lookup_mime_type_for(extname.to_s) - mime_type ? mime_type.to_s : 'text/plain' - end + _mime_type ? _mime_type.to_s : 'text/plain' + end + + # Internal: Is the blob binary according to its mime type + # + # Return true or false + def binary_mime_type? + _mime_type ? _mime_type.binary? : false end # Public: Get the Content-Type header value @@ -86,15 +106,6 @@ module Linguist @detect_encoding ||= CharlockHolmes::EncodingDetector.new.detect(data) if data end - # Internal: Is the blob binary according to its mime type - # - # Return true or false - def binary_mime_type? - if mime_type = Mime.lookup_mime_type_for(extname) - mime_type.binary? - end - end - # Public: Is the blob binary? # # Return true or false diff --git a/lib/linguist/mime.rb b/lib/linguist/mime.rb deleted file mode 100644 index a1e5555f..00000000 --- a/lib/linguist/mime.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'mime/types' -require 'yaml' - -module Linguist - module Mime - # Internal: Lookup mime type for extension or mime type - # - # ext_or_mime_type - A file extension ".txt" or mime type "text/plain". - # - # Returns a MIME::Type - def self.lookup_mime_type_for(ext_or_mime_type) - ext_or_mime_type ||= '' - - if ext_or_mime_type =~ /\w+\/\w+/ - guesses = ::MIME::Types[ext_or_mime_type] - else - guesses = ::MIME::Types.type_for(ext_or_mime_type) - end - - # Prefer text mime types over binary - guesses.detect { |type| type.ascii? } || - - # Otherwise use the first guess - guesses.first - end - end -end