Merge branch 'fix_encoding' of https://github.com/brodock/gitlabhq into brodock-fix_encoding

Conflicts:
	lib/gitlab/encode.rb
This commit is contained in:
randx 2012-05-27 12:20:35 +03:00
commit d72f8db08a
5 changed files with 29 additions and 13 deletions

View file

@ -1,3 +1,5 @@
# Patch Strings to enable detect_encoding! on views
require 'charlock_holmes/string'
module Gitlab
module Encode
extend self
@ -5,16 +7,26 @@ module Gitlab
def utf8 message
return nil unless message
hash = CharlockHolmes::EncodingDetector.detect(message) rescue {}
if hash[:encoding]
CharlockHolmes::Converter.convert(message, hash[:encoding], 'UTF-8')
detect = CharlockHolmes::EncodingDetector.detect(message) rescue {}
# It's better to default to UTF-8 as sometimes it's wrongly detected as another charset
if detect[:encoding] && detect[:confidence] == 100
CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8')
else
message
end.force_encoding("utf-8")
# Prevent app from crash cause of
# encoding errors
rescue
""
"--broken encoding: #{encoding}"
end
def detect_encoding message
return nil unless message
hash = CharlockHolmes::EncodingDetector.detect(message) rescue {}
return hash[:encoding] ? hash[:encoding] : nil
end
end
end