rewrite encode strategy.
This commit is contained in:
parent
89043d6b89
commit
206230a4ec
|
@ -1,25 +1,29 @@
|
||||||
# Patch Strings to enable detect_encoding! on views
|
# Patch Strings to enable detect_encoding! on views
|
||||||
require 'charlock_holmes/string'
|
require 'charlock_holmes/string'
|
||||||
module Gitlab
|
module Gitlab
|
||||||
module Encode
|
module Encode
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
def utf8 message
|
def utf8 message
|
||||||
|
# return nil if message is nil
|
||||||
return nil unless message
|
return nil unless message
|
||||||
|
|
||||||
detect = CharlockHolmes::EncodingDetector.detect(message) rescue {}
|
# if message is utf-8 encoding, just return it
|
||||||
|
message.force_encoding("utf-8")
|
||||||
|
return message if message.valid_encoding?
|
||||||
|
|
||||||
# It's better to default to UTF-8 as sometimes it's wrongly detected as another charset
|
# if message is not utf-8 encoding, detect and convert it
|
||||||
if detect[:encoding] && detect[:confidence] == 100
|
detect = CharlockHolmes::EncodingDetector.detect(message)
|
||||||
CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8')
|
if detect[:encoding] && detect[:confidence] > 60
|
||||||
else
|
message.force_encoding(detect[:encoding])
|
||||||
message
|
message.encode!("utf-8", detect[:encoding], :undef => :replace, :replace => "", :invalid => :replace)
|
||||||
end.force_encoding("utf-8")
|
end
|
||||||
|
|
||||||
# Prevent app from crash cause of
|
message.valid_encoding? ? message : raise
|
||||||
# encoding errors
|
|
||||||
|
# Prevent app from crash cause of encoding errors
|
||||||
rescue
|
rescue
|
||||||
"--broken encoding: #{encoding}"
|
"--broken encoding: #{detect[:encoding]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_encoding message
|
def detect_encoding message
|
||||||
|
|
Loading…
Reference in New Issue