Commit d34904b86c07f6a408bbd4496ea23a23f1208ec5
Exists in
master
and in
4 other branches
Merge branch 'grit_ext'
Showing
10 changed files
with
31 additions
and
29 deletions
Show diff stats
.travis.yml
Gemfile
| ... | ... | @@ -22,9 +22,9 @@ gem "acts_as_list" |
| 22 | 22 | gem "rdiscount" |
| 23 | 23 | gem "acts-as-taggable-on", "~> 2.1.0" |
| 24 | 24 | gem "drapper" |
| 25 | -gem "rchardet19", "~> 1.3.5" | |
| 26 | 25 | gem "resque" |
| 27 | 26 | gem "httparty" |
| 27 | +gem "charlock_holmes" | |
| 28 | 28 | |
| 29 | 29 | group :assets do |
| 30 | 30 | gem "sass-rails", "~> 3.1.0" | ... | ... |
Gemfile.lock
| ... | ... | @@ -77,6 +77,7 @@ GEM |
| 77 | 77 | xpath (~> 0.1.4) |
| 78 | 78 | carrierwave (0.5.8) |
| 79 | 79 | activesupport (~> 3.0) |
| 80 | + charlock_holmes (0.6.8) | |
| 80 | 81 | childprocess (0.2.2) |
| 81 | 82 | ffi (~> 1.0.6) |
| 82 | 83 | coffee-rails (3.1.1) |
| ... | ... | @@ -172,7 +173,6 @@ GEM |
| 172 | 173 | rdoc (~> 3.4) |
| 173 | 174 | thor (~> 0.14.6) |
| 174 | 175 | rake (0.9.2.2) |
| 175 | - rchardet19 (1.3.5) | |
| 176 | 176 | rdiscount (1.6.8) |
| 177 | 177 | rdoc (3.11) |
| 178 | 178 | json (~> 1.4) |
| ... | ... | @@ -285,6 +285,7 @@ DEPENDENCIES |
| 285 | 285 | awesome_print |
| 286 | 286 | capybara |
| 287 | 287 | carrierwave |
| 288 | + charlock_holmes | |
| 288 | 289 | coffee-rails (~> 3.1.0) |
| 289 | 290 | database_cleaner |
| 290 | 291 | devise (= 1.5.0) |
| ... | ... | @@ -302,7 +303,6 @@ DEPENDENCIES |
| 302 | 303 | pygments.rb (= 0.2.3) |
| 303 | 304 | rails (= 3.1.1) |
| 304 | 305 | rails-footnotes (~> 3.7.5) |
| 305 | - rchardet19 (~> 1.3.5) | |
| 306 | 306 | rdiscount |
| 307 | 307 | resque |
| 308 | 308 | rspec-rails | ... | ... |
app/helpers/application_helper.rb
app/models/commit.rb
| 1 | 1 | class Commit |
| 2 | - include Utils::CharEncode | |
| 3 | 2 | |
| 4 | 3 | attr_accessor :commit |
| 5 | 4 | attr_accessor :head |
| ... | ... | @@ -22,7 +21,7 @@ class Commit |
| 22 | 21 | end |
| 23 | 22 | |
| 24 | 23 | def safe_message |
| 25 | - encode(message) | |
| 24 | + message | |
| 26 | 25 | end |
| 27 | 26 | |
| 28 | 27 | def created_at |
| ... | ... | @@ -30,11 +29,11 @@ class Commit |
| 30 | 29 | end |
| 31 | 30 | |
| 32 | 31 | def author_email |
| 33 | - encode(author.email) | |
| 32 | + author.email | |
| 34 | 33 | end |
| 35 | 34 | |
| 36 | 35 | def author_name |
| 37 | - encode(author.name) | |
| 36 | + author.name | |
| 38 | 37 | end |
| 39 | 38 | |
| 40 | 39 | def prev_commit | ... | ... |
app/views/commits/_text_file.html.haml
app/views/refs/_tree.html.haml
| ... | ... | @@ -40,9 +40,9 @@ |
| 40 | 40 | %h3= content.name |
| 41 | 41 | .readme |
| 42 | 42 | - if content.name =~ /\.(md|markdown)$/i |
| 43 | - = markdown(encode content.data) | |
| 43 | + = markdown(content.data) | |
| 44 | 44 | - else |
| 45 | - = simple_format(encode content.data) | |
| 45 | + = simple_format(content.data) | |
| 46 | 46 | |
| 47 | 47 | :javascript |
| 48 | 48 | $(function(){ | ... | ... |
config/initializers/gitlabhq/20_grit_ext.rb
| ... | ... | @@ -7,5 +7,23 @@ Grit::Blob.class_eval do |
| 7 | 7 | include Utils::Colorize |
| 8 | 8 | end |
| 9 | 9 | |
| 10 | +#monkey patch raw_object from string | |
| 11 | +Grit::GitRuby::Internal::RawObject.class_eval do | |
| 12 | + def content | |
| 13 | + transcoding(@content) | |
| 14 | + end | |
| 15 | + | |
| 16 | + private | |
| 17 | + def transcoding(content) | |
| 18 | + content ||= "" | |
| 19 | + detection = CharlockHolmes::EncodingDetector.detect(content) | |
| 20 | + if hash = detection | |
| 21 | + content = CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding] | |
| 22 | + end | |
| 23 | + content | |
| 24 | + end | |
| 25 | +end | |
| 26 | + | |
| 27 | + | |
| 10 | 28 | Grit::Git.git_timeout = GIT_OPTS["git_timeout"] |
| 11 | 29 | Grit::Git.git_max_size = GIT_OPTS["git_max_size"] | ... | ... |
lib/graph_commit.rb
| 1 | 1 | require "grit" |
| 2 | 2 | |
| 3 | 3 | class GraphCommit |
| 4 | - include Utils::CharEncode | |
| 5 | 4 | attr_accessor :time, :space |
| 6 | 5 | attr_accessor :refs |
| 7 | 6 | |
| ... | ... | @@ -97,13 +96,13 @@ class GraphCommit |
| 97 | 96 | h[:parents] = self.parents.collect do |p| |
| 98 | 97 | [p.id,0,0] |
| 99 | 98 | end |
| 100 | - h[:author] = encode(author.name) | |
| 99 | + h[:author] = author.name | |
| 101 | 100 | h[:time] = time |
| 102 | 101 | h[:space] = space |
| 103 | 102 | h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil? |
| 104 | 103 | h[:id] = sha |
| 105 | 104 | h[:date] = date |
| 106 | - h[:message] = encode(message) | |
| 105 | + h[:message] = message | |
| 107 | 106 | h[:login] = author.email |
| 108 | 107 | h |
| 109 | 108 | end | ... | ... |
lib/utils.rb
| ... | ... | @@ -16,28 +16,14 @@ module Utils |
| 16 | 16 | end |
| 17 | 17 | end |
| 18 | 18 | |
| 19 | - module CharEncode | |
| 20 | - def encode(string) | |
| 21 | - return '' unless string | |
| 22 | - cd = CharDet.detect(string) | |
| 23 | - if cd.confidence > 0.6 | |
| 24 | - string.force_encoding(cd.encoding) | |
| 25 | - end | |
| 26 | - string.encode("utf-8", :undef => :replace, :replace => "?", :invalid => :replace) | |
| 27 | - rescue | |
| 28 | - "Invalid Encoding" | |
| 29 | - end | |
| 30 | - end | |
| 31 | - | |
| 32 | 19 | module Colorize |
| 33 | - include CharEncode | |
| 34 | 20 | def colorize |
| 35 | 21 | system_colorize(data, name) |
| 36 | 22 | end |
| 37 | 23 | |
| 38 | 24 | def system_colorize(data, file_name) |
| 39 | 25 | ft = handle_file_type(file_name) |
| 40 | - Pygments.highlight(encode(data), :lexer => ft, :options => { :encoding => 'utf-8', :linenos => 'True' }) | |
| 26 | + Pygments.highlight(data, :lexer => ft, :options => { :encoding => 'utf-8', :linenos => 'True' }) | |
| 41 | 27 | end |
| 42 | 28 | |
| 43 | 29 | def handle_file_type(file_name, mime_type = nil) | ... | ... |