Project

General

Profile

Actions

Feature #30429

open

Automatic language detection in syntax highlighting

Added by Go MAEDA almost 6 years ago. Updated almost 6 years ago.

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

0%

Estimated time:
Resolution:

Description

As of Redmine 4.0, you can use >100 languages in syntax highlighter (#24681). However, as you can use so many languages, it is difficult to memorize which languages you can highlight and what language name you should describe highlighting the snippet.

To resolve the problem, I propose a language name "auto". When you use the language name "auto" like the following, Redmine automatically detects the language and highlight the code snippet.

Probably we can use Rouge::Lexer.guess_by_source to implement this feature.


Files

auto-keyword-textile.png (25.8 KB) auto-keyword-textile.png Go MAEDA, 2019-01-15 03:12
Actions #1

Updated by Go MAEDA almost 6 years ago

Go MAEDA wrote:

Probably we can use Rouge::Lexer.guess_by_source to implement this feature.

Unfortunately, I found that guess_by_source is not so useful because it checks only a shebang line for most languages. It means that the following snippet that obviously looks like C language is not detected as C but plain text.

#include <stdio.h>

int main()
{
    printf("Hello, world!\n");
    return 0;
}
2.6.0 :012 > text
 => "#include <stdio.h>\n\nint main()\n{\n    printf(\"Hello, world!\n\");\n    return 0;\n}\n" 
2.6.0 :013 > Rouge::Lexer.guess_by_source(text)
 => Rouge::Lexers::PlainText
Actions #2

Updated by Anonymous almost 6 years ago

+1 I am thinking it would probably be better to have the syntax highlighter guess the language only in case a user forgot to define what language it is, else the manual definition to always override the automatic detection. :)

Actions #3

Updated by Bernhard Rohloff almost 6 years ago

It's for sure a nice improvement for Redmine.
+1

Max Johansson wrote:

I am thinking it would probably be better to have the syntax highlighter guess the language only in case a user forgot to define what language it is, else the manual definition to always override the automatic detection. :)

I would also prefer to have auto detection enabled by default as it would be more convenient and less error prone.

Actions

Also available in: Atom PDF