summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2019-05-03 07:31:46 +0000
committerGo MAEDA <maeda@farend.jp>2019-05-03 07:31:46 +0000
commitdea03717b9e8deeba5071c5dbcd29f410d8139ac (patch)
tree95b33f5b99042dedc1daee64af90b003f49cc10d
parent3d91bff0fbee266f6a5c0ab2b35089931571d31a (diff)
downloadredmine-dea03717b9e8deeba5071c5dbcd29f410d8139ac.tar.gz
redmine-dea03717b9e8deeba5071c5dbcd29f410d8139ac.zip
Redmine::SyntaxHighlighting.highlight_by_filename may not be able to determine the language by filename alone (#31285).
Patch by Go MAEDA. git-svn-id: http://svn.redmine.org/redmine/trunk@18118 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--lib/redmine/syntax_highlighting.rb2
-rw-r--r--test/unit/lib/redmine/syntax_highlighting/rouge_test.rb53
2 files changed, 54 insertions, 1 deletions
diff --git a/lib/redmine/syntax_highlighting.rb b/lib/redmine/syntax_highlighting.rb
index 396e63b6e..6e9aa02a7 100644
--- a/lib/redmine/syntax_highlighting.rb
+++ b/lib/redmine/syntax_highlighting.rb
@@ -85,7 +85,7 @@ module Redmine
# 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
diff --git a/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb b/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb
new file mode 100644
index 000000000..0bee260e1
--- /dev/null
+++ b/test/unit/lib/redmine/syntax_highlighting/rouge_test.rb
@@ -0,0 +1,53 @@
+# 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
+ raw_perl = <<'RAW_PERL'
+#!/usr/bin/perl
+print "Hello, world!\n";
+RAW_PERL
+ expected_perl = <<'EXPECTED_PERL'
+<span class="c1">#!/usr/bin/perl</span>
+<span class="k">print</span> <span class="s">"Hello, world!\n"</span><span class="p">;</span>
+EXPECTED_PERL
+ raw_prolog = <<'RAW_PROLOG'
+#!/usr/bin/swipl
+:- writeln('Hello, world!'),halt.
+RAW_PROLOG
+ expected_prolog = <<'EXPECTED_PROLOG'
+<span class="c1">#!/usr/bin/swipl</span>
+<span class="p">:-</span> <span class="ss">writeln</span><span class="p">(</span><span class="ss">'Hello, world!'</span><span class="p">),</span><span class="ss">halt</span><span class="p">.</span>
+EXPECTED_PROLOG
+
+ filename = 'hello.pl'
+
+ # Rouge cannot distinguish between Perl and Prolog by filename alone
+ assert_raises Rouge::Guesser::Ambiguous do
+ Rouge::Lexer.guess(:filename => filename)
+ end
+ assert_equal Rouge::Lexers::Perl, Rouge::Lexer.guess(:filename => filename, :source => raw_perl)
+ assert_equal Rouge::Lexers::Prolog, Rouge::Lexer.guess(:filename => filename, :source => raw_prolog)
+
+ assert_equal expected_perl, Redmine::SyntaxHighlighting::Rouge.highlight_by_filename(raw_perl, filename)
+ assert_equal expected_prolog, Redmine::SyntaxHighlighting::Rouge.highlight_by_filename(raw_prolog, filename)
+ end
+end