]> source.dussan.org Git - gitea.git/commitdiff
Use existing analyzer module for language detection for highlighting (#13522)
authormrsdizzie <info@mrsdizzie.com>
Fri, 13 Nov 2020 21:13:41 +0000 (16:13 -0500)
committerGitHub <noreply@github.com>
Fri, 13 Nov 2020 21:13:41 +0000 (16:13 -0500)
* Use existing analyzer module for language detction for highlighting

Thanks @lafriks for pointing out we can reuse existing code for more reliable language detection here.

* Update modules/highlight/highlight.go

Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
modules/highlight/highlight.go

index 1a641081396a04737eb991521f6e69278c355619..914ba8210ebf6d2bc6ce1158776f6975dba9f351 100644 (file)
@@ -13,6 +13,7 @@ import (
        "strings"
        "sync"
 
+       "code.gitea.io/gitea/modules/analyze"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
        "github.com/alecthomas/chroma/formatters/html"
@@ -117,9 +118,11 @@ func File(numLines int, fileName string, code []byte) map[int]string {
                fileName = "test." + val
        }
 
-       lexer := lexers.Match(fileName)
+       language := analyze.GetCodeLanguage(fileName, code)
+
+       lexer := lexers.Get(language)
        if lexer == nil {
-               lexer = lexers.Analyse(string(code))
+               lexer = lexers.Match(fileName)
                if lexer == nil {
                        lexer = lexers.Fallback
                }