@@ -18,6 +18,7 @@ import (
1818 "code.gitea.io/gitea/modules/analyze"
1919 "code.gitea.io/gitea/modules/log"
2020 "code.gitea.io/gitea/modules/setting"
21+ "code.gitea.io/gitea/modules/util"
2122
2223 "github.com/alecthomas/chroma/v2"
2324 "github.com/alecthomas/chroma/v2/formatters/html"
@@ -56,18 +57,18 @@ func NewContext() {
5657 })
5758}
5859
59- // Code returns a HTML version of code string with chroma syntax highlighting classes
60- func Code (fileName , language , code string ) string {
60+ // Code returns a HTML version of code string with chroma syntax highlighting classes and the matched lexer name
61+ func Code (fileName , language , code string ) ( string , string ) {
6162 NewContext ()
6263
6364 // diff view newline will be passed as empty, change to literal '\n' so it can be copied
6465 // preserve literal newline in blame view
6566 if code == "" || code == "\n " {
66- return "\n "
67+ return "\n " , ""
6768 }
6869
6970 if len (code ) > sizeLimit {
70- return code
71+ return code , ""
7172 }
7273
7374 var lexer chroma.Lexer
@@ -103,7 +104,10 @@ func Code(fileName, language, code string) string {
103104 }
104105 cache .Add (fileName , lexer )
105106 }
106- return CodeFromLexer (lexer , code )
107+
108+ lexerName := formatLexerName (lexer .Config ().Name )
109+
110+ return CodeFromLexer (lexer , code ), lexerName
107111}
108112
109113// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
@@ -134,12 +138,12 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
134138 return strings .TrimSuffix (htmlbuf .String (), "\n " )
135139}
136140
137- // File returns a slice of chroma syntax highlighted HTML lines of code
138- func File (fileName , language string , code []byte ) ([]string , error ) {
141+ // File returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name
142+ func File (fileName , language string , code []byte ) ([]string , string , error ) {
139143 NewContext ()
140144
141145 if len (code ) > sizeLimit {
142- return PlainText (code ), nil
146+ return PlainText (code ), "" , nil
143147 }
144148
145149 formatter := html .New (html .WithClasses (true ),
@@ -172,9 +176,11 @@ func File(fileName, language string, code []byte) ([]string, error) {
172176 }
173177 }
174178
179+ lexerName := formatLexerName (lexer .Config ().Name )
180+
175181 iterator , err := lexer .Tokenise (nil , string (code ))
176182 if err != nil {
177- return nil , fmt .Errorf ("can't tokenize code: %w" , err )
183+ return nil , "" , fmt .Errorf ("can't tokenize code: %w" , err )
178184 }
179185
180186 tokensLines := chroma .SplitTokensIntoLines (iterator .Tokens ())
@@ -185,13 +191,13 @@ func File(fileName, language string, code []byte) ([]string, error) {
185191 iterator = chroma .Literator (tokens ... )
186192 err = formatter .Format (htmlBuf , styles .GitHub , iterator )
187193 if err != nil {
188- return nil , fmt .Errorf ("can't format code: %w" , err )
194+ return nil , "" , fmt .Errorf ("can't format code: %w" , err )
189195 }
190196 lines = append (lines , htmlBuf .String ())
191197 htmlBuf .Reset ()
192198 }
193199
194- return lines , nil
200+ return lines , lexerName , nil
195201}
196202
197203// PlainText returns non-highlighted HTML for code
@@ -212,3 +218,11 @@ func PlainText(code []byte) []string {
212218 }
213219 return m
214220}
221+
222+ func formatLexerName (name string ) string {
223+ if name == "fallback" {
224+ return "Plaintext"
225+ }
226+
227+ return util .ToTitleCaseNoLower (name )
228+ }
0 commit comments