diff options
| author | Joshua Peek <josh@joshpeek.com> | 2012-08-20 11:40:32 -0500 |
|---|---|---|
| committer | Joshua Peek <josh@joshpeek.com> | 2012-08-20 11:40:32 -0500 |
| commit | cfe496e9fcb40cf9a95f506963a8064e3bc876fb (patch) | |
| tree | d7ef41dfa322293b6cd7dcf8e2ffcc0901286bbe | |
| parent | b85aeaad3e8cbbc8e910911166f78394c24bf8a5 (diff) | |
Drop mime type module
Closes #206
| -rw-r--r-- | lib/linguist.rb | 1 | ||||
| -rw-r--r-- | lib/linguist/blob_helper.rb | 39 | ||||
| -rw-r--r-- | lib/linguist/mime.rb | 27 |
3 files changed, 25 insertions, 42 deletions
diff --git a/lib/linguist.rb b/lib/linguist.rb index 0f1b0f8..e717fb6 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 75bee67..b2d72f1 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 a1e5555..0000000 --- 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 |
