]> source.dussan.org Git - redmine.git/commitdiff
add Redmine::CodesetUtil and move replacing invalid utf8 logic to it.
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Sat, 16 Apr 2011 06:43:49 +0000 (06:43 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Sat, 16 Apr 2011 06:43:49 +0000 (06:43 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5474 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/repositories_helper.rb
app/views/repositories/_dir_list_content.rhtml
lib/redmine/codeset_util.rb [new file with mode: 0644]

index 9037b39f162a39116af92e387e6839b39570ffc0..fb1d4dd0564ed81355806ab7c391a80afff65cb1 100644 (file)
@@ -16,6 +16,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 require 'iconv'
+require 'redmine/codeset_util'
 
 module RepositoriesHelper
   def format_revision(revision)
@@ -143,35 +144,10 @@ module RepositoriesHelper
         # do nothing here and try the next encoding
       end
     end
-    str = replace_invalid_utf8(str)
+    str = Redmine::CodesetUtil.replace_invalid_utf8(str)
   end
   private :to_utf8_internal
 
-  def replace_invalid_utf8(str)
-    return str if str.nil?
-    if str.respond_to?(:force_encoding)
-      str.force_encoding('UTF-8')
-      if ! str.valid_encoding?
-        str = str.encode("US-ASCII", :invalid => :replace,
-              :undef => :replace, :replace => '?').encode("UTF-8")
-      end
-    else
-      ic = Iconv.new('UTF-8', 'UTF-8')
-      txtar = ""
-      begin
-        txtar += ic.iconv(str)
-      rescue Iconv::IllegalSequence
-        txtar += $!.success
-        str = '?' + $!.failed[1,$!.failed.length]
-        retry
-      rescue
-        txtar += $!.success
-      end
-      str = txtar
-    end
-    str
-  end
-
   def repository_field_tags(form, repository)
     method = repository.class.name.demodulize.underscore + "_field_tags"
     if repository.is_a?(Repository) &&
index fd9dd7afc265c4842a6434eee324449f84d13cc0..a4e08b046e4a72ac30582650eac8ea875dd60818 100644 (file)
@@ -1,8 +1,8 @@
 <% @entries.each do |entry| %>
 <% tr_id = Digest::MD5.hexdigest(entry.path)
    depth = params[:depth].to_i %>
-<%  ent_path = replace_invalid_utf8(entry.path)   %>
-<%  ent_name = replace_invalid_utf8(entry.name)   %>
+<%  ent_path = Redmine::CodesetUtil.replace_invalid_utf8(entry.path)   %>
+<%  ent_name = Redmine::CodesetUtil.replace_invalid_utf8(entry.name)   %>
 <tr id="<%= tr_id %>" class="<%= h params[:parent_id] %> entry <%= entry.kind %>">
 <td style="padding-left: <%=18 * depth%>px;" class="filename">
 <% if entry.is_dir? %>
@@ -21,7 +21,7 @@
 <% changeset = @project.repository.find_changeset_by_name(entry.lastrev.identifier) if entry.lastrev && entry.lastrev.identifier %>
 <td class="revision"><%= link_to_revision(changeset, @project) if changeset %></td>
 <td class="age"><%= distance_of_time_in_words(entry.lastrev.time, Time.now) if entry.lastrev && entry.lastrev.time %></td>
-<td class="author"><%= changeset.nil? ? h(replace_invalid_utf8(entry.lastrev.author.to_s.split('<').first)) : changeset.author if entry.lastrev %></td>
+<td class="author"><%= changeset.nil? ? h(Redmine::CodesetUtil.replace_invalid_utf8(entry.lastrev.author.to_s.split('<').first)) : changeset.author if entry.lastrev %></td>
 <td class="comments"><%=h truncate(changeset.comments, :length => 50) unless changeset.nil? %></td>
 </tr>
 <% end %>
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb
new file mode 100644 (file)
index 0000000..20156b8
--- /dev/null
@@ -0,0 +1,31 @@
+require 'iconv'
+
+module Redmine
+  module CodesetUtil
+
+    def self.replace_invalid_utf8(str)
+      return str if str.nil?
+      if str.respond_to?(:force_encoding)
+        str.force_encoding('UTF-8')
+        if ! str.valid_encoding?
+          str = str.encode("US-ASCII", :invalid => :replace,
+                :undef => :replace, :replace => '?').encode("UTF-8")
+        end
+      else
+        ic = Iconv.new('UTF-8', 'UTF-8')
+        txtar = ""
+        begin
+          txtar += ic.iconv(str)
+        rescue Iconv::IllegalSequence
+          txtar += $!.success
+          str = '?' + $!.failed[1,$!.failed.length]
+          retry
+        rescue
+          txtar += $!.success
+        end
+        str = txtar
+      end
+      str
+    end
+  end
+end