Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-11 22:11:58 +01:00

fix: normalize guessed languages from enry

- In the case that Enry correctly recognized the language, but returns
the language name in a way that isn't recognized by enry. Although
overkill I've added a map such that new entries should be easier to add.
- Resolves #6077
- Added unit test
This commit is contained in:
Gusted 2024-11-27 17:57:02 +01:00
parent 0734596eaa
commit 7aeb1ba1d5
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 15 additions and 0 deletions

View file

@ -134,6 +134,12 @@ func CodeFromLexer(lexer chroma.Lexer, code string) template.HTML {
return template.HTML(strings.TrimSuffix(htmlbuf.String(), "\n")) return template.HTML(strings.TrimSuffix(htmlbuf.String(), "\n"))
} }
// For the case where Enry recognizes the language, but doesn't use the naming
// that Chroma expects.
var normalizeEnryToChroma = map[string]string{
"F#": "FSharp",
}
// File returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name // File returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name
func File(fileName, language string, code []byte) ([]template.HTML, string, error) { func File(fileName, language string, code []byte) ([]template.HTML, string, error) {
NewContext() NewContext()
@ -162,6 +168,9 @@ func File(fileName, language string, code []byte) ([]template.HTML, string, erro
if lexer == nil { if lexer == nil {
guessLanguage := analyze.GetCodeLanguage(fileName, code) guessLanguage := analyze.GetCodeLanguage(fileName, code)
if normalizedGuessLanguage, ok := normalizeEnryToChroma[guessLanguage]; ok {
guessLanguage = normalizedGuessLanguage
}
lexer = lexers.Get(guessLanguage) lexer = lexers.Get(guessLanguage)
if lexer == nil { if lexer == nil {

View file

@ -115,6 +115,12 @@ c=2
want: lines(""), want: lines(""),
lexerName: "ObjectPascal", lexerName: "ObjectPascal",
}, },
{
name: "test.fs",
code: "module Crypt = let generateCryptTable: array<uint32> =",
want: lines(`<span class="k">module</span> <span class="nn">Crypt</span> <span class="o">=</span> <span class="k">let</span> <span class="nv">generateCryptTable</span><span class="o">:</span> <span class="n">array</span><span class="o">&lt;</span><span class="kt">uint32</span><span class="o">&gt;</span> <span class="o">=</span>`),
lexerName: "FSharp",
},
} }
for _, tt := range tests { for _, tt := range tests {