mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
@@ -1,5 +1,5 @@
|
|||||||
require 'linguist/blob_helper'
|
require 'linguist/blob_helper'
|
||||||
require 'linguist/language'
|
require 'linguist/language'
|
||||||
require 'linguist/mime'
|
require 'linguist/mime'
|
||||||
require 'linguist/pathname'
|
|
||||||
require 'linguist/repository'
|
require 'linguist/repository'
|
||||||
|
require 'linguist/samples'
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
require 'linguist/classifier'
|
require 'linguist/classifier'
|
||||||
require 'linguist/language'
|
require 'linguist/language'
|
||||||
require 'linguist/mime'
|
require 'linguist/mime'
|
||||||
require 'linguist/pathname'
|
|
||||||
require 'linguist/samples'
|
require 'linguist/samples'
|
||||||
|
|
||||||
require 'charlock_holmes'
|
require 'charlock_holmes'
|
||||||
@@ -13,13 +12,6 @@ module Linguist
|
|||||||
# BlobHelper is a mixin for Blobish classes that respond to "name",
|
# BlobHelper is a mixin for Blobish classes that respond to "name",
|
||||||
# "data" and "size" such as Grit::Blob.
|
# "data" and "size" such as Grit::Blob.
|
||||||
module BlobHelper
|
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
|
# Public: Get the extname of the path
|
||||||
#
|
#
|
||||||
# Examples
|
# Examples
|
||||||
@@ -29,7 +21,7 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns a String
|
# Returns a String
|
||||||
def extname
|
def extname
|
||||||
pathname.extname
|
File.extname(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Get the actual blob mime type
|
# Public: Get the actual blob mime type
|
||||||
@@ -41,7 +33,7 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns a mime type String.
|
# Returns a mime type String.
|
||||||
def mime_type
|
def mime_type
|
||||||
@mime_type ||= pathname.mime_type
|
@mime_type ||= Mime.mime_for(extname)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Get the Content-Type header value
|
# Public: Get the Content-Type header value
|
||||||
@@ -73,7 +65,7 @@ module Linguist
|
|||||||
elsif name.nil?
|
elsif name.nil?
|
||||||
"attachment"
|
"attachment"
|
||||||
else
|
else
|
||||||
"attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}"
|
"attachment; filename=#{EscapeUtils.escape_url(File.basename(name))}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -96,7 +88,7 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Return true or false
|
# Return true or false
|
||||||
def binary_mime_type?
|
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?
|
mime_type.binary?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -422,7 +414,7 @@ module Linguist
|
|||||||
disambiguate_extension_language ||
|
disambiguate_extension_language ||
|
||||||
|
|
||||||
# See if there is a Language for the extension
|
# See if there is a Language for the extension
|
||||||
pathname.language ||
|
Language.find_by_filename(name) ||
|
||||||
|
|
||||||
# Try to detect Language from shebang line
|
# Try to detect Language from shebang line
|
||||||
shebang_language
|
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
|
|
||||||
@@ -29,10 +29,6 @@ class TestBlob < Test::Unit::TestCase
|
|||||||
assert_equal "foo.rb", blob("foo.rb").name
|
assert_equal "foo.rb", blob("foo.rb").name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pathname
|
|
||||||
assert_equal Pathname.new("foo.rb"), blob("foo.rb").pathname
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_mime_type
|
def test_mime_type
|
||||||
assert_equal "application/octet-stream", blob("Binary/dog.o").mime_type
|
assert_equal "application/octet-stream", blob("Binary/dog.o").mime_type
|
||||||
assert_equal "application/ogg", blob("Binary/foo.ogg").mime_type
|
assert_equal "application/ogg", blob("Binary/foo.ogg").mime_type
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
require 'linguist/pathname'
|
|
||||||
|
|
||||||
require 'test/unit'
|
|
||||||
require 'pygments'
|
|
||||||
|
|
||||||
class TestPathname < Test::Unit::TestCase
|
|
||||||
include Linguist
|
|
||||||
|
|
||||||
Lexer = Pygments::Lexer
|
|
||||||
|
|
||||||
def test_to_s
|
|
||||||
assert_equal "file.rb", Pathname.new("file.rb").to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_basename
|
|
||||||
assert_equal 'file.rb', Pathname.new("file.rb").basename
|
|
||||||
assert_equal 'file.rb', Pathname.new("./file.rb").basename
|
|
||||||
assert_equal 'file.rb', Pathname.new("sub/dir/file.rb").basename
|
|
||||||
assert_equal '.profile', Pathname.new(".profile").basename
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_extname
|
|
||||||
assert_equal '.rb', Pathname.new("file.rb").extname
|
|
||||||
assert_equal '.rb', Pathname.new("./file.rb").extname
|
|
||||||
assert_equal '.rb', Pathname.new("sub/dir/file.rb").extname
|
|
||||||
assert_equal '', Pathname.new(".profile").extname
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_language
|
|
||||||
assert_nil Pathname.new(".rb").language
|
|
||||||
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("file.rb").language
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("./file.rb").language
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("sub/dir/file.rb").language
|
|
||||||
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("Rakefile").language
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("vendor/Rakefile").language
|
|
||||||
assert_equal Language['Ruby'], Pathname.new("./Rakefile").language
|
|
||||||
|
|
||||||
assert_equal Language['Gentoo Ebuild'], Pathname.new("file.ebuild").language
|
|
||||||
assert_equal Language['Python'], Pathname.new("itty.py").language
|
|
||||||
assert_equal Language['Nu'], Pathname.new("itty.nu").language
|
|
||||||
|
|
||||||
assert_nil Pathname.new("defu.nkt").language
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_lexer
|
|
||||||
assert_equal Lexer['Ruby'], Pathname.new("file.rb").lexer
|
|
||||||
assert_equal Lexer['Ruby'], Pathname.new("Rakefile").lexer
|
|
||||||
assert_equal Lexer['Bash'], Pathname.new("file.ebuild").lexer
|
|
||||||
assert_equal Lexer['Python'], Pathname.new("itty.py").lexer
|
|
||||||
assert_equal Lexer['Scheme'], Pathname.new("itty.nu").lexer
|
|
||||||
assert_equal Lexer['Text only'], Pathname.new("defu.nkt").lexer
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_mime_type
|
|
||||||
assert_equal 'application/x-ruby', Pathname.new("file.rb").mime_type
|
|
||||||
assert_equal 'application/javascript', Pathname.new("file.js").mime_type
|
|
||||||
assert_equal 'application/x-python', Pathname.new("itty.py").mime_type
|
|
||||||
assert_equal 'text/plain', Pathname.new("defu.nkt").mime_type
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user