Merge pull request #277 from warpc/274-file_detection_fixes

[Refs #274] file detection fixes
This commit is contained in:
Vladimir Sharshov 2012-03-07 06:53:16 -08:00
commit 1cb109119d
8 changed files with 71 additions and 8 deletions

View File

@ -305,6 +305,7 @@ table.tablesorter tbody td a .issue_title {
#output.formatted {
width: auto;
font-family: "Consolas","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
color: #000000;
padding: 10px 5px 0px;
margin-left: 45px;
}

View File

@ -53,8 +53,10 @@ module GitHelper
def choose_render_way(blob)
return :image if blob.mime_type.match(/image/)
return :text if blob.mime_type.match(/text|xml|json/)
:binary
return :binary if blob.binary?
:text
# return :text if blob.mime_type.match(/text|xml|json/)
# :binary
end
def force_encoding_to_site(string)

View File

@ -36,7 +36,7 @@
:javascript
$(function() {
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {lineNumbers: true, mode: '#{@blob.mime_type}', theme: 'eclipse'});
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {lineNumbers: true, mode: '#{@blob.raw_mime_type.content_type}', theme: 'eclipse'});
});
/ - content_for :javascripts do

View File

@ -53,5 +53,5 @@
:javascript
$(document).ready(function() {
var text = $('#code').text().replace(/&/gi, '&');
CodeMirror.runMode(text, "#{File.extname(@blob.name) == '.spec' ? 'text/x-rpm-spec' : @blob.mime_type}", document.getElementById("output"));
CodeMirror.runMode(text, "#{@blob.raw_mime_type.content_type}", document.getElementById("output"));
});

View File

@ -13,3 +13,14 @@ Rosa::Application.config.middleware.insert_before ::Grack::Handler, ::Grack::Aut
# Grit::Git.git_timeout = 60
Dir[Rails.root.join("lib/ext/**/*.rb")].each {|f| require f}
# add rpm spec as mime type for *.spec files
types = [
["text/x-python", ['py'], '8bit'],
["text/x-rpm-spec", ['spec'], '8bit'],
["text/x-csrc", ['h', 'c'], '8bit'],
["text/x-c++src", ['cpp'], '8bit']
]
types.each do |type|
MIME::Types.add MIME::Type.from_array(type)
end

View File

@ -284,11 +284,11 @@ ActiveRecord::Schema.define(:version => 20120303171802) do
t.text "description"
t.string "ancestry"
t.boolean "has_issues", :default => true
t.boolean "has_wiki", :default => false
t.string "srpm_file_name"
t.string "srpm_content_type"
t.integer "srpm_file_size"
t.datetime "srpm_updated_at"
t.boolean "has_wiki", :default => false
t.string "default_branch", :default => "master"
t.boolean "is_rpm", :default => true
end
@ -309,7 +309,6 @@ ActiveRecord::Schema.define(:version => 20120303171802) do
end
add_index "register_requests", ["email"], :name => "index_register_requests_on_email", :unique => true, :case_sensitive => false
add_index "register_requests", ["token"], :name => "index_register_requests_on_token", :unique => true, :case_sensitive => false
create_table "relations", :force => true do |t|
t.integer "object_id"
@ -369,6 +368,7 @@ ActiveRecord::Schema.define(:version => 20120303171802) do
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at"
t.datetime "updated_at"
@ -376,7 +376,6 @@ ActiveRecord::Schema.define(:version => 20120303171802) do
t.string "uname"
t.string "role"
t.string "language", :default => "en"
t.datetime "reset_password_sent_at"
t.integer "own_projects_count", :default => 0, :null => false
t.text "professional_experience"
t.string "site"

49
lib/grit/blob.rb Normal file
View File

@ -0,0 +1,49 @@
# -*- ruby encoding: utf-8 -*-
module Grit
class Blob
DEFAULT_RAW_MIME_TYPE = MIME::Types[DEFAULT_MIME_TYPE].first
delegate :binary?, :ascii?, :encoding, :to => :raw_mime_type
def mime_type_with_class_store
set_associated_mimes
@associated_mimes.first.simplified
end
alias_method_chain :mime_type, :class_store
attr_accessor :raw_mime_type
def raw_mime_type
set_associated_mimes
@raw_mime_type = @associated_mimes.first || DEFAULT_RAW_MIME_TYPE
@raw_mime_type
end
def raw_mime_types
set_associated_mimes
end
protected
# store all associated MIME::Types inside class
def set_associated_mimes
@associated_mimes ||= []
if @associated_mimes.empty?
guesses = MIME::Types.type_for(self.name) rescue [DEFAULT_RAW_MIME_TYPE]
guesses = [DEFAULT_RAW_MIME_TYPE] if guesses.empty?
@associated_mimes = guesses.sort{|a,b| mime_sort(a, b)}
end
@associated_mimes
end
# TODO make more clever function
def mime_sort(a,b)
return 0 if a.media_type == b.media_type and a.registered? == b.registered?
return -1 if a.media_type == 'text' and !a.registered?
return 1
end
end
end

View File

@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require './lib/grit/repo.rb'
require './lib/grit/diff.rb'
require './lib/grit/blob.rb'