OSDN Git Service

Text files can now be viewed online when browsing the repository.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 8 May 2007 15:49:20 +0000 (15:49 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 8 May 2007 15:49:20 +0000 (15:49 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@521 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/repositories_controller.rb
app/models/svn_repos.rb
app/views/repositories/entry.rhtml [new file with mode: 0644]
app/views/repositories/revisions.rhtml
lib/redmine.rb
lib/redmine/mime_type.rb [new file with mode: 0644]

index badbf6e..6cb85a8 100644 (file)
@@ -61,10 +61,10 @@ class RepositoriesController < ApplicationController
   end
   
   def entry
-    if 'raw' == params[:format]
-      content = @repository.scm.cat(@path, @rev)
-      show_error and return unless content
-      send_data content, :filename => @path.split('/').last
+    @content = @repository.scm.cat(@path, @rev)
+    show_error and return unless @content
+    if 'raw' == params[:format]      
+      send_data @content, :filename => @path.split('/').last
     end
   end
   
index 3fe698e..b4503ce 100644 (file)
@@ -174,6 +174,7 @@ module SvnRepos
       cmd << " --username #{@login} --password #{@password}" if @login
       cat = nil
       shellout(cmd) do |io|
+        io.binmode
         cat = io.read
       end
       return nil if $? && $?.exitstatus != 0
@@ -248,6 +249,10 @@ module SvnRepos
     def is_dir?
       'dir' == self.kind
     end
+    
+    def is_text?
+      Redmine::MimeType.is_type?('text', name)
+    end
   end
   
   class Revisions < Array
diff --git a/app/views/repositories/entry.rhtml b/app/views/repositories/entry.rhtml
new file mode 100644 (file)
index 0000000..dfceac2
--- /dev/null
@@ -0,0 +1,23 @@
+<h2><%= render :partial => 'navigation', :locals => { :path => @path, :kind => 'file', :revision => @rev } %></h2>
+
+<table class="list">
+  <thead>
+    <tr>
+      <th colspan="2" class="list-filename"><%= @path %></th>
+    </tr>
+  </thead>
+  <tbody>
+    <% line_num = 1 %>
+    <% @content.each_line do |line| %>
+    <tr>
+      <th class="line-num"><%= line_num %></th>
+      <td class="line-code"><%= h(line).gsub(/\s/, '&nbsp;') %></td>
+    </tr>
+    <% line_num += 1 %>
+    <% end %>
+  <tbody>
+</table>
+
+<% content_for :header_tags do %>
+<%= stylesheet_link_tag "scm" %>
+<% end %>
index 91ef64e..4a5b376 100644 (file)
@@ -9,7 +9,12 @@
 
 <% if @entry && @entry.is_file? %>
 <h3><%=h @entry.name %></h3>
-<p><%= link_to l(:button_download), {:action => 'entry', :id => @project, :path => @path, :rev => @rev, :format => 'raw' }, :class => "icon file" %> (<%= number_to_human_size @entry.size %>)</p>
+<p>
+<% if @entry.is_text? %>
+<%= link_to l(:button_view), {:action => 'entry', :id => @project, :path => @path, :rev => @rev } %> |
+<% end %>
+<%= link_to l(:button_download), {:action => 'entry', :id => @project, :path => @path, :rev => @rev, :format => 'raw' } %>
+(<%= number_to_human_size @entry.size %>)</p>
 <% end %>
 
 <h3><%= l(:label_revision_plural) %></h3>
index cfccccc..553ea7f 100644 (file)
@@ -1,2 +1,3 @@
 require 'redmine/version'
+require 'redmine/mime_type'
 require 'redmine/acts_as_watchable/init'
diff --git a/lib/redmine/mime_type.rb b/lib/redmine/mime_type.rb
new file mode 100644 (file)
index 0000000..1177372
--- /dev/null
@@ -0,0 +1,58 @@
+# redMine - project management software
+# Copyright (C) 2006-2007  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+module Redmine
+  module MimeType
+
+    MIME_TYPES = {
+      'text/plain' => 'txt',
+      'text/css' => 'css',
+      'text/html' => 'html,htm,xhtml',
+      'text/x-javascript' => 'js',
+      'text/x-html-template' => 'rhtml',
+      'text/x-ruby' => 'rb,ruby',
+      'image/gif' => 'gif',
+      'image/jpeg' => 'jpg,jpeg,jpe',
+      'image/png' => 'png',
+      'image/tiff' => 'tiff,tif'
+    }.freeze
+    
+    EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
+      exts.split(',').each {|ext| map[ext] = type}
+      map
+    end
+    
+    # returns mime type for name or nil if unknown
+    def self.of(name)
+      return nil unless name
+      m = name.to_s.match(/\.([^\.]+)$/)
+      EXTENSIONS[m[1]] if m
+    end
+    
+    def self.main_mimetype_of(name)
+      mimetype = of(name)
+      mimetype.split('/').first if mimetype
+    end
+    
+    # return true if mime-type for name is type/*
+    # otherwise false
+    def self.is_type?(type, name)
+      main_mimetype = main_mimetype_of(name)
+      type.to_s == main_mimetype
+    end  
+  end
+end