mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
More docs
This commit is contained in:
@@ -2,7 +2,11 @@ require 'mime/types'
|
|||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
|
||||||
# Register additional mime type extensions
|
# Register additional mime type extensions
|
||||||
|
#
|
||||||
|
# Follows same format as mime-types data file
|
||||||
|
# https://github.com/halostatue/mime-types/blob/master/lib/mime/types.rb.data
|
||||||
File.read(File.expand_path("../mimes.yml", __FILE__)).lines.each do |line|
|
File.read(File.expand_path("../mimes.yml", __FILE__)).lines.each do |line|
|
||||||
|
# Regexp was cargo culted from mime-types lib
|
||||||
next unless line =~ %r{^
|
next unless line =~ %r{^
|
||||||
#{MIME::Type::MEDIA_TYPE_RE}
|
#{MIME::Type::MEDIA_TYPE_RE}
|
||||||
(?:\s@([^\s]+))?
|
(?:\s@([^\s]+))?
|
||||||
@@ -14,7 +18,9 @@ File.read(File.expand_path("../mimes.yml", __FILE__)).lines.each do |line|
|
|||||||
extensions = $3
|
extensions = $3
|
||||||
encoding = $4
|
encoding = $4
|
||||||
|
|
||||||
|
# Lookup existing mime type
|
||||||
mime_type = MIME::Types["#{mediatype}/#{subtype}"].first ||
|
mime_type = MIME::Types["#{mediatype}/#{subtype}"].first ||
|
||||||
|
# Or create a new instance
|
||||||
MIME::Type.new("#{mediatype}/#{subtype}")
|
MIME::Type.new("#{mediatype}/#{subtype}")
|
||||||
|
|
||||||
if extensions
|
if extensions
|
||||||
@@ -23,8 +29,11 @@ File.read(File.expand_path("../mimes.yml", __FILE__)).lines.each do |line|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if encoding
|
||||||
mime_type.encoding = encoding
|
mime_type.encoding = encoding
|
||||||
|
end
|
||||||
|
|
||||||
|
# Kind of hacky, but we need to reindex the mime type after making changes
|
||||||
MIME::Types.add_type_variant(mime_type)
|
MIME::Types.add_type_variant(mime_type)
|
||||||
MIME::Types.index_extensions(mime_type)
|
MIME::Types.index_extensions(mime_type)
|
||||||
end
|
end
|
||||||
@@ -51,7 +60,8 @@ module Linguist
|
|||||||
|
|
||||||
# Internal: Determine if extension or mime type is binary.
|
# Internal: Determine if extension or mime type is binary.
|
||||||
#
|
#
|
||||||
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
# ext_or_mime_type - A file extension ".exe" or
|
||||||
|
# mime type "application/octet-stream".
|
||||||
#
|
#
|
||||||
# Returns true or false
|
# Returns true or false
|
||||||
def self.binary?(ext_or_mime_type)
|
def self.binary?(ext_or_mime_type)
|
||||||
@@ -59,6 +69,15 @@ module Linguist
|
|||||||
mime_type ? mime_type.binary? : false
|
mime_type ? mime_type.binary? : false
|
||||||
end
|
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
|
# Internal: Lookup mime type for extension or mime type
|
||||||
#
|
#
|
||||||
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
|
||||||
|
|||||||
Reference in New Issue
Block a user