Project

General

Profile

Defect #31285 » fix-syntax-highlighting-for-pl-attachments.diff

patch - Go MAEDA, 2019-05-01 10:06

View differences:

lib/redmine/syntax_highlighting.rb (作業コピー)
# See also: https://github.com/jneen/rouge/pull/1078
text = text.gsub(/\r\n?/, "\n")
lexer =::Rouge::Lexer.guess_by_filename(filename)
lexer =::Rouge::Lexer.guess(:source => text, :filename => filename)
formatter = ::Rouge::Formatters::HTML.new
::Rouge.highlight(text, lexer, CustomHTMLLinewise.new(formatter))
end
test/unit/lib/redmine/syntax_highlighting/rouge_test.rb (作業コピー)
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006-2017 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.
require File.expand_path('../../../../../test_helper', __FILE__)
class Redmine::SyntaxHighlighting::RougeTest < ActiveSupport::TestCase
def test_highlight_by_filename_should_distinguish_perl_and_prolog
code_perl, code_prolog = <<'PERL', <<'PROLOG'
#!/usr/bin/perl
print "Hello, world!\n";
PERL
#!/usr/bin/swipl
:- writeln('Hello, world!'),halt.
PROLOG
filename = 'hello.pl'
assert_equal Rouge::Lexers::Perl, Rouge::Lexer.guess(:filename => filename, :source => code_perl)
assert_equal Rouge::Lexers::Prolog, Rouge::Lexer.guess(:filename => filename, :source => code_prolog)
# Rouge raises Rouge::Guesser::Ambiguous exception if it cannot decide
# between prolog and perl
assert_nothing_raised do
html = Redmine::SyntaxHighlighting::Rouge.highlight_by_filename(code_perl, filename)
assert_match '<span class=', html
html = Redmine::SyntaxHighlighting::Rouge.highlight_by_filename(code_prolog, filename)
assert_match '<span class=', html
end
end
end
(3-3/4)