Extract filename strategy

This commit is contained in:
Brandon Keepers
2014-11-02 22:15:52 -05:00
parent f2cd75332c
commit 8d7b4f81b4
2 changed files with 23 additions and 12 deletions

View File

@@ -92,6 +92,8 @@ module Linguist
language language
end end
require 'linguist/strategy/filename'
# Public: Detects the Language of the blob. # Public: Detects the Language of the blob.
# #
# blob - an object that includes the Linguist `BlobHelper` interface; # blob - an object that includes the Linguist `BlobHelper` interface;
@@ -99,8 +101,6 @@ module Linguist
# #
# Returns Language or nil. # Returns Language or nil.
def self.detect(blob) def self.detect(blob)
name = blob.name.to_s
# Check if the blob is possibly binary and bail early; this is a cheap # Check if the blob is possibly binary and bail early; this is a cheap
# test that uses the extension name to guess a binary binary mime type. # test that uses the extension name to guess a binary binary mime type.
# #
@@ -108,16 +108,7 @@ module Linguist
# looking for binary characters in the blob # looking for binary characters in the blob
return nil if blob.likely_binary? || blob.binary? return nil if blob.likely_binary? || blob.binary?
# A bit of an elegant hack. If the file is executable but extensionless, possible_languages = Linguist::Strategy::Filename.new.call(blob)
# append a "magic" extension so it can be classified with other
# languages that have shebang scripts.
extension = FileBlob.new(name).extension
if extension.empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
name += ".script!"
end
# First try to find languages that match based on filename.
possible_languages = find_by_filename(name)
# If there is more than one possible language with that extension (or no # If there is more than one possible language with that extension (or no
# extension at all, in the case of extensionless scripts), we need to continue # extension at all, in the case of extensionless scripts), we need to continue

View File

@@ -0,0 +1,20 @@
module Linguist
module Strategy
class Filename
def call(blob)
name = blob.name.to_s
# A bit of an elegant hack. If the file is executable but extensionless,
# append a "magic" extension so it can be classified with other
# languages that have shebang scripts.
extension = FileBlob.new(name).extension
if extension.empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
name += ".script!"
end
# First try to find languages that match based on filename.
possible_languages = Language.find_by_filename(name)
end
end
end
end