From d6d8410b0ffb33dfda231d213395315720169a35 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Wed, 7 Mar 2012 05:20:35 +0400 Subject: [PATCH 1/2] [issue #274] Fixed file detection. TODO. 1. Change MIME in editor 2. Change MIME sorting --- app/assets/stylesheets/design/custom.scss | 1 + app/helpers/git_helper.rb | 8 +++-- app/views/git/blobs/_show.html.haml | 2 +- config/initializers/load_config.rb | 3 ++ db/schema.rb | 5 ++- lib/grit/blob.rb | 37 +++++++++++++++++++++++ lib/grit1.rb | 1 + 7 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 lib/grit/blob.rb diff --git a/app/assets/stylesheets/design/custom.scss b/app/assets/stylesheets/design/custom.scss index 1d2ce5629..eee5871a1 100644 --- a/app/assets/stylesheets/design/custom.scss +++ b/app/assets/stylesheets/design/custom.scss @@ -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; } diff --git a/app/helpers/git_helper.rb b/app/helpers/git_helper.rb index 7ea91820f..36dffef40 100644 --- a/app/helpers/git_helper.rb +++ b/app/helpers/git_helper.rb @@ -52,9 +52,11 @@ module GitHelper end 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 :image if blob.mime_type.match(/image/) + return :binary if blob.binary? + :text +# return :text if blob.mime_type.match(/text|xml|json/) +# :binary end def force_encoding_to_site(string) diff --git a/app/views/git/blobs/_show.html.haml b/app/views/git/blobs/_show.html.haml index f1f07fe2f..5f21d3b1f 100644 --- a/app/views/git/blobs/_show.html.haml +++ b/app/views/git/blobs/_show.html.haml @@ -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")); }); diff --git a/config/initializers/load_config.rb b/config/initializers/load_config.rb index 7dfd2abbd..24ff52a08 100644 --- a/config/initializers/load_config.rb +++ b/config/initializers/load_config.rb @@ -13,3 +13,6 @@ 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 +MIME::Types.add(MIME::Type.from_array(["text/x-rpm-spec", ['spec'], '8bit'])) diff --git a/db/schema.rb b/db/schema.rb index bd61df91b..eb223e711 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/grit/blob.rb b/lib/grit/blob.rb new file mode 100644 index 000000000..24c9c46d1 --- /dev/null +++ b/lib/grit/blob.rb @@ -0,0 +1,37 @@ +# -*- 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 ||= MIME::Types.type_for(self.name) rescue [DEFAULT_RAW_MIME_TYPE] + @associated_mimes = [DEFAULT_RAW_MIME_TYPE] if @associated_mimes.empty? + @associated_mimes + end + + end +end diff --git a/lib/grit1.rb b/lib/grit1.rb index c7be50296..110f01613 100644 --- a/lib/grit1.rb +++ b/lib/grit1.rb @@ -1,3 +1,4 @@ # -*- encoding : utf-8 -*- require './lib/grit/repo.rb' require './lib/grit/diff.rb' +require './lib/grit/blob.rb' From 464de82c6845cc44cc833f4662ecaefc416412b7 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Wed, 7 Mar 2012 17:36:39 +0400 Subject: [PATCH 2/2] [issue #274] Added some MIME types. Changed MIME ordering. --- app/views/git/blobs/_editor.html.haml | 2 +- config/initializers/load_config.rb | 10 +++++++++- lib/grit/blob.rb | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/views/git/blobs/_editor.html.haml b/app/views/git/blobs/_editor.html.haml index abe5f88e9..61263d9e3 100644 --- a/app/views/git/blobs/_editor.html.haml +++ b/app/views/git/blobs/_editor.html.haml @@ -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 diff --git a/config/initializers/load_config.rb b/config/initializers/load_config.rb index 24ff52a08..11203b486 100644 --- a/config/initializers/load_config.rb +++ b/config/initializers/load_config.rb @@ -15,4 +15,12 @@ Rosa::Application.config.middleware.insert_before ::Grack::Handler, ::Grack::Aut Dir[Rails.root.join("lib/ext/**/*.rb")].each {|f| require f} # add rpm spec as mime type for *.spec files -MIME::Types.add(MIME::Type.from_array(["text/x-rpm-spec", ['spec'], '8bit'])) +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 diff --git a/lib/grit/blob.rb b/lib/grit/blob.rb index 24c9c46d1..043553435 100644 --- a/lib/grit/blob.rb +++ b/lib/grit/blob.rb @@ -28,10 +28,22 @@ module Grit # store all associated MIME::Types inside class def set_associated_mimes - @associated_mimes ||= MIME::Types.type_for(self.name) rescue [DEFAULT_RAW_MIME_TYPE] - @associated_mimes = [DEFAULT_RAW_MIME_TYPE] if @associated_mimes.empty? + @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