mirror of
https://github.com/meineerde/redmine.git
synced 2025-12-20 07:21:12 +00:00
Attachment preview does not work for some source files such as JavaScript and Go (#29259).
Patch by Go MAEDA with the help of Stephan Wenzel's contribution. git-svn-id: http://svn.redmine.org/redmine/trunk@18122 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
cff50a5915
commit
400e1ca2ff
@ -237,7 +237,7 @@ class Attachment < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def is_text?
|
def is_text?
|
||||||
Redmine::MimeType.is_type?('text', filename)
|
Redmine::MimeType.is_type?('text', filename) || Redmine::SyntaxHighlighting.filename_supported?(filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_image?
|
def is_image?
|
||||||
|
|||||||
@ -52,6 +52,14 @@ module Redmine
|
|||||||
rescue
|
rescue
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filename_supported?(filename)
|
||||||
|
if highlighter.respond_to? :filename_supported?
|
||||||
|
highlighter.filename_supported? filename
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Rouge
|
module Rouge
|
||||||
@ -101,7 +109,11 @@ module Redmine
|
|||||||
def language_supported?(language)
|
def language_supported?(language)
|
||||||
find_lexer(language.to_s.downcase) ? true : false
|
find_lexer(language.to_s.downcase) ? true : false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filename_supported?(filename)
|
||||||
|
!::Rouge::Lexer.guesses(:filename => filename).empty?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
# Alias names used by CodeRay and not supported by Rouge
|
# Alias names used by CodeRay and not supported by Rouge
|
||||||
LANG_ALIASES = {
|
LANG_ALIASES = {
|
||||||
|
|||||||
1
test/fixtures/files/hello.js
vendored
Normal file
1
test/fixtures/files/hello.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
document.write('Hello, World!');
|
||||||
@ -502,4 +502,21 @@ class AttachmentTest < ActiveSupport::TestCase
|
|||||||
puts '(ImageMagick convert not available)'
|
puts '(ImageMagick convert not available)'
|
||||||
end
|
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?, attachment.inspect
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,6 +20,18 @@
|
|||||||
require File.expand_path('../../../../../test_helper', __FILE__)
|
require File.expand_path('../../../../../test_helper', __FILE__)
|
||||||
|
|
||||||
class Redmine::SyntaxHighlighting::RougeTest < ActiveSupport::TestCase
|
class Redmine::SyntaxHighlighting::RougeTest < ActiveSupport::TestCase
|
||||||
|
def test_filename_supported
|
||||||
|
to_test = {
|
||||||
|
'application.js' => true,
|
||||||
|
'Gemfile' => true,
|
||||||
|
'AUTOEXEC.BAT' => false, # Rouge does not support BAT files
|
||||||
|
'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
|
def test_highlight_by_filename_should_distinguish_perl_and_prolog
|
||||||
raw_perl = <<'RAW_PERL'
|
raw_perl = <<'RAW_PERL'
|
||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user