Commit 206230a4ecb5c7e1b4de445ccda14814ad5a0232
1 parent
89043d6b
Exists in
master
and in
4 other branches
rewrite encode strategy.
Showing
1 changed file
with
15 additions
and
11 deletions
Show diff stats
lib/gitlab/encode.rb
| 1 | 1 | # Patch Strings to enable detect_encoding! on views |
| 2 | 2 | require 'charlock_holmes/string' |
| 3 | 3 | module Gitlab |
| 4 | - module Encode | |
| 4 | + module Encode | |
| 5 | 5 | extend self |
| 6 | 6 | |
| 7 | 7 | def utf8 message |
| 8 | + # return nil if message is nil | |
| 8 | 9 | return nil unless message |
| 9 | 10 | |
| 10 | - detect = CharlockHolmes::EncodingDetector.detect(message) rescue {} | |
| 11 | + # if message is utf-8 encoding, just return it | |
| 12 | + message.force_encoding("utf-8") | |
| 13 | + return message if message.valid_encoding? | |
| 11 | 14 | |
| 12 | - # It's better to default to UTF-8 as sometimes it's wrongly detected as another charset | |
| 13 | - if detect[:encoding] && detect[:confidence] == 100 | |
| 14 | - CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8') | |
| 15 | - else | |
| 16 | - message | |
| 17 | - end.force_encoding("utf-8") | |
| 15 | + # if message is not utf-8 encoding, detect and convert it | |
| 16 | + detect = CharlockHolmes::EncodingDetector.detect(message) | |
| 17 | + if detect[:encoding] && detect[:confidence] > 60 | |
| 18 | + message.force_encoding(detect[:encoding]) | |
| 19 | + message.encode!("utf-8", detect[:encoding], :undef => :replace, :replace => "", :invalid => :replace) | |
| 20 | + end | |
| 18 | 21 | |
| 19 | - # Prevent app from crash cause of | |
| 20 | - # encoding errors | |
| 22 | + message.valid_encoding? ? message : raise | |
| 23 | + | |
| 24 | + # Prevent app from crash cause of encoding errors | |
| 21 | 25 | rescue |
| 22 | - "--broken encoding: #{encoding}" | |
| 26 | + "--broken encoding: #{detect[:encoding]}" | |
| 23 | 27 | end |
| 24 | 28 | |
| 25 | 29 | def detect_encoding message | ... | ... |