Skip to content

Commit

Permalink
Use existing analyzer module for language detection for highlighting (g…
Browse files Browse the repository at this point in the history
…o-gitea#13522)

* 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>
  • Loading branch information
4 people committed Nov 13, 2020
1 parent ee0097f commit 5003eca
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5003eca

Please sign in to comment.