diff --git a/lib/linguist.rb b/lib/linguist.rb index cb227282..36702e34 100644 --- a/lib/linguist.rb +++ b/lib/linguist.rb @@ -1,5 +1,5 @@ require 'linguist/blob_helper' require 'linguist/language' require 'linguist/mime' -require 'linguist/pathname' require 'linguist/repository' +require 'linguist/samples' diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 3e101e43..0dd3913e 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -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 diff --git a/lib/linguist/pathname.rb b/lib/linguist/pathname.rb deleted file mode 100644 index f7c3cd78..00000000 --- a/lib/linguist/pathname.rb +++ /dev/null @@ -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 diff --git a/test/test_blob.rb b/test/test_blob.rb index 2bdc1cb8..ecb8782c 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -29,10 +29,6 @@ class TestBlob < Test::Unit::TestCase assert_equal "foo.rb", blob("foo.rb").name end - def test_pathname - assert_equal Pathname.new("foo.rb"), blob("foo.rb").pathname - end - def test_mime_type assert_equal "application/octet-stream", blob("Binary/dog.o").mime_type assert_equal "application/ogg", blob("Binary/foo.ogg").mime_type diff --git a/test/test_pathname.rb b/test/test_pathname.rb deleted file mode 100644 index 2d7f8427..00000000 --- a/test/test_pathname.rb +++ /dev/null @@ -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