Project

General

Profile

Actions

Defect #31285

closed

Syntax highlighting does not work for attachments with .pl extension

Added by Go MAEDA over 5 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Text formatting
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

The new syntax highlighter Rouge that was introduced in Redmine 4.0.0 supports many languages including Perl and Prolog.

However, Redmine 4.0.3 does not highlight attachments with ".pl" extension. It is because the extension is used for both Perl and Prolog source codes and Rouge cannot determine the language only with a filename. As a result, Rouge raises Rouge::Guesser::Ambiguous exception (Ambiguous guess: can't decide between ["prolog", "perl"]).

irb(main):001:0> Rouge::Lexer.guess_by_filename('hello.pl')
Traceback (most recent call last):
Rouge::Guesser::Ambiguous (Rouge::Guesser::Ambiguous)

The problem can be easily fixed by replacing Rouge::Lexer::guess_by_filename with Rouge::Lexer::guess.

Index: lib/redmine/syntax_highlighting.rb
===================================================================
--- lib/redmine/syntax_highlighting.rb  (リビジョン 18114)
+++ lib/redmine/syntax_highlighting.rb  (作業コピー)
@@ -85,7 +85,7 @@
           # 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

[Before]

[After]


Files


Related issues

Blocks Redmine - Defect #29259: Attachment preview does not work for some source files such as JavaScript and GoClosedGo MAEDA

Actions
Actions

Also available in: Atom PDF