Feature #30429

Automatic language detection in syntax highlighting

Added by Go MAEDA about 4 years ago. Updated about 4 years ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Text formatting
Target version:-
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.

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

History

#1 Updated by Go MAEDA about 4 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

#2 Updated by Anonymous about 4 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. :)

#3 Updated by Bernhard Rohloff about 4 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.

Also available in: Atom PDF