@@ -16,6 +16,7 @@ import (
16
16
"code.gitea.io/gitea/modules/analyze"
17
17
"code.gitea.io/gitea/modules/log"
18
18
"code.gitea.io/gitea/modules/setting"
19
+ "github.com/alecthomas/chroma"
19
20
"github.com/alecthomas/chroma/formatters/html"
20
21
"github.com/alecthomas/chroma/lexers"
21
22
"github.com/alecthomas/chroma/styles"
@@ -66,14 +67,17 @@ func Code(fileName, code string) string {
66
67
htmlbuf := bytes.Buffer {}
67
68
htmlw := bufio .NewWriter (& htmlbuf )
68
69
70
+ var lexer chroma.Lexer
69
71
if val , ok := highlightMapping [filepath .Ext (fileName )]; ok {
70
- //change file name to one with mapped extension so we look that up instead
71
- fileName = "mapped." + val
72
+ //use mapped value to find lexer
73
+ lexer = lexers . Get ( val )
72
74
}
73
75
74
- lexer := lexers .Match (fileName )
75
76
if lexer == nil {
76
- lexer = lexers .Fallback
77
+ lexer = lexers .Match (fileName )
78
+ if lexer == nil {
79
+ lexer = lexers .Fallback
80
+ }
77
81
}
78
82
79
83
iterator , err := lexer .Tokenise (nil , string (code ))
@@ -114,17 +118,20 @@ func File(numLines int, fileName string, code []byte) map[int]string {
114
118
htmlbuf := bytes.Buffer {}
115
119
htmlw := bufio .NewWriter (& htmlbuf )
116
120
121
+ var lexer chroma.Lexer
117
122
if val , ok := highlightMapping [filepath .Ext (fileName )]; ok {
118
- fileName = "test." + val
123
+ lexer = lexers . Get ( val )
119
124
}
120
125
121
- language := analyze .GetCodeLanguage (fileName , code )
122
-
123
- lexer := lexers .Get (language )
124
126
if lexer == nil {
125
- lexer = lexers .Match (fileName )
127
+ language := analyze .GetCodeLanguage (fileName , code )
128
+
129
+ lexer = lexers .Get (language )
126
130
if lexer == nil {
127
- lexer = lexers .Fallback
131
+ lexer = lexers .Match (fileName )
132
+ if lexer == nil {
133
+ lexer = lexers .Fallback
134
+ }
128
135
}
129
136
}
130
137
0 commit comments