mirror of
https://github.com/KevinMidboe/linguist.git
synced 2026-02-10 02:09:34 +00:00
@@ -1,7 +1,6 @@
|
||||
require 'linguist/classifier'
|
||||
require 'linguist/language'
|
||||
require 'linguist/mime'
|
||||
require 'linguist/pathname'
|
||||
require 'linguist/samples'
|
||||
|
||||
require 'charlock_holmes'
|
||||
@@ -13,13 +12,6 @@ module Linguist
|
||||
# BlobHelper is a mixin for Blobish classes that respond to "name",
|
||||
# "data" and "size" such as Grit::Blob.
|
||||
module BlobHelper
|
||||
# Internal: Get a Pathname wrapper for Blob#name
|
||||
#
|
||||
# Returns a Pathname.
|
||||
def pathname
|
||||
Pathname.new(name || "")
|
||||
end
|
||||
|
||||
# Public: Get the extname of the path
|
||||
#
|
||||
# Examples
|
||||
@@ -29,7 +21,7 @@ module Linguist
|
||||
#
|
||||
# Returns a String
|
||||
def extname
|
||||
pathname.extname
|
||||
File.extname(name)
|
||||
end
|
||||
|
||||
# Public: Get the actual blob mime type
|
||||
@@ -41,7 +33,7 @@ module Linguist
|
||||
#
|
||||
# Returns a mime type String.
|
||||
def mime_type
|
||||
@mime_type ||= pathname.mime_type
|
||||
@mime_type ||= Mime.mime_for(extname)
|
||||
end
|
||||
|
||||
# Public: Get the Content-Type header value
|
||||
@@ -73,7 +65,7 @@ module Linguist
|
||||
elsif name.nil?
|
||||
"attachment"
|
||||
else
|
||||
"attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}"
|
||||
"attachment; filename=#{EscapeUtils.escape_url(File.basename(name))}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,7 +88,7 @@ module Linguist
|
||||
#
|
||||
# Return true or false
|
||||
def binary_mime_type?
|
||||
if mime_type = Mime.lookup_mime_type_for(pathname.extname)
|
||||
if mime_type = Mime.lookup_mime_type_for(extname)
|
||||
mime_type.binary?
|
||||
end
|
||||
end
|
||||
@@ -422,7 +414,7 @@ module Linguist
|
||||
disambiguate_extension_language ||
|
||||
|
||||
# See if there is a Language for the extension
|
||||
pathname.language ||
|
||||
Language.find_by_filename(name) ||
|
||||
|
||||
# Try to detect Language from shebang line
|
||||
shebang_language
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
require 'linguist/language'
|
||||
require 'linguist/mime'
|
||||
require 'pygments'
|
||||
|
||||
module Linguist
|
||||
# Similar to ::Pathname, Linguist::Pathname wraps a path string and
|
||||
# provides helpful query methods. Its useful when you only have a
|
||||
# filename but not a blob and need to figure out the language of the file.
|
||||
class Pathname
|
||||
# Public: Initialize a Pathname
|
||||
#
|
||||
# path - A filename String. The file may or maybe actually exist.
|
||||
#
|
||||
# Returns a Pathname.
|
||||
def initialize(path)
|
||||
@path = path
|
||||
end
|
||||
|
||||
# Public: Get the basename of the path
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Pathname.new('sub/dir/file.rb').basename
|
||||
# # => 'file.rb'
|
||||
#
|
||||
# Returns a String.
|
||||
def basename
|
||||
File.basename(@path)
|
||||
end
|
||||
|
||||
# Public: Get the extname of the path
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Pathname.new('.rb').extname
|
||||
# # => '.rb'
|
||||
#
|
||||
# Pathname.new('file.rb').extname
|
||||
# # => '.rb'
|
||||
#
|
||||
# Returns a String.
|
||||
def extname
|
||||
File.extname(@path)
|
||||
end
|
||||
|
||||
# Public: Get the language of the path
|
||||
#
|
||||
# The path extension name is the only heuristic used to detect the
|
||||
# language name.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Pathname.new('file.rb').language
|
||||
# # => Language['Ruby']
|
||||
#
|
||||
# Returns a Language or nil if none was found.
|
||||
def language
|
||||
@language ||= Language.find_by_filename(@path)
|
||||
end
|
||||
|
||||
# Internal: Get the lexer of the path
|
||||
#
|
||||
# Returns a Lexer.
|
||||
def lexer
|
||||
language ? language.lexer : Pygments::Lexer.find_by_name('Text only')
|
||||
end
|
||||
|
||||
# Public: Get the mime type
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Pathname.new('index.html').mime_type
|
||||
# # => 'text/html'
|
||||
#
|
||||
# Returns a mime type String.
|
||||
def mime_type
|
||||
@mime_type ||= Mime.mime_for(extname)
|
||||
end
|
||||
|
||||
# Public: Return self as String
|
||||
#
|
||||
# Returns a String
|
||||
def to_s
|
||||
@path.dup
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
other.is_a?(self.class) && @path == other.to_s
|
||||
end
|
||||
alias_method :==, :eql?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user