@@ -18,6 +18,7 @@ import (
18
18
"code.gitea.io/gitea/modules/analyze"
19
19
"code.gitea.io/gitea/modules/log"
20
20
"code.gitea.io/gitea/modules/setting"
21
+ "code.gitea.io/gitea/modules/util"
21
22
22
23
"github.com/alecthomas/chroma/v2"
23
24
"github.com/alecthomas/chroma/v2/formatters/html"
@@ -56,18 +57,18 @@ func NewContext() {
56
57
})
57
58
}
58
59
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 ) {
61
62
NewContext ()
62
63
63
64
// diff view newline will be passed as empty, change to literal '\n' so it can be copied
64
65
// preserve literal newline in blame view
65
66
if code == "" || code == "\n " {
66
- return "\n "
67
+ return "\n " , ""
67
68
}
68
69
69
70
if len (code ) > sizeLimit {
70
- return code
71
+ return code , ""
71
72
}
72
73
73
74
var lexer chroma.Lexer
@@ -103,7 +104,10 @@ func Code(fileName, language, code string) string {
103
104
}
104
105
cache .Add (fileName , lexer )
105
106
}
106
- return CodeFromLexer (lexer , code )
107
+
108
+ lexerName := formatLexerName (lexer .Config ().Name )
109
+
110
+ return CodeFromLexer (lexer , code ), lexerName
107
111
}
108
112
109
113
// 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 {
134
138
return strings .TrimSuffix (htmlbuf .String (), "\n " )
135
139
}
136
140
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 ) {
139
143
NewContext ()
140
144
141
145
if len (code ) > sizeLimit {
142
- return PlainText (code ), nil
146
+ return PlainText (code ), "" , nil
143
147
}
144
148
145
149
formatter := html .New (html .WithClasses (true ),
@@ -172,9 +176,11 @@ func File(fileName, language string, code []byte) ([]string, error) {
172
176
}
173
177
}
174
178
179
+ lexerName := formatLexerName (lexer .Config ().Name )
180
+
175
181
iterator , err := lexer .Tokenise (nil , string (code ))
176
182
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 )
178
184
}
179
185
180
186
tokensLines := chroma .SplitTokensIntoLines (iterator .Tokens ())
@@ -185,13 +191,13 @@ func File(fileName, language string, code []byte) ([]string, error) {
185
191
iterator = chroma .Literator (tokens ... )
186
192
err = formatter .Format (htmlBuf , styles .GitHub , iterator )
187
193
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 )
189
195
}
190
196
lines = append (lines , htmlBuf .String ())
191
197
htmlBuf .Reset ()
192
198
}
193
199
194
- return lines , nil
200
+ return lines , lexerName , nil
195
201
}
196
202
197
203
// PlainText returns non-highlighted HTML for code
@@ -212,3 +218,11 @@ func PlainText(code []byte) []string {
212
218
}
213
219
return m
214
220
}
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