]> source.dussan.org Git - redmine.git/commitdiff
Render Textile and Markdown attachments on the preview page (#29752).
authorGo MAEDA <maeda@farend.jp>
Thu, 3 Oct 2019 00:09:28 +0000 (00:09 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 3 Oct 2019 00:09:28 +0000 (00:09 +0000)
Patch by Takenori TAKAKI.

git-svn-id: http://svn.redmine.org/redmine/trunk@18584 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/attachments_helper.rb
app/models/attachment.rb
app/views/attachments/file.html.erb
app/views/common/_markup.html.erb [new file with mode: 0644]
test/fixtures/files/testfile.md [new file with mode: 0644]
test/fixtures/files/testfile.textile [new file with mode: 0644]
test/functional/attachments_controller_test.rb

index 5421731ea9ddeca5573e914bc5d36211fd568049..476b93ef3addf000f7392ed3d3083e95611ac635 100644 (file)
@@ -86,4 +86,14 @@ module AttachmentsHelper
     end
     api.created_on attachment.created_on
   end
+
+  def render_file_content(attachment, content)
+    if attachment.is_markdown?
+      render :partial => 'common/markup', :locals => {:markup_text_formatting => 'markdown', :markup_text => content}
+    elsif attachment.is_textile?
+      render :partial => 'common/markup', :locals => {:markup_text_formatting => 'textile', :markup_text => content}
+    else
+      render :partial => 'common/file', :locals => {:content => content, :filename => attachment.filename}
+    end
+  end
 end
index 29d1acc792d934684ada2284e0a4ae1e9e083a13..0cd6664992a9e62e3a73bfeac9a388b42d55f29d 100644 (file)
@@ -240,6 +240,14 @@ class Attachment < ActiveRecord::Base
     Redmine::MimeType.is_type?('text', filename) || Redmine::SyntaxHighlighting.filename_supported?(filename)
   end
 
+  def is_markdown?
+    Redmine::MimeType.of(filename) == 'text/markdown'
+  end
+
+  def is_textile?
+    self.filename =~ /\.textile$/i
+  end
+
   def is_image?
     Redmine::MimeType.is_type?('image', filename)
   end
index af5ea78dcf51983b07c5b8e9700c25ac9c34666f..e19b9127ef90e264dcdf2d502d48a9d76434878e 100644 (file)
@@ -1,4 +1,4 @@
 <%= render :layout => 'layouts/file' do %>
   &nbsp;
-  <%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %>
+  <%= render_file_content(@attachment, @content) %>
 <% end %>
diff --git a/app/views/common/_markup.html.erb b/app/views/common/_markup.html.erb
new file mode 100644 (file)
index 0000000..dc6c7f4
--- /dev/null
@@ -0,0 +1,3 @@
+<div class="wiki">
+  <%= Redmine::WikiFormatting.to_html(markup_text_formatting, Redmine::CodesetUtil.to_utf8_by_setting(markup_text)).html_safe %>
+</div>
diff --git a/test/fixtures/files/testfile.md b/test/fixtures/files/testfile.md
new file mode 100644 (file)
index 0000000..3606a41
--- /dev/null
@@ -0,0 +1,3 @@
+# Header 1
+## Header 2
+### Header 3
diff --git a/test/fixtures/files/testfile.textile b/test/fixtures/files/testfile.textile
new file mode 100644 (file)
index 0000000..6339de5
--- /dev/null
@@ -0,0 +1,5 @@
+h1. Header 1
+
+h2. Header 2
+
+h3. Header 3
index ff2838931e4ca46b755a687d08e6c057124b62b0..b9fad5ec2983621a6c2dd1b7c46f4da1b6826c1c 100644 (file)
@@ -205,6 +205,38 @@ class AttachmentsControllerTest < Redmine::ControllerTest
     end
   end
 
+  def test_show_text_file_formated_markdown
+    set_tmp_attachments_directory
+    a = Attachment.new(:container => Issue.find(1),
+                       :file => uploaded_test_file('testfile.md', 'text/plain'),
+                       :author => User.find(1))
+    assert a.save
+    assert_equal 'testfile.md', a.filename
+
+    get :show, :params => {
+        :id => a.id
+      }
+    assert_response :success
+    assert_equal 'text/html', @response.content_type
+    assert_select 'div.wiki', :html => "<h1>Header 1</h1>\n\n<h2>Header 2</h2>\n\n<h3>Header 3</h3>"
+  end
+
+  def test_show_text_file_fromated_textile
+    set_tmp_attachments_directory
+    a = Attachment.new(:container => Issue.find(1),
+                       :file => uploaded_test_file('testfile.textile', 'text/plain'),
+                       :author => User.find(1))
+    assert a.save
+    assert_equal 'testfile.textile', a.filename
+
+    get :show, :params => {
+        :id => a.id
+      }
+    assert_response :success
+    assert_equal 'text/html', @response.content_type
+    assert_select 'div.wiki', :html => "<h1>Header 1</h1>\n\n\n\t<h2>Header 2</h2>\n\n\n\t<h3>Header 3</h3>"
+  end
+
   def test_show_image
     @request.session[:user_id] = 2
     get :show, :params => {