mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Custom File.extname method which returns the filename if it is an extension
This commit is contained in:
committed by
Arfon Smith
parent
41fc785330
commit
d5098c6f66
@@ -52,5 +52,20 @@ module Linguist
|
|||||||
def size
|
def size
|
||||||
File.size(@path)
|
File.size(@path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Public: Get file extension.
|
||||||
|
#
|
||||||
|
# Returns a String.
|
||||||
|
def extension
|
||||||
|
# File.extname returns nil if the filename is an extension.
|
||||||
|
extension = File.extname(name)
|
||||||
|
basename = File.basename(name)
|
||||||
|
# Checks if the filename is an extension.
|
||||||
|
if extension.empty? && basename[0] == "."
|
||||||
|
basename
|
||||||
|
else
|
||||||
|
extension
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -109,7 +109,8 @@ module Linguist
|
|||||||
# A bit of an elegant hack. If the file is executable but extensionless,
|
# A bit of an elegant hack. If the file is executable but extensionless,
|
||||||
# append a "magic" extension so it can be classified with other
|
# append a "magic" extension so it can be classified with other
|
||||||
# languages that have shebang scripts.
|
# languages that have shebang scripts.
|
||||||
if File.extname(name).empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
|
extension = FileBlob.new(name).extension
|
||||||
|
if extension.empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
|
||||||
name += ".script!"
|
name += ".script!"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -189,7 +190,8 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns all matching Languages or [] if none were found.
|
# Returns all matching Languages or [] if none were found.
|
||||||
def self.find_by_filename(filename)
|
def self.find_by_filename(filename)
|
||||||
basename, extname = File.basename(filename), File.extname(filename)
|
basename = File.basename(filename)
|
||||||
|
extname = FileBlob.new(filename).extension()
|
||||||
langs = @filename_index[basename] +
|
langs = @filename_index[basename] +
|
||||||
@extension_index[extname]
|
@extension_index[extname]
|
||||||
langs.compact.uniq
|
langs.compact.uniq
|
||||||
|
|||||||
Reference in New Issue
Block a user