From 562e5cdf324597882b7e6971be1b9a148bbc7839 Mon Sep 17 00:00:00 2001 From: Mai-Lapyst Date: Sat, 16 Mar 2024 01:17:04 +0100 Subject: [PATCH] Get locales directly from context like the other code; add translations for subtitle --- modules/markup/html.go | 34 ++++++++++++++++++------------ modules/markup/html_test.go | 4 ---- modules/markup/renderer.go | 2 -- options/locale/locale_en-US.ini | 4 ++++ services/markup/processorhelper.go | 14 ------------ 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index d0d2530735..1e83dad701 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -5,6 +5,7 @@ package markup import ( "bytes" + "html/template" "io" "net/url" "path" @@ -1065,7 +1066,7 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { if ctx.Metas == nil { return } - if DefaultProcessorHelper.GetRepoFileContent == nil || DefaultProcessorHelper.GetLocale == nil { + if DefaultProcessorHelper.GetRepoFileContent == nil { return } @@ -1119,9 +1120,17 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { lineSpecs := strings.Split(hash, "-") lineCount := len(fileContent) - var subTitle string + commitLinkBuffer := new(bytes.Buffer) + html.Render(commitLinkBuffer, createLink(node.Data[m[0]:m[5]], commitSha[0:7], "text black")) + + var subTitle template.HTML var lineOffset int + locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale) + if !ok { + locale = translation.NewLocale("en-US") + } + if len(lineSpecs) == 1 { line, _ := strconv.Atoi(strings.TrimPrefix(lineSpecs[0], "L")) if line < 1 || line > lineCount { @@ -1129,7 +1138,10 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { } fileContent = fileContent[line-1 : line] - subTitle = "Line " + strconv.Itoa(line) + subTitle = locale.Tr( + "markup.filepreview.line", line, + template.HTML(commitLinkBuffer.String()), + ) lineOffset = line - 1 } else { @@ -1141,7 +1153,10 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { } fileContent = fileContent[startLine-1 : endLine] - subTitle = "Lines " + strconv.Itoa(startLine) + " to " + strconv.Itoa(endLine) + subTitle = locale.Tr( + "markup.filepreview.lines", startLine, endLine, + template.HTML(commitLinkBuffer.String()), + ) lineOffset = startLine - 1 } @@ -1156,12 +1171,6 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { Data: atom.Tbody.String(), } - locale, err := DefaultProcessorHelper.GetLocale(ctx.Ctx) - if err != nil { - log.Error("Unable to get locale. Error: %v", err) - return - } - status := &charset.EscapeStatus{} statuses := make([]*charset.EscapeStatus, len(fileContent)) for i, line := range fileContent { @@ -1286,10 +1295,9 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) { Attr: []html.Attribute{{Key: "class", Val: "text small grey"}}, } psubtitle.AppendChild(&html.Node{ - Type: html.TextNode, - Data: subTitle + " in ", + Type: html.RawNode, + Data: string(subTitle), }) - psubtitle.AppendChild(createLink(urlFull[m[0]:m[5]], commitSha[0:7], "text black")) header.AppendChild(psubtitle) preview := &html.Node{ diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 652db13e5e..c43f006266 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -19,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" @@ -684,9 +683,6 @@ func TestRender_FilePreview(t *testing.T) { buf := []byte("A\nB\nC\nD\n") return highlight.PlainText(buf), nil }, - GetLocale: func(ctx context.Context) (translation.Locale, error) { - return translation.NewLocale("en-US"), nil - }, }) sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579" diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 37d3fde58c..b6d742e5ce 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -17,7 +17,6 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/util" "github.com/yuin/goldmark/ast" @@ -34,7 +33,6 @@ const ( type ProcessorHelper struct { IsUsernameMentionable func(ctx context.Context, username string) bool GetRepoFileContent func(ctx context.Context, ownerName, repoName, commitSha, filePath string) ([]template.HTML, error) - GetLocale func(ctx context.Context) (translation.Locale, error) ElementDir string // the direction of the elements, eg: "ltr", "rtl", "auto", default to no direction attribute } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a7f4de48a8..ebc8db24ca 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3707,3 +3707,7 @@ normal_file = Normal file executable_file = Executable file symbolic_link = Symbolic link submodule = Submodule + +[markup] +filepreview.line = Line %[1]d in %[3]s +filepreview.lines = Lines %[1]d to %[2]d in %[3]s diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go index ab6a66b367..df96f25ce9 100644 --- a/services/markup/processorhelper.go +++ b/services/markup/processorhelper.go @@ -17,7 +17,6 @@ import ( "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/translation" gitea_context "code.gitea.io/gitea/services/context" file_service "code.gitea.io/gitea/services/repository/files" ) @@ -100,18 +99,5 @@ func ProcessorHelper() *markup.ProcessorHelper { return fileContent, nil }, - GetLocale: func(ctx context.Context) (translation.Locale, error) { - giteaCtx, ok := ctx.(*gitea_context.Context) - if ok { - return giteaCtx.Locale, nil - } - - giteaBaseCtx, ok := ctx.(*gitea_context.Base) - if ok { - return giteaBaseCtx.Locale, nil - } - - return nil, fmt.Errorf("could not retrieve locale from context") - }, } }