2012-05-26 18:22:47 +00:00
|
|
|
# Patch Strings to enable detect_encoding! on views
|
|
|
|
require 'charlock_holmes/string'
|
2012-05-26 10:37:49 +00:00
|
|
|
module Gitlab
|
2012-04-11 20:03:56 +00:00
|
|
|
module Encode
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def utf8 message
|
2012-05-21 20:17:41 +00:00
|
|
|
return nil unless message
|
|
|
|
|
2012-05-26 23:15:06 +00:00
|
|
|
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
|
2012-05-27 02:19:27 +00:00
|
|
|
CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8')
|
2012-04-11 20:03:56 +00:00
|
|
|
else
|
|
|
|
message
|
|
|
|
end.force_encoding("utf-8")
|
2012-05-26 23:15:06 +00:00
|
|
|
|
2012-05-22 11:57:04 +00:00
|
|
|
# Prevent app from crash cause of
|
|
|
|
# encoding errors
|
|
|
|
rescue
|
2012-05-26 23:15:06 +00:00
|
|
|
"--broken encoding: #{encoding}"
|
2012-04-11 20:03:56 +00:00
|
|
|
end
|
2012-05-26 18:20:35 +00:00
|
|
|
|
|
|
|
def detect_encoding message
|
|
|
|
return nil unless message
|
|
|
|
|
|
|
|
hash = CharlockHolmes::EncodingDetector.detect(message) rescue {}
|
|
|
|
return hash[:encoding] ? hash[:encoding] : nil
|
2012-04-11 20:03:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|