Defect #31285

Syntax highlighting does not work for attachments with .pl extension

Added by Go MAEDA 3 months ago. Updated 2 months ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:Text formatting
Target version:4.0.4
Resolution:Fixed Affected version:4.0.0

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]

pl-highlight-before.png (20.1 KB) Go MAEDA, 2019-05-01 09:53

pl-highlight-after.png (20.5 KB) Go MAEDA, 2019-05-01 09:53

fix-syntax-highlighting-for-pl-attachments.diff Magnifier - patch (2.76 KB) Go MAEDA, 2019-05-01 10:06

0001-Fix-that-Redmine-SyntaxHighlighting.highlight_by_fil.patch Magnifier (3.7 KB) Go MAEDA, 2019-05-02 08:12


Related issues

Blocks Redmine - Defect #29259: Attachment preview does not work for some source files su... Closed

Associated revisions

Revision 18118
Added by Go MAEDA 2 months ago

Redmine::SyntaxHighlighting.highlight_by_filename may not be able to determine the language by filename alone (#31285).

Patch by Go MAEDA.

Revision 18119
Added by Go MAEDA 2 months ago

Merged r18118 from trunk to 4.0-stable (#31285).

History

#1 Updated by Go MAEDA 3 months ago

  • File deleted (fix-syntax-highlighting-for-pl-attachments.diff)

#3 Updated by Go MAEDA 3 months ago

  • Target version changed from Candidate for next minor release to 4.0.4

#4 Updated by Go MAEDA 3 months ago

Updated the test code.

#5 Updated by Go MAEDA 3 months ago

  • Blocks Defect #29259: Attachment preview does not work for some source files such as JavaScript and Go added

#6 Updated by Go MAEDA 2 months ago

  • Subject changed from Syntax highlighting is not working for attachments with .pl extension to Syntax highlighting does not work for attachments with .pl extension
  • Status changed from New to Closed
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch.

#7 Updated by Go MAEDA 2 months ago

  • Status changed from Closed to Resolved

#8 Updated by Go MAEDA 2 months ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF