From 6a78b901e1b505b8fb55574129724d78931d8dcf Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 22:36:47 +0100 Subject: [PATCH 01/11] Show syntax lexer name in file view/blame --- modules/highlight/highlight.go | 31 +++++++++++++++++++---------- modules/highlight/highlight_test.go | 3 ++- modules/indexer/code/search.go | 5 ++++- routers/web/repo/blame.go | 13 +++++++++++- routers/web/repo/view.go | 3 ++- services/gitdiff/gitdiff.go | 3 ++- services/gitdiff/highlightdiff.go | 4 ++-- templates/repo/blame.tmpl | 13 ++++-------- templates/repo/file_info.tmpl | 29 +++++++++++++++++++++++++++ templates/repo/view_file.tmpl | 30 ++++------------------------ 10 files changed, 81 insertions(+), 53 deletions(-) create mode 100644 templates/repo/file_info.tmpl diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 65ed74b019950..3806811bf557e 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -56,18 +56,18 @@ func NewContext() { }) } -// Code returns a HTML version of code string with chroma syntax highlighting classes -func Code(fileName, language, code string) string { +// Code returns a HTML version of code string with chroma syntax highlighting classes and the matched lexer name +func Code(fileName, language, code string) (string, string) { NewContext() // diff view newline will be passed as empty, change to literal '\n' so it can be copied // preserve literal newline in blame view if code == "" || code == "\n" { - return "\n" + return "\n", "" } if len(code) > sizeLimit { - return code + return code, "" } var lexer chroma.Lexer @@ -103,7 +103,10 @@ func Code(fileName, language, code string) string { } cache.Add(fileName, lexer) } - return CodeFromLexer(lexer, code) + + lexerName := formatLexerName(lexer.Config().Name) + + return CodeFromLexer(lexer, code), lexerName } // CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes @@ -134,12 +137,12 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string { return strings.TrimSuffix(htmlbuf.String(), "\n") } -// File returns a slice of chroma syntax highlighted HTML lines of code -func File(fileName, language string, code []byte) ([]string, error) { +// File returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name +func File(fileName, language string, code []byte) ([]string, string, error) { NewContext() if len(code) > sizeLimit { - return PlainText(code), nil + return PlainText(code), "", nil } formatter := html.New(html.WithClasses(true), @@ -172,9 +175,11 @@ func File(fileName, language string, code []byte) ([]string, error) { } } + lexerName := formatLexerName(lexer.Config().Name) + iterator, err := lexer.Tokenise(nil, string(code)) if err != nil { - return nil, fmt.Errorf("can't tokenize code: %w", err) + return nil, "", fmt.Errorf("can't tokenize code: %w", err) } tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens()) @@ -185,13 +190,13 @@ func File(fileName, language string, code []byte) ([]string, error) { iterator = chroma.Literator(tokens...) err = formatter.Format(htmlBuf, styles.GitHub, iterator) if err != nil { - return nil, fmt.Errorf("can't format code: %w", err) + return nil, "", fmt.Errorf("can't format code: %w", err) } lines = append(lines, htmlBuf.String()) htmlBuf.Reset() } - return lines, nil + return lines, lexerName, nil } // PlainText returns non-highlighted HTML for code @@ -212,3 +217,7 @@ func PlainText(code []byte) []string { } return m } + +func formatLexerName(name string) string { + return strings.Title(name) +} diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 8f83f4a2f6128..b99ce45428c73 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -78,10 +78,11 @@ c=2 for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - out, err := File(tt.name, "", []byte(tt.code)) + out, lexerName, err := File(tt.name, "", []byte(tt.code)) assert.NoError(t, err) expected := strings.Join(tt.want, "\n") actual := strings.Join(out, "\n") + assert.NotEmpty(t, lexerName) assert.Equal(t, strings.Count(actual, "")) assert.EqualValues(t, expected, actual) }) diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index bb7715bafcdc6..df255fa8758f8 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -94,6 +94,9 @@ func searchResult(result *SearchResult, startIndex, endIndex int) (*Result, erro lineNumbers[i] = startLineNum + i index += len(line) } + + highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String()) + return &Result{ RepoID: result.RepoID, Filename: result.Filename, @@ -102,7 +105,7 @@ func searchResult(result *SearchResult, startIndex, endIndex int) (*Result, erro Language: result.Language, Color: result.Color, LineNumbers: lineNumbers, - FormattedLines: highlight.Code(result.Filename, "", formattedLinesBuffer.String()), + FormattedLines: highlighted, }, nil } diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 64a6f0ec53850..1065d3ef9a388 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -100,6 +100,8 @@ func RefBlame(ctx *context.Context) { ctx.Data["FileName"] = blob.Name() ctx.Data["NumLines"], err = blob.GetBlobLineCount() + ctx.Data["NumLinesSet"] = true + if err != nil { ctx.NotFound("GetBlobLineCount", err) return @@ -237,6 +239,8 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m rows := make([]*blameRow, 0) escapeStatus := &charset.EscapeStatus{} + var lexerName string + i := 0 commitCnt := 0 for _, part := range blameParts { @@ -278,7 +282,13 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m line += "\n" } fileName := fmt.Sprintf("%v", ctx.Data["FileName"]) - line = highlight.Code(fileName, language, line) + line, lexerNameForLine := highlight.Code(fileName, language, line) + + // set lexer name to the first detected lexer. this is certainly suboptimal and + // we should instead highlight the whole file at once + if (lexerName == "") { + lexerName = lexerNameForLine + } br.EscapeStatus, line = charset.EscapeControlHTML(line, ctx.Locale) br.Code = gotemplate.HTML(line) @@ -290,4 +300,5 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m ctx.Data["EscapeStatus"] = escapeStatus ctx.Data["BlameRows"] = rows ctx.Data["CommitCnt"] = commitCnt + ctx.Data["LexerName"] = lexerName } diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index e7aca04819296..f22b7e976933c 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -568,7 +568,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st language = "" } } - fileContent, err := highlight.File(blob.Name(), language, buf) + fileContent, lexerName, err := highlight.File(blob.Name(), language, buf) if err != nil { log.Error("highlight.File failed, fallback to plain text: %v", err) fileContent = highlight.PlainText(buf) @@ -581,6 +581,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } ctx.Data["EscapeStatus"] = status ctx.Data["FileContent"] = fileContent + ctx.Data["LexerName"] = lexerName ctx.Data["LineEscapeStatus"] = statuses } if !isLFSFile { diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 3c8c5c81a5694..d24c0ac082698 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -280,7 +280,8 @@ func DiffInlineWithUnicodeEscape(s template.HTML, locale translation.Locale) Dif // DiffInlineWithHighlightCode makes a DiffInline with code highlight and hidden unicode characters escaped func DiffInlineWithHighlightCode(fileName, language, code string, locale translation.Locale) DiffInline { - status, content := charset.EscapeControlHTML(highlight.Code(fileName, language, code), locale) + highlighted, _ := highlight.Code(fileName, language, code) + status, content := charset.EscapeControlHTML(highlighted, locale) return DiffInline{EscapeStatus: status, Content: template.HTML(content)} } diff --git a/services/gitdiff/highlightdiff.go b/services/gitdiff/highlightdiff.go index 4ceada4d7ec95..727827232d618 100644 --- a/services/gitdiff/highlightdiff.go +++ b/services/gitdiff/highlightdiff.go @@ -91,8 +91,8 @@ func (hcd *highlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB hcd.collectUsedRunes(codeA) hcd.collectUsedRunes(codeB) - highlightCodeA := highlight.Code(filename, language, codeA) - highlightCodeB := highlight.Code(filename, language, codeB) + highlightCodeA, _ := highlight.Code(filename, language, codeA) + highlightCodeB, _ := highlight.Code(filename, language, codeB) highlightCodeA = hcd.convertToPlaceholders(highlightCodeA) highlightCodeB = hcd.convertToPlaceholders(highlightCodeB) diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index b697573d24eaa..e4a10ee57dd56 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -1,14 +1,9 @@
-

