diff options
author | mlpo <mlpo@mlpo.fr> | 2021-05-13 11:31:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-13 12:31:23 +0300 |
commit | 52f8dcda43645a1e961243e46b557f39f6a2d616 (patch) | |
tree | 327a8f0b9d2f8cab2ff8a56edde0ce2a25d90922 /modules/highlight | |
parent | 27b29ffb227ae1f563c38b40679a232f920f9c19 (diff) | |
download | gitea-52f8dcda43645a1e961243e46b557f39f6a2d616.tar.gz gitea-52f8dcda43645a1e961243e46b557f39f6a2d616.zip |
Allow custom highlight mapping beyond file extensions (#15808)
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/highlight')
-rw-r--r-- | modules/highlight/highlight.go | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 914ba8210e..a46499691e 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "github.com/alecthomas/chroma" "github.com/alecthomas/chroma/formatters/html" "github.com/alecthomas/chroma/lexers" "github.com/alecthomas/chroma/styles" @@ -66,14 +67,17 @@ func Code(fileName, code string) string { htmlbuf := bytes.Buffer{} htmlw := bufio.NewWriter(&htmlbuf) + var lexer chroma.Lexer if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { - //change file name to one with mapped extension so we look that up instead - fileName = "mapped." + val + //use mapped value to find lexer + lexer = lexers.Get(val) } - lexer := lexers.Match(fileName) if lexer == nil { - lexer = lexers.Fallback + lexer = lexers.Match(fileName) + if lexer == nil { + lexer = lexers.Fallback + } } iterator, err := lexer.Tokenise(nil, string(code)) @@ -114,17 +118,20 @@ func File(numLines int, fileName string, code []byte) map[int]string { htmlbuf := bytes.Buffer{} htmlw := bufio.NewWriter(&htmlbuf) + var lexer chroma.Lexer if val, ok := highlightMapping[filepath.Ext(fileName)]; ok { - fileName = "test." + val + lexer = lexers.Get(val) } - language := analyze.GetCodeLanguage(fileName, code) - - lexer := lexers.Get(language) if lexer == nil { - lexer = lexers.Match(fileName) + language := analyze.GetCodeLanguage(fileName, code) + + lexer = lexers.Get(language) if lexer == nil { - lexer = lexers.Fallback + lexer = lexers.Match(fileName) + if lexer == nil { + lexer = lexers.Fallback + } } } |