mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
154 lines
3.3 KiB
Ruby
154 lines
3.3 KiB
Ruby
require 'linguist/language'
|
|
require 'linguist/mime'
|
|
|
|
require 'escape_utils'
|
|
|
|
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 langauge 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'
|
|
#
|
|
# Pathname.new('Rakefile').extname
|
|
# # => 'Rakefile'
|
|
#
|
|
# Returns a String.
|
|
def extname
|
|
if basename[0] == ?.
|
|
basename
|
|
elsif basename.include?('.')
|
|
File.extname(basename)
|
|
else
|
|
basename
|
|
end
|
|
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 Langauge.
|
|
def language
|
|
Language.find_by_extension(extname) || Language['Text']
|
|
end
|
|
|
|
# Internal: Has a language.
|
|
#
|
|
# Will return false if language was guessed to be Text.
|
|
#
|
|
# Returns true or false.
|
|
def language?
|
|
Language.find_by_extension(extname) ? true : false
|
|
end
|
|
|
|
# Deprecated: Get the lexer of the path
|
|
#
|
|
# Returns a Lexer.
|
|
def lexer
|
|
language.lexer
|
|
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: Get the Content-Type header
|
|
#
|
|
# This value is used when serving raw blobs.
|
|
#
|
|
# Examples
|
|
#
|
|
# Pathname.new('file.txt').content_type
|
|
# # => 'text/plain; charset=utf-8'
|
|
#
|
|
# Returns a content type String.
|
|
def content_type
|
|
@content_type ||= Mime.content_type_for(extname)
|
|
end
|
|
|
|
# Public: Determine if the Pathname is a binary mime type.
|
|
#
|
|
# Returns true or false.
|
|
def binary?
|
|
@binary ||= language? ? false : Mime.binary?(extname)
|
|
end
|
|
|
|
# Public: Determine if the Pathname should be served as an
|
|
# attachment.
|
|
#
|
|
# Returns true or false.
|
|
def attachment?
|
|
@attachment ||= language? ? false : Mime.attachment?(extname)
|
|
end
|
|
|
|
# Public: Get the Content-Disposition header value
|
|
#
|
|
# This value is used when serving raw blobs.
|
|
#
|
|
# # => "attachment; filename=file.tar"
|
|
# # => "inline"
|
|
#
|
|
# Returns a content disposition String.
|
|
def disposition
|
|
if attachment?
|
|
"attachment; filename=#{EscapeUtils.escape_url(basename)}"
|
|
else
|
|
'inline'
|
|
end
|
|
end
|
|
|
|
def to_s
|
|
@path.dup
|
|
end
|
|
|
|
def eql?(other)
|
|
other.is_a?(self.class) && @path == other.to_s
|
|
end
|
|
alias_method :==, :eql?
|
|
end
|
|
end
|