gitlabhq/config/initializers/gitlabhq/20_grit_ext.rb

48 lines
1,020 B
Ruby
Raw Normal View History

2011-10-20 21:00:00 +02:00
require 'grit'
2011-10-21 14:35:42 +02:00
require 'pygments'
2011-10-20 21:00:00 +02:00
require "utils"
Grit::Blob.class_eval do
include Utils::FileHelper
include Utils::Colorize
end
2011-10-24 21:38:03 +02:00
2011-12-30 15:26:13 +01:00
#monkey patch raw_object from string
Grit::GitRuby::Internal::RawObject.class_eval do
def content
transcoding(@content)
rescue Exception => ex
Rails.logger.error ex.message
@content
2011-12-30 15:26:13 +01:00
end
private
2012-04-05 00:51:49 +02:00
2011-12-30 15:26:13 +01:00
def transcoding(content)
content ||= ""
2012-04-05 00:51:49 +02:00
hash = CharlockHolmes::EncodingDetector.detect(content)
if hash
return content if hash[:type] == :binary
if hash[:encoding] == "UTF-8"
content = if hash[:confidence] < 100
content
else
content.force_encoding("UTF-8")
end
return content
end
CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
else
content.force_encoding("UTF-8")
end
end
2011-12-30 15:26:13 +01:00
end
2011-11-25 21:18:36 +01:00
Grit::Git.git_timeout = GIT_OPTS["git_timeout"]
Grit::Git.git_max_size = GIT_OPTS["git_max_size"]