Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Drop mime type module

Closes #206
  • Loading branch information...
commit cfe496e9fcb40cf9a95f506963a8064e3bc876fb 1 parent b85aeaa
@josh josh authored
Showing with 25 additions and 42 deletions.
  1. +0 −1  lib/linguist.rb
  2. +25 −14 lib/linguist/blob_helper.rb
  3. +0 −27 lib/linguist/mime.rb
View
1  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'
View
39 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 @@ def extname
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 @@ def extname
#
# 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 @@ def detect_encoding
@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
View
27 lib/linguist/mime.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.