mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-29 21:31:01 +00:00
Add Blob
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
module Linguist
|
module Linguist
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require 'linguist/blob'
|
||||||
require 'linguist/language'
|
require 'linguist/language'
|
||||||
require 'linguist/mime'
|
require 'linguist/mime'
|
||||||
require 'linguist/pathname'
|
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
|
module Mime
|
||||||
Special = YAML.load_file(File.expand_path("../special_mime_types.yml", __FILE__))
|
Special = YAML.load_file(File.expand_path("../special_mime_types.yml", __FILE__))
|
||||||
|
|
||||||
def self.lookup(ext)
|
def self.lookup(ext, orginal_type = nil)
|
||||||
ext ||= ''
|
ext ||= ''
|
||||||
|
|
||||||
guesses = ::MIME::Types.type_for(ext)
|
if orginal_type.nil?
|
||||||
orginal_type = guesses.first ? guesses.first.simplified : 'text/plain'
|
guesses = ::MIME::Types.type_for(ext)
|
||||||
|
orginal_type = guesses.first ? guesses.first.simplified : 'text/plain'
|
||||||
|
end
|
||||||
|
|
||||||
type = Special[orginal_type] ||
|
type = Special[orginal_type] ||
|
||||||
Special[ext.sub(/^\./, '')] ||
|
Special[ext.sub(/^\./, '')] ||
|
||||||
|
|||||||
@@ -37,19 +37,6 @@ module Linguist
|
|||||||
mime_type.split('/').first
|
mime_type.split('/').first
|
||||||
end
|
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
|
def to_s
|
||||||
@path.dup
|
@path.dup
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -64,21 +64,21 @@ class TestPathname < Test::Unit::TestCase
|
|||||||
assert_equal 'text', Pathname.new("defun.kt").media_type
|
assert_equal 'text', Pathname.new("defun.kt").media_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_file
|
# def test_file
|
||||||
assert Pathname.new("octocat.png").file?
|
# assert Pathname.new("octocat.png").file?
|
||||||
assert Pathname.new("linguist.gem").file?
|
# assert Pathname.new("linguist.gem").file?
|
||||||
end
|
# end
|
||||||
|
|
||||||
def test_text
|
# def test_text
|
||||||
assert Pathname.new("file.txt").text?
|
# assert Pathname.new("file.txt").text?
|
||||||
assert Pathname.new("file.json").text?
|
# assert Pathname.new("file.json").text?
|
||||||
end
|
# end
|
||||||
|
|
||||||
def test_image
|
# def test_image
|
||||||
assert Pathname.new("octocat.png").image?
|
# assert Pathname.new("octocat.png").image?
|
||||||
assert Pathname.new("octocat.jpg").image?
|
# assert Pathname.new("octocat.jpg").image?
|
||||||
assert Pathname.new("octocat.jpeg").image?
|
# assert Pathname.new("octocat.jpeg").image?
|
||||||
assert Pathname.new("octocat.gif").image?
|
# assert Pathname.new("octocat.gif").image?
|
||||||
assert !Pathname.new("octocat.psd").image?
|
# assert !Pathname.new("octocat.psd").image?
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user