Compare commits

...

2 Commits

Author SHA1 Message Date
Kolan Sh 65b94d2014 Character encoding detection in attachments is now automatic 2012-03-24 23:39:46 +04:00
Kolan Sh 786e92349b Automatic character encoding detection in Mercurial repos. 2012-03-24 23:39:33 +04:00
3 changed files with 55 additions and 16 deletions

View File

@ -11,6 +11,8 @@ gem "liquid", "~> 2.3.0"
gem "acts-as-taggable-on", "= 2.1.0" gem "acts-as-taggable-on", "= 2.1.0"
# Needed only on RUBY_VERSION = 1.8, ruby 1.9+ compatible interpreters should bring their csv # Needed only on RUBY_VERSION = 1.8, ruby 1.9+ compatible interpreters should bring their csv
gem "fastercsv", "~> 1.5.0", :platforms => [:ruby_18, :jruby, :mingw_18] gem "fastercsv", "~> 1.5.0", :platforms => [:ruby_18, :jruby, :mingw_18]
# need for automatic encoding detection in diff,annotate and cat
gem "chardet", ">= 0.9.0"
group :test do group :test do
gem 'shoulda', '~> 2.10.3' gem 'shoulda', '~> 2.10.3'

View File

@ -12,6 +12,9 @@
# See doc/COPYRIGHT.rdoc for more details. # See doc/COPYRIGHT.rdoc for more details.
#++ #++
require 'rubygems'
require 'UniversalDetector'
module AttachmentsHelper module AttachmentsHelper
# Displays view/delete links to the attachments of the given object # Displays view/delete links to the attachments of the given object
# Options: # Options:
@ -26,18 +29,16 @@ module AttachmentsHelper
end end
def to_utf8_for_attachments(str) def to_utf8_for_attachments(str)
if str.respond_to?(:force_encoding) return nil if str.nil?
str.force_encoding('UTF-8') iconv_str = str
return str if str.valid_encoding? detected = UniversalDetector::chardet(str)
else enc = detected['encoding']
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii if !enc.nil?
end begin
iconv_str = Iconv.conv('UTF-8', enc, str)
begin rescue Iconv::Failure => err
Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] end
rescue Iconv::InvalidEncoding end
# "UTF-8//IGNORE" is not supported on some OS iconv_str
str
end
end end
end end

View File

@ -14,6 +14,8 @@
require 'redmine/scm/adapters/abstract_adapter' require 'redmine/scm/adapters/abstract_adapter'
require 'cgi' require 'cgi'
require 'rubygems'
require 'UniversalDetector'
module Redmine module Redmine
module Scm module Scm
@ -239,7 +241,18 @@ module Redmine
diff = [] diff = []
hg *hg_args do |io| hg *hg_args do |io|
io.each_line do |line| io.each_line do |line|
diff << line iconv_line = line
if !line.nil?
detected = UniversalDetector::chardet(line)
enc = detected['encoding']
if !enc.nil?
begin
iconv_line = Iconv.conv('UTF-8', enc, line)
rescue Iconv::Failure => err
end
end
end
diff << iconv_line
end end
end end
diff diff
@ -251,7 +264,18 @@ module Redmine
p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path)) p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io| hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
io.binmode io.binmode
io.read str = io.read
return nil if str.nil?
iconv_str = str
detected = UniversalDetector::chardet(str)
enc = detected['encoding']
if !enc.nil?
begin
iconv_str = Iconv.conv('UTF-8', enc, str)
rescue Iconv::Failure => err
end
end
return iconv_str
end end
rescue HgCommandAborted rescue HgCommandAborted
nil # means not found nil # means not found
@ -266,7 +290,19 @@ module Redmine
next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$} next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3, r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
:identifier => $3) :identifier => $3)
blame.add_line($4.rstrip, r) str = $4.rstrip
iconv_str = str
if !str.nil?
detected = UniversalDetector::chardet(str)
enc = detected['encoding']
if !enc.nil?
begin
iconv_str = Iconv.conv('UTF-8', enc, str)
rescue Iconv::Failure => err
end
end
end
blame.add_line(iconv_str, r)
end end
end end
blame blame