Project

General

Profile

Feature #29752 » 29752.patch

Takenori TAKAKI, 2018-10-16 08:15

View differences:

app/helpers/attachments_helper.rb
86 86
    end
87 87
    api.created_on attachment.created_on
88 88
  end
89

  
90
  def render_file_content(attachment, content)
91
    if attachment.is_markdown?
92
      render :partial => 'common/markup', :locals => {:markup_text_formatting => 'markdown', :markup_text => content}
93
    elsif attachment.is_textile?
94
      render :partial => 'common/markup', :locals => {:markup_text_formatting => 'textile', :markup_text => content}
95
    else
96
      render :partial => 'common/file', :locals => {:content => content, :filename => attachment.filename}
97
    end
98
  end
89 99
end
app/models/attachment.rb
238 238
    Redmine::MimeType.is_type?('text', filename)
239 239
  end
240 240

  
241
  def is_markdown?
242
    Redmine::MimeType.of(filename) == "text/markdown"
243
  end
244

  
245
  def is_textile?
246
    self.filename =~ /\.(textile)$/i
247
  end
248

  
241 249
  def is_image?
242 250
    Redmine::MimeType.is_type?('image', filename)
243 251
  end
app/views/attachments/file.html.erb
1 1
<%= render :layout => 'layouts/file' do %>
2 2
  &nbsp;
3
  <%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %>
3
  <%= render_file_content(@attachment, @content) %>
4 4
<% end %>
app/views/common/_markup.html.erb
1
<div class="wiki">
2
  <%= Redmine::WikiFormatting.to_html(markup_text_formatting, Redmine::CodesetUtil.to_utf8_by_setting(markup_text)).html_safe %>
3
</div>
test/fixtures/files/testfile.md
1
# Header 1
2
## Header 2
3
### Header 3
test/fixtures/files/testfile.textile
1
h1. Header 1
2

  
3
h2. Header 2
4

  
5
h3. Header 3
test/functional/attachments_controller_test.rb
212 212
    set_tmp_attachments_directory
213 213
  end
214 214

  
215
  def test_show_text_file_formated_markdown
216
    set_tmp_attachments_directory
217
    a = Attachment.new(:container => Issue.find(1),
218
                       :file => uploaded_test_file("testfile.md", "text/plain"),
219
                       :author => User.find(1))
220
    assert a.save
221
    assert_equal 'testfile.md', a.filename
222

  
223
    get :show, :params => {
224
        :id => a.id
225
      }
226
    assert_response :success
227
    assert_equal 'text/html', @response.content_type
228
    assert_select 'div.wiki', :html => "<h1>Header 1</h1>\n\n<h2>Header 2</h2>\n\n<h3>Header 3</h3>"
229
  end
230

  
231
  def test_show_text_file_fromated_textile
232
    set_tmp_attachments_directory
233
    a = Attachment.new(:container => Issue.find(1),
234
                       :file => uploaded_test_file("testfile.textile", "text/plain"),
235
                       :author => User.find(1))
236
    assert a.save
237
    assert_equal 'testfile.textile', a.filename
238

  
239
    get :show, :params => {
240
        :id => a.id
241
      }
242
    assert_response :success
243
    assert_equal 'text/html', @response.content_type
244
    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>"
245
  end
246

  
215 247
  def test_show_image
216 248
    @request.session[:user_id] = 2
217 249
    get :show, :params => {
(1-1/3)