mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
@@ -1,6 +1,5 @@
|
||||
require 'linguist/blob_helper'
|
||||
require 'linguist/generated'
|
||||
require 'linguist/language'
|
||||
require 'linguist/mime'
|
||||
require 'linguist/repository'
|
||||
require 'linguist/samples'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user