annotate lib/redmine/codeset_util.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents 261b3d9a4903
children
rev   line source
Chris@1464 1 if RUBY_VERSION < '1.9'
Chris@1464 2 require 'iconv'
Chris@1464 3 end
Chris@441 4
Chris@441 5 module Redmine
Chris@441 6 module CodesetUtil
Chris@441 7
Chris@441 8 def self.replace_invalid_utf8(str)
Chris@441 9 return str if str.nil?
Chris@441 10 if str.respond_to?(:force_encoding)
Chris@441 11 str.force_encoding('UTF-8')
Chris@441 12 if ! str.valid_encoding?
Chris@441 13 str = str.encode("US-ASCII", :invalid => :replace,
Chris@441 14 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@441 15 end
Chris@909 16 elsif RUBY_PLATFORM == 'java'
Chris@909 17 begin
Chris@909 18 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@909 19 str = ic.iconv(str)
Chris@909 20 rescue
Chris@909 21 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@909 22 end
Chris@441 23 else
Chris@441 24 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@441 25 txtar = ""
Chris@441 26 begin
Chris@441 27 txtar += ic.iconv(str)
Chris@441 28 rescue Iconv::IllegalSequence
Chris@441 29 txtar += $!.success
Chris@441 30 str = '?' + $!.failed[1,$!.failed.length]
Chris@441 31 retry
Chris@441 32 rescue
Chris@441 33 txtar += $!.success
Chris@441 34 end
Chris@441 35 str = txtar
Chris@441 36 end
Chris@441 37 str
Chris@441 38 end
Chris@909 39
Chris@909 40 def self.to_utf8(str, encoding)
Chris@909 41 return str if str.nil?
Chris@909 42 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@909 43 if str.empty?
Chris@909 44 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@909 45 return str
Chris@909 46 end
Chris@909 47 enc = encoding.blank? ? "UTF-8" : encoding
Chris@909 48 if str.respond_to?(:force_encoding)
Chris@909 49 if enc.upcase != "UTF-8"
Chris@909 50 str.force_encoding(enc)
Chris@909 51 str = str.encode("UTF-8", :invalid => :replace,
Chris@909 52 :undef => :replace, :replace => '?')
Chris@909 53 else
Chris@909 54 str.force_encoding("UTF-8")
Chris@909 55 if ! str.valid_encoding?
Chris@909 56 str = str.encode("US-ASCII", :invalid => :replace,
Chris@909 57 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@909 58 end
Chris@909 59 end
Chris@909 60 elsif RUBY_PLATFORM == 'java'
Chris@909 61 begin
Chris@909 62 ic = Iconv.new('UTF-8', enc)
Chris@909 63 str = ic.iconv(str)
Chris@909 64 rescue
Chris@909 65 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@909 66 end
Chris@909 67 else
Chris@909 68 ic = Iconv.new('UTF-8', enc)
Chris@909 69 txtar = ""
Chris@909 70 begin
Chris@909 71 txtar += ic.iconv(str)
Chris@909 72 rescue Iconv::IllegalSequence
Chris@909 73 txtar += $!.success
Chris@909 74 str = '?' + $!.failed[1,$!.failed.length]
Chris@909 75 retry
Chris@909 76 rescue
Chris@909 77 txtar += $!.success
Chris@909 78 end
Chris@909 79 str = txtar
Chris@909 80 end
Chris@909 81 str
Chris@909 82 end
Chris@909 83
Chris@909 84 def self.to_utf8_by_setting(str)
Chris@909 85 return str if str.nil?
Chris@909 86 str = self.to_utf8_by_setting_internal(str)
Chris@909 87 if str.respond_to?(:force_encoding)
Chris@909 88 str.force_encoding('UTF-8')
Chris@909 89 end
Chris@909 90 str
Chris@909 91 end
Chris@909 92
Chris@909 93 def self.to_utf8_by_setting_internal(str)
Chris@909 94 return str if str.nil?
Chris@909 95 if str.respond_to?(:force_encoding)
Chris@909 96 str.force_encoding('ASCII-8BIT')
Chris@909 97 end
Chris@909 98 return str if str.empty?
Chris@909 99 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@909 100 if str.respond_to?(:force_encoding)
Chris@909 101 str.force_encoding('UTF-8')
Chris@909 102 end
Chris@909 103 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
Chris@909 104 encodings.each do |encoding|
Chris@1464 105 if str.respond_to?(:force_encoding)
Chris@1464 106 begin
Chris@1464 107 str.force_encoding(encoding)
Chris@1464 108 utf8 = str.encode('UTF-8')
Chris@1464 109 return utf8 if utf8.valid_encoding?
Chris@1464 110 rescue
Chris@1464 111 # do nothing here and try the next encoding
Chris@1464 112 end
Chris@1464 113 else
Chris@1464 114 begin
Chris@1464 115 return Iconv.conv('UTF-8', encoding, str)
Chris@1464 116 rescue Iconv::Failure
Chris@1464 117 # do nothing here and try the next encoding
Chris@1464 118 end
Chris@909 119 end
Chris@909 120 end
Chris@909 121 str = self.replace_invalid_utf8(str)
Chris@909 122 if str.respond_to?(:force_encoding)
Chris@909 123 str.force_encoding('UTF-8')
Chris@909 124 end
Chris@909 125 str
Chris@909 126 end
Chris@909 127
Chris@909 128 def self.from_utf8(str, encoding)
Chris@909 129 str ||= ''
Chris@909 130 if str.respond_to?(:force_encoding)
Chris@909 131 str.force_encoding('UTF-8')
Chris@909 132 if encoding.upcase != 'UTF-8'
Chris@909 133 str = str.encode(encoding, :invalid => :replace,
Chris@909 134 :undef => :replace, :replace => '?')
Chris@909 135 else
Chris@909 136 str = self.replace_invalid_utf8(str)
Chris@909 137 end
Chris@909 138 elsif RUBY_PLATFORM == 'java'
Chris@909 139 begin
Chris@909 140 ic = Iconv.new(encoding, 'UTF-8')
Chris@909 141 str = ic.iconv(str)
Chris@909 142 rescue
Chris@909 143 str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@909 144 end
Chris@909 145 else
Chris@909 146 ic = Iconv.new(encoding, 'UTF-8')
Chris@909 147 txtar = ""
Chris@909 148 begin
Chris@909 149 txtar += ic.iconv(str)
Chris@909 150 rescue Iconv::IllegalSequence
Chris@909 151 txtar += $!.success
Chris@909 152 str = '?' + $!.failed[1, $!.failed.length]
Chris@909 153 retry
Chris@909 154 rescue
Chris@909 155 txtar += $!.success
Chris@909 156 end
Chris@909 157 str = txtar
Chris@909 158 end
Chris@909 159 end
Chris@441 160 end
Chris@441 161 end