-
-
-
- {{.NumLines}} {{.locale.TrN .NumLines "repo.line" "repo.lines"}} -
-
{{FileSize .FileSize}}
-
+

+
+ {{template "repo/file_info" .}}
-
+
{{.locale.Tr "repo.file_raw"}} {{if not .IsViewCommit}} diff --git a/templates/repo/file_info.tmpl b/templates/repo/file_info.tmpl new file mode 100644 index 0000000000000..71864a3caf717 --- /dev/null +++ b/templates/repo/file_info.tmpl @@ -0,0 +1,29 @@ +
+ {{if .FileIsSymlink}} +
+ {{.locale.Tr "repo.symbolic_link"}} +
+ {{end}} + {{if .NumLinesSet}} +
+ {{.NumLines}} {{.locale.TrN .NumLines "repo.line" "repo.lines"}} +
+ {{end}} + {{if .FileSize}} +
+ {{FileSize .FileSize}}{{if .IsLFSFile}} ({{.locale.Tr "repo.stored_lfs"}}){{end}} +
+ {{end}} + {{if .LFSLock}} +
+ {{svg "octicon-lock" 16 "mr-2"}} + {{.LFSLockOwner}} +
+ {{end}} + {{if .LexerName}} +
+ {{.LexerName}} +
+ {{end}} + +
diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 60d2a812defd0..321600a9975fe 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -6,38 +6,16 @@
{{end}} -

