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