move repositories helper to_utf8 logic to lib/redmine/codeset_util.rb for common use (#2371)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7825 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
fdf6e8547b
commit
88f8daf8b7
@ -117,36 +117,9 @@ module RepositoriesHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_utf8(str)
|
def to_utf8(str)
|
||||||
return str if str.nil?
|
Redmine::CodesetUtil.to_utf8_by_setting(str)
|
||||||
str = to_utf8_internal(str)
|
|
||||||
if str.respond_to?(:force_encoding)
|
|
||||||
str.force_encoding('UTF-8')
|
|
||||||
end
|
|
||||||
str
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_utf8_internal(str)
|
|
||||||
return str if str.nil?
|
|
||||||
if str.respond_to?(:force_encoding)
|
|
||||||
str.force_encoding('ASCII-8BIT')
|
|
||||||
end
|
|
||||||
return str if str.empty?
|
|
||||||
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
|
|
||||||
if str.respond_to?(:force_encoding)
|
|
||||||
str.force_encoding('UTF-8')
|
|
||||||
end
|
|
||||||
@encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
|
|
||||||
@encodings.each do |encoding|
|
|
||||||
begin
|
|
||||||
return Iconv.conv('UTF-8', encoding, str)
|
|
||||||
rescue Iconv::Failure
|
|
||||||
# do nothing here and try the next encoding
|
|
||||||
end
|
|
||||||
end
|
|
||||||
str = Redmine::CodesetUtil.replace_invalid_utf8(str)
|
|
||||||
end
|
|
||||||
private :to_utf8_internal
|
|
||||||
|
|
||||||
def repository_field_tags(form, repository)
|
def repository_field_tags(form, repository)
|
||||||
method = repository.class.name.demodulize.underscore + "_field_tags"
|
method = repository.class.name.demodulize.underscore + "_field_tags"
|
||||||
if repository.is_a?(Repository) &&
|
if repository.is_a?(Repository) &&
|
||||||
|
@ -79,6 +79,40 @@ module Redmine
|
|||||||
str
|
str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.to_utf8_by_setting(str)
|
||||||
|
return str if str.nil?
|
||||||
|
str = self.to_utf8_by_setting_internal(str)
|
||||||
|
if str.respond_to?(:force_encoding)
|
||||||
|
str.force_encoding('UTF-8')
|
||||||
|
end
|
||||||
|
str
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.to_utf8_by_setting_internal(str)
|
||||||
|
return str if str.nil?
|
||||||
|
if str.respond_to?(:force_encoding)
|
||||||
|
str.force_encoding('ASCII-8BIT')
|
||||||
|
end
|
||||||
|
return str if str.empty?
|
||||||
|
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
|
||||||
|
if str.respond_to?(:force_encoding)
|
||||||
|
str.force_encoding('UTF-8')
|
||||||
|
end
|
||||||
|
encodings = Setting.repositories_encodings.split(',').collect(&:strip)
|
||||||
|
encodings.each do |encoding|
|
||||||
|
begin
|
||||||
|
return Iconv.conv('UTF-8', encoding, str)
|
||||||
|
rescue Iconv::Failure
|
||||||
|
# do nothing here and try the next encoding
|
||||||
|
end
|
||||||
|
end
|
||||||
|
str = self.replace_invalid_utf8(str)
|
||||||
|
if str.respond_to?(:force_encoding)
|
||||||
|
str.force_encoding('UTF-8')
|
||||||
|
end
|
||||||
|
str
|
||||||
|
end
|
||||||
|
|
||||||
def self.from_utf8(str, encoding)
|
def self.from_utf8(str, encoding)
|
||||||
str ||= ''
|
str ||= ''
|
||||||
if str.respond_to?(:force_encoding)
|
if str.respond_to?(:force_encoding)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user