-
+

+
{{if .ReadmeInList}} {{svg "octicon-book" 16 "mr-3"}} {{.FileName}} {{else}} -
- {{if .FileIsSymlink}} -
- {{.locale.Tr "repo.symbolic_link"}} -
- {{end}} - {{if .NumLinesSet}} -
- {{.NumLines}} {{.locale.TrN .NumLines "repo.line" "repo.lines"}} -
- {{end}} - {{if .FileSize}} -
- {{FileSize .FileSize}}{{if .IsLFSFile}} ({{.locale.Tr "repo.stored_lfs"}}){{end}} -
- {{end}} - {{if .LFSLock}} -
- {{svg "octicon-lock" 16 "mr-2"}} - {{.LFSLockOwner}} -
- {{end}} -
+ {{template "repo/file_info" .}} {{end}}
-
+
{{if .HasSourceRenderedToggle}}
{{svg "octicon-code" 15}} From 08176e07bd61cb8a36cbc646d864fca4c351b1b2 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:04:02 +0100 Subject: [PATCH 02/11] map fallback lexer to plaintext --- modules/highlight/highlight.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 3806811bf557e..22888978869a1 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -219,5 +219,9 @@ func PlainText(code []byte) []string { } func formatLexerName(name string) string { + if name == "fallback" { + return "Plaintext" + } + return strings.Title(name) } From da7ab697e314e70b0737c566fc93781750790b18 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:05:50 +0100 Subject: [PATCH 03/11] fmt --- routers/web/repo/blame.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 1065d3ef9a388..52d5c3c6e628f 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -286,7 +286,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m // set lexer name to the first detected lexer. this is certainly suboptimal and // we should instead highlight the whole file at once - if (lexerName == "") { + if lexerName == "" { lexerName = lexerNameForLine } From b921f9ffcf94796056187c0e6bbbff765c1a254a Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:08:34 +0100 Subject: [PATCH 04/11] fix whitespace --- templates/repo/file_info.tmpl | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/repo/file_info.tmpl b/templates/repo/file_info.tmpl index 71864a3caf717..6e3f817426d0c 100644 --- a/templates/repo/file_info.tmpl +++ b/templates/repo/file_info.tmpl @@ -25,5 +25,4 @@ {{.LexerName}}
{{end}} -
From 2032aeccec55b8f2ffdcf18b25f395ea22f8426a Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:33:57 +0100 Subject: [PATCH 05/11] fix lint --- modules/highlight/highlight.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 22888978869a1..6226e4bfa297f 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/formatters/html" @@ -223,5 +224,5 @@ func formatLexerName(name string) string { return "Plaintext" } - return strings.Title(name) + return util.ToTitleCase(name) } From c46ac0b9fb7aaa0b1f7a0454dd8609f730d9107e Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:47:27 +0100 Subject: [PATCH 06/11] test for exact lexerName --- modules/highlight/highlight_test.go | 46 +++++++++++++++++++---------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index b99ce45428c73..c4468dbf78102 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -20,31 +20,43 @@ func TestFile(t *testing.T) { name string code string want []string + lexer string }{ { - name: "empty.py", - code: "", - want: lines(""), + name: "empty.py", + code: "", + want: lines(""), + lexer: "Python", }, { - name: "tags.txt", - code: "<>", - want: lines("<>"), + name: "empty.js", + code: "", + want: lines(""), + lexer: "Javascript", }, { - name: "tags.py", - code: "<>", - want: lines(`<>`), + name: "tags.txt", + code: "<>", + want: lines("<>"), + lexer: "Plaintext", }, { - name: "eol-no.py", - code: "a=1", - want: lines(`a=1`), + name: "tags.py", + code: "<>", + want: lines(`<>`), + lexer: "Python", }, { - name: "eol-newline1.py", - code: "a=1\n", - want: lines(`a=1\n`), + name: "eol-no.py", + code: "a=1", + want: lines(`a=1`), + lexer: "Python", + }, + { + name: "eol-newline1.py", + code: "a=1\n", + want: lines(`a=1\n`), + lexer: "Python", }, { name: "eol-newline2.py", @@ -54,6 +66,7 @@ func TestFile(t *testing.T) { \n `, ), + lexer: "Python", }, { name: "empty-line-with-space.py", @@ -73,6 +86,7 @@ c=2 \n c=2`, ), + lexer: "Python", }, } @@ -82,9 +96,9 @@ c=2 assert.NoError(t, err) expected := strings.Join(tt.want, "\n") actual := strings.Join(out, "\n") - assert.NotEmpty(t, lexerName) assert.Equal(t, strings.Count(actual, "")) assert.EqualValues(t, expected, actual) + assert.Equal(t, tt.lexer, lexerName) }) } } From f3184c26a5c06f3e519ade1a8ecbed1e4b22409a Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:47:58 +0100 Subject: [PATCH 07/11] Update templates/repo/file_info.tmpl Co-authored-by: delvh --- templates/repo/file_info.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/file_info.tmpl b/templates/repo/file_info.tmpl index 6e3f817426d0c..90a831fb8e897 100644 --- a/templates/repo/file_info.tmpl +++ b/templates/repo/file_info.tmpl @@ -4,7 +4,7 @@ {{.locale.Tr "repo.symbolic_link"}}
{{end}} - {{if .NumLinesSet}} + {{if .NumLinesSet}}{{/* Explicit attribute needed to show 0 line changes */}}
{{.NumLines}} {{.locale.TrN .NumLines "repo.line" "repo.lines"}}
From a4e26e5793c2a9a3ea72e63fa20c6afeef77899c Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 14 Nov 2022 23:53:12 +0100 Subject: [PATCH 08/11] fix fmt and rename variable --- modules/highlight/highlight_test.go | 62 ++++++++++++++--------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index c4468dbf78102..7518814e1be4b 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -17,46 +17,46 @@ func lines(s string) []string { func TestFile(t *testing.T) { tests := []struct { - name string - code string - want []string - lexer string + name string + code string + want []string + lexerName string }{ { - name: "empty.py", - code: "", - want: lines(""), - lexer: "Python", + name: "empty.py", + code: "", + want: lines(""), + lexerName: "Python", }, { - name: "empty.js", - code: "", - want: lines(""), - lexer: "Javascript", + name: "empty.js", + code: "", + want: lines(""), + lexerName: "Javascript", }, { - name: "tags.txt", - code: "<>", - want: lines("<>"), - lexer: "Plaintext", + name: "tags.txt", + code: "<>", + want: lines("<>"), + lexerName: "Plaintext", }, { - name: "tags.py", - code: "<>", - want: lines(`<>`), - lexer: "Python", + name: "tags.py", + code: "<>", + want: lines(`<>`), + lexerName: "Python", }, { - name: "eol-no.py", - code: "a=1", - want: lines(`a=1`), - lexer: "Python", + name: "eol-no.py", + code: "a=1", + want: lines(`a=1`), + lexerName: "Python", }, { - name: "eol-newline1.py", - code: "a=1\n", - want: lines(`a=1\n`), - lexer: "Python", + name: "eol-newline1.py", + code: "a=1\n", + want: lines(`a=1\n`), + lexerName: "Python", }, { name: "eol-newline2.py", @@ -66,7 +66,7 @@ func TestFile(t *testing.T) { \n `, ), - lexer: "Python", + lexerName: "Python", }, { name: "empty-line-with-space.py", @@ -86,7 +86,7 @@ c=2 \n c=2`, ), - lexer: "Python", + lexerName: "Python", }, } @@ -98,7 +98,7 @@ c=2 actual := strings.Join(out, "\n") assert.Equal(t, strings.Count(actual, "")) assert.EqualValues(t, expected, actual) - assert.Equal(t, tt.lexer, lexerName) + assert.Equal(t, tt.lexerName, lexerName) }) } } From 66ef6e226b8a59891171f3b9df95881b8ce8aacb Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 15 Nov 2022 00:44:26 +0100 Subject: [PATCH 09/11] fix JavaScript case --- modules/highlight/highlight.go | 2 +- modules/highlight/highlight_test.go | 2 +- modules/util/util.go | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 6226e4bfa297f..3836c05880884 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -224,5 +224,5 @@ func formatLexerName(name string) string { return "Plaintext" } - return util.ToTitleCase(name) + return util.ToTitleCaseNoLower(name) } diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 7518814e1be4b..4f791be18597a 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -32,7 +32,7 @@ func TestFile(t *testing.T) { name: "empty.js", code: "", want: lines(""), - lexerName: "Javascript", + lexerName: "JavaScript", }, { name: "tags.txt", diff --git a/modules/util/util.go b/modules/util/util.go index be60fe4b4bbdf..e7dab4b8ddbf8 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -187,12 +187,18 @@ func ToUpperASCII(s string) string { } var titleCaser = cases.Title(language.English) +var titleCaserNoLower = cases.Title(language.English, cases.NoLower) // ToTitleCase returns s with all english words capitalized func ToTitleCase(s string) string { return titleCaser.String(s) } +// ToTitleCaseNoLower returns s with all english words capitalized without lowercasing +func ToTitleCaseNoLower(s string) string { + return titleCaserNoLower.String(s) +} + var ( whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") From 10fb6e991c8fcceabd0a211b53fb9adc6f3e6ce3 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 15 Nov 2022 00:45:14 +0100 Subject: [PATCH 10/11] add YAML test case --- modules/highlight/highlight_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 4f791be18597a..189975374614a 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -34,6 +34,12 @@ func TestFile(t *testing.T) { want: lines(""), lexerName: "JavaScript", }, + { + name: "empty.yaml", + code: "", + want: lines(""), + lexerName: "YAML", + }, { name: "tags.txt", code: "<>", From bd23f063e3749f611da78c435dec4de2eb683117 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 15 Nov 2022 12:02:20 +0100 Subject: [PATCH 11/11] fix fmt and move context variable assignment --- modules/util/util.go | 6 ++++-- routers/web/repo/view.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/util/util.go b/modules/util/util.go index e7dab4b8ddbf8..6df47ca56821f 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -186,8 +186,10 @@ func ToUpperASCII(s string) string { return string(b) } -var titleCaser = cases.Title(language.English) -var titleCaserNoLower = cases.Title(language.English, cases.NoLower) +var ( + titleCaser = cases.Title(language.English) + titleCaserNoLower = cases.Title(language.English, cases.NoLower) +) // ToTitleCase returns s with all english words capitalized func ToTitleCase(s string) string { diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index f22b7e976933c..7500dbb34bb35 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -569,6 +569,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } } fileContent, lexerName, err := highlight.File(blob.Name(), language, buf) + ctx.Data["LexerName"] = lexerName if err != nil { log.Error("highlight.File failed, fallback to plain text: %v", err) fileContent = highlight.PlainText(buf) @@ -581,7 +582,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } ctx.Data["EscapeStatus"] = status ctx.Data["FileContent"] = fileContent - ctx.Data["LexerName"] = lexerName ctx.Data["LineEscapeStatus"] = statuses } if !isLFSFile {