From 708550fb7d7440b8baec94fa001ff8a77678eba7 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Thu, 2 May 2019 15:24:34 +0900 Subject: [PATCH 2/2] Attachment preview does not support source code in some languages --- app/models/attachment.rb | 2 +- lib/redmine/syntax_highlighting.rb | 12 ++++++++++++ test/fixtures/files/hello.js | 1 + test/unit/attachment_test.rb | 17 +++++++++++++++++ .../redmine/syntax_highlighting/rouge_test.rb | 12 ++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/files/hello.js diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 45560d0dc..362ac1fde 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -237,7 +237,7 @@ class Attachment < ActiveRecord::Base end def is_text? - Redmine::MimeType.is_type?('text', filename) + Redmine::MimeType.is_type?('text', filename) || Redmine::SyntaxHighlighting.filename_supported?(filename) end def is_image? diff --git a/lib/redmine/syntax_highlighting.rb b/lib/redmine/syntax_highlighting.rb index 6e9aa02a7..01fc0d579 100644 --- a/lib/redmine/syntax_highlighting.rb +++ b/lib/redmine/syntax_highlighting.rb @@ -52,6 +52,14 @@ module Redmine rescue false end + + def filename_supported?(filename) + if highlighter.respond_to? :filename_supported? + highlighter.filename_supported? filename + else + false + end + end end module Rouge @@ -101,6 +109,10 @@ module Redmine def language_supported?(language) find_lexer(language.to_s.downcase) ? true : false end + + def filename_supported?(filename) + !::Rouge::Lexer.guesses(:filename => filename).empty? + end private # Alias names used by CodeRay and not supported by Rouge diff --git a/test/fixtures/files/hello.js b/test/fixtures/files/hello.js new file mode 100644 index 000000000..c0380dc44 --- /dev/null +++ b/test/fixtures/files/hello.js @@ -0,0 +1 @@ +document.write('Hello, World!'); diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 1ed1a719e..3a488bb60 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -502,4 +502,21 @@ class AttachmentTest < ActiveSupport::TestCase puts '(ImageMagick convert not available)' end + def test_is_text + js_attachment = Attachment.new( + :container => Issue.find(1), + :file => uploaded_test_file('hello.js', 'application/javascript'), + :author => User.find(1)) + + to_test = { + js_attachment => true, # hello.js (application/javascript) + attachments(:attachments_003) => false, # logo.gif (image/gif) + attachments(:attachments_004) => true, # source.rb (application/x-ruby) + attachments(:attachments_015) => true, # private.diff (text/x-diff) + attachments(:attachments_016) => false, # testfile.png (image/png) + } + to_test.each do |attachment, expected| + assert_equal expected, attachment.is_text? + end + end end diff --git a/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb b/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb index 0bee260e1..dc0ffcd35 100644 --- a/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb +++ b/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb @@ -20,6 +20,18 @@ require File.expand_path('../../../../../test_helper', __FILE__) class Redmine::SyntaxHighlighting::RougeTest < ActiveSupport::TestCase + def test_filename_supported + to_test = { + 'application.js' => true, + 'Gemfile' => true, + 'AUTOEXEC.BAT' => false, + 'HELLO.C' => true + } + to_test.each do |filename, expected| + assert_equal expected, Redmine::SyntaxHighlighting::Rouge.filename_supported?(filename) + end + end + def test_highlight_by_filename_should_distinguish_perl_and_prolog raw_perl = <<'RAW_PERL' #!/usr/bin/perl -- 2.20.1 (Apple Git-117)