mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-22 04:33:42 +09:00
Backport #37850 Fix #37849 Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
This commit is contained in:
co-authored by
wxiaoguang
TheFox0x7
parent
c044b0f48c
commit
cc3ee01fd8
@@ -63,7 +63,7 @@ func TestFile(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "tags.py",
|
name: "tags.py",
|
||||||
code: "<>",
|
code: "<>",
|
||||||
want: lines(`<span class="o"><</span><span class="o">></span>`),
|
want: lines(`<span class="o"><></span>`),
|
||||||
lexerName: "Python",
|
lexerName: "Python",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -102,7 +102,7 @@ c=2
|
|||||||
<span class="n">def</span><span class="p">:</span>\n
|
<span class="n">def</span><span class="p">:</span>\n
|
||||||
<span class="n">a</span><span class="o">=</span><span class="mi">1</span>\n
|
<span class="n">a</span><span class="o">=</span><span class="mi">1</span>\n
|
||||||
\n
|
\n
|
||||||
<span class="n">b</span><span class="o">=</span><span class="sa"></span><span class="s1">'</span><span class="s1">'</span>\n
|
<span class="n">b</span><span class="o">=</span><span class="s1">''</span>\n
|
||||||
\n
|
\n
|
||||||
<span class="n">c</span><span class="o">=</span><span class="mi">2</span>`,
|
<span class="n">c</span><span class="o">=</span><span class="mi">2</span>`,
|
||||||
),
|
),
|
||||||
@@ -114,6 +114,18 @@ c=2
|
|||||||
want: []template.HTML{"<span class=\"c1\">--\n</span>", `<span class="k">SELECT</span>`},
|
want: []template.HTML{"<span class=\"c1\">--\n</span>", `<span class="k">SELECT</span>`},
|
||||||
lexerName: "SQL",
|
lexerName: "SQL",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "test.http",
|
||||||
|
code: `HTTP/1.0 400 Bad request
|
||||||
|
Content-Type: text/html
|
||||||
|
|
||||||
|
<html></html>`,
|
||||||
|
want: lines(`<span class="kr">HTTP</span><span class="o">/</span><span class="m">1.0</span> <span class="m">400</span> <span class="ne">Bad request</span>\n
|
||||||
|
<span class="n">Content-Type</span><span class="o">:</span> <span class="l">text/html</span>\n
|
||||||
|
\n
|
||||||
|
<span class="p"><</span><span class="nt">html</span><span class="p">></</span><span class="nt">html</span><span class="p">></span>`),
|
||||||
|
lexerName: "HTTP",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
@@ -288,24 +288,24 @@ func detectChromaLexerWithAnalyze(fileName, lang string, code []byte) chroma.Lex
|
|||||||
|
|
||||||
// if lang is provided, and it matches a lexer, use it directly
|
// if lang is provided, and it matches a lexer, use it directly
|
||||||
if byLang {
|
if byLang {
|
||||||
return lexer
|
return chroma.Coalesce(lexer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if a lexer is detected and there is no conflict for the file extension, use it directly
|
// if a lexer is detected and there is no conflict for the file extension, use it directly
|
||||||
fileExt := path.Ext(fileName)
|
fileExt := path.Ext(fileName)
|
||||||
_, hasConflicts := chromaLexers().conflictingExtLangMap[fileExt]
|
_, hasConflicts := chromaLexers().conflictingExtLangMap[fileExt]
|
||||||
if !hasConflicts && lexer != lexers.Fallback {
|
if !hasConflicts && lexer != lexers.Fallback {
|
||||||
return lexer
|
return chroma.Coalesce(lexer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to detect language by content, for best guessing for the language
|
// try to detect language by content, for best guessing for the language
|
||||||
// when using "code" to detect, analyze.GetCodeLanguage is slow, it iterates many rules to detect language from content
|
// when using "code" to detect, analyze.GetCodeLanguage is slow, it iterates many rules to detect language from content
|
||||||
analyzedLanguage := analyze.GetCodeLanguage(fileName, code)
|
analyzedLanguage := analyze.GetCodeLanguage(fileName, code)
|
||||||
lexer = DetectChromaLexerByFileName(fileName, analyzedLanguage)
|
lexer, _ = detectChromaLexerByFileName(fileName, analyzedLanguage)
|
||||||
if lexer == lexers.Fallback {
|
if lexer == lexers.Fallback {
|
||||||
if analyzedLanguage != enry.OtherLanguage {
|
if analyzedLanguage != enry.OtherLanguage {
|
||||||
log.Warn("No chroma lexer found for enry detected language: %s (file: %s), need to fix the language mapping between enry and chroma.", analyzedLanguage, fileName)
|
log.Warn("No chroma lexer found for enry detected language: %s (file: %s), need to fix the language mapping between enry and chroma.", analyzedLanguage, fileName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lexer
|
return chroma.Coalesce(lexer)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user