mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add Blob
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
module Linguist
|
||||
end
|
||||
|
||||
require 'linguist/blob'
|
||||
require 'linguist/language'
|
||||
require 'linguist/mime'
|
||||
require 'linguist/pathname'
|
||||
|
||||
54
lib/linguist/blob.rb
Normal file
54
lib/linguist/blob.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
require 'linguist/mime'
|
||||
require 'linguist/pathname'
|
||||
|
||||
module Linguist
|
||||
class Blob
|
||||
def initialize(blob)
|
||||
@blob = blob
|
||||
@name = Pathname.new(blob.name || "")
|
||||
end
|
||||
|
||||
attr_reader :name
|
||||
|
||||
def data
|
||||
@blob.data
|
||||
end
|
||||
|
||||
def mime_type
|
||||
Mime.lookup(name.extname, @blob.mime_type)
|
||||
end
|
||||
|
||||
def size
|
||||
@blob.size
|
||||
end
|
||||
|
||||
def submodule?
|
||||
defined?(Grit::Submodule) && @blob.kind_of?(Grit::Submodule)
|
||||
end
|
||||
|
||||
def file?
|
||||
image? || !text? || mime_type == 'octet-stream'
|
||||
end
|
||||
|
||||
def text?
|
||||
return false if submodule?
|
||||
|
||||
name.media_type == 'text' ||
|
||||
name.mime_type == 'application/json'
|
||||
end
|
||||
|
||||
def image?
|
||||
['.png', '.jpg', '.jpeg', '.gif'].include?(name.extname)
|
||||
end
|
||||
|
||||
MEGABYTE = 1024 * 1024
|
||||
|
||||
def large?
|
||||
size.to_i > MEGABYTE
|
||||
end
|
||||
|
||||
def viewable?
|
||||
!file? && !large?
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,11 +10,13 @@ module Linguist
|
||||
module Mime
|
||||
Special = YAML.load_file(File.expand_path("../special_mime_types.yml", __FILE__))
|
||||
|
||||
def self.lookup(ext)
|
||||
def self.lookup(ext, orginal_type = nil)
|
||||
ext ||= ''
|
||||
|
||||
guesses = ::MIME::Types.type_for(ext)
|
||||
orginal_type = guesses.first ? guesses.first.simplified : 'text/plain'
|
||||
if orginal_type.nil?
|
||||
guesses = ::MIME::Types.type_for(ext)
|
||||
orginal_type = guesses.first ? guesses.first.simplified : 'text/plain'
|
||||
end
|
||||
|
||||
type = Special[orginal_type] ||
|
||||
Special[ext.sub(/^\./, '')] ||
|
||||
|
||||
@@ -37,19 +37,6 @@ module Linguist
|
||||
mime_type.split('/').first
|
||||
end
|
||||
|
||||
def file?
|
||||
image? || !text? || mime_type == 'octet-stream'
|
||||
end
|
||||
|
||||
def text?
|
||||
media_type == 'text' ||
|
||||
mime_type == 'application/json'
|
||||
end
|
||||
|
||||
def image?
|
||||
['.png', '.jpg', '.jpeg', '.gif'].include?(extname)
|
||||
end
|
||||
|
||||
def to_s
|
||||
@path.dup
|
||||
end
|
||||
|
||||
@@ -64,21 +64,21 @@ class TestPathname < Test::Unit::TestCase
|
||||
assert_equal 'text', Pathname.new("defun.kt").media_type
|
||||
end
|
||||
|
||||
def test_file
|
||||
assert Pathname.new("octocat.png").file?
|
||||
assert Pathname.new("linguist.gem").file?
|
||||
end
|
||||
# def test_file
|
||||
# assert Pathname.new("octocat.png").file?
|
||||
# assert Pathname.new("linguist.gem").file?
|
||||
# end
|
||||
|
||||
def test_text
|
||||
assert Pathname.new("file.txt").text?
|
||||
assert Pathname.new("file.json").text?
|
||||
end
|
||||
# def test_text
|
||||
# assert Pathname.new("file.txt").text?
|
||||
# assert Pathname.new("file.json").text?
|
||||
# end
|
||||
|
||||
def test_image
|
||||
assert Pathname.new("octocat.png").image?
|
||||
assert Pathname.new("octocat.jpg").image?
|
||||
assert Pathname.new("octocat.jpeg").image?
|
||||
assert Pathname.new("octocat.gif").image?
|
||||
assert !Pathname.new("octocat.psd").image?
|
||||
end
|
||||
# def test_image
|
||||
# assert Pathname.new("octocat.png").image?
|
||||
# assert Pathname.new("octocat.jpg").image?
|
||||
# assert Pathname.new("octocat.jpeg").image?
|
||||
# assert Pathname.new("octocat.gif").image?
|
||||
# assert !Pathname.new("octocat.psd").image?
|
||||
# end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user