##// END OF EJS Templates
move repositories helper to_utf8 logic to lib/redmine/codeset_util.rb for common use (#2371)...
Toshi MARUYAMA -
r7705:88f8daf8b702
parent child
Show More
@@ -117,35 +117,8 module RepositoriesHelper
117 end
117 end
118
118
119 def to_utf8(str)
119 def to_utf8(str)
120 return str if str.nil?
120 Redmine::CodesetUtil.to_utf8_by_setting(str)
121 str = to_utf8_internal(str)
122 if str.respond_to?(:force_encoding)
123 str.force_encoding('UTF-8')
124 end
125 str
126 end
127
128 def to_utf8_internal(str)
129 return str if str.nil?
130 if str.respond_to?(:force_encoding)
131 str.force_encoding('ASCII-8BIT')
132 end
133 return str if str.empty?
134 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
135 if str.respond_to?(:force_encoding)
136 str.force_encoding('UTF-8')
137 end
138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
139 @encodings.each do |encoding|
140 begin
141 return Iconv.conv('UTF-8', encoding, str)
142 rescue Iconv::Failure
143 # do nothing here and try the next encoding
144 end
145 end
146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
147 end
121 end
148 private :to_utf8_internal
149
122
150 def repository_field_tags(form, repository)
123 def repository_field_tags(form, repository)
151 method = repository.class.name.demodulize.underscore + "_field_tags"
124 method = repository.class.name.demodulize.underscore + "_field_tags"
@@ -79,6 +79,40 module Redmine
79 str
79 str
80 end
80 end
81
81
82 def self.to_utf8_by_setting(str)
83 return str if str.nil?
84 str = self.to_utf8_by_setting_internal(str)
85 if str.respond_to?(:force_encoding)
86 str.force_encoding('UTF-8')
87 end
88 str
89 end
90
91 def self.to_utf8_by_setting_internal(str)
92 return str if str.nil?
93 if str.respond_to?(:force_encoding)
94 str.force_encoding('ASCII-8BIT')
95 end
96 return str if str.empty?
97 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
98 if str.respond_to?(:force_encoding)
99 str.force_encoding('UTF-8')
100 end
101 encodings = Setting.repositories_encodings.split(',').collect(&:strip)
102 encodings.each do |encoding|
103 begin
104 return Iconv.conv('UTF-8', encoding, str)
105 rescue Iconv::Failure
106 # do nothing here and try the next encoding
107 end
108 end
109 str = self.replace_invalid_utf8(str)
110 if str.respond_to?(:force_encoding)
111 str.force_encoding('UTF-8')
112 end
113 str
114 end
115
82 def self.from_utf8(str, encoding)
116 def self.from_utf8(str, encoding)
83 str ||= ''
117 str ||= ''
84 if str.respond_to?(:force_encoding)
118 if str.respond_to?(:force_encoding)
General Comments 0
You need to be logged in to leave comments. Login now