This commit is contained in:
Joshua Peek
2011-05-11 22:08:12 -05:00
parent b424c32bb4
commit 39bd49950e
5 changed files with 75 additions and 31 deletions

View File

@@ -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
View 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

View File

@@ -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(/^\./, '')] ||

View File

@@ -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

View File

@@ -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