Merge pull request '[v9.0/forgejo] fix: normalize guessed languages from enry' (#6085) from bp-v9.0/forgejo-7aeb1ba into v9.0/forgejo
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/bitnami/redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/bitnami/valkey:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6085
This commit is contained in:
Earl Warren 2024-11-27 21:39:54 +00:00
commit a167d7b91c
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"))
}
// 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
func File(fileName, language string, code []byte) ([]template.HTML, string, error) {
NewContext()
@ -162,6 +168,9 @@ func File(fileName, language string, code []byte) ([]template.HTML, string, erro
if lexer == nil {
guessLanguage := analyze.GetCodeLanguage(fileName, code)
if normalizedGuessLanguage, ok := normalizeEnryToChroma[guessLanguage]; ok {
guessLanguage = normalizedGuessLanguage
}
lexer = lexers.Get(guessLanguage)
if lexer == nil {

View file

@ -115,6 +115,12 @@ c=2
want: lines(""),
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 {