From b7260400f80073648d3491ce667de967668d72a8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 25 Dec 2024 00:51:13 +0800 Subject: [PATCH] Refactor tmpl and blob_excerpt (#32967) 1. do not use `{{/* */}}` to remove spaces, use `{{- -}}` 2. fix "blob_excerpt" endpoint, remove the legacy fragile code: have tested commit diff and wiki diff --- routers/common/middleware.go | 2 +- routers/web/repo/compare.go | 2 +- routers/web/web.go | 26 +---- templates/base/head_script.tmpl | 3 +- templates/base/modal_actions_confirm.tmpl | 3 +- templates/repo/diff/blob_excerpt.tmpl | 43 ++++--- templates/repo/diff/box.tmpl | 5 +- templates/repo/diff/escape_title.tmpl | 4 +- templates/repo/diff/section_split.tmpl | 108 +++++++++--------- templates/repo/diff/section_unified.tmpl | 28 +++-- templates/repo/issue/filter_item_label.tmpl | 2 +- .../repo/issue/sidebar/milestone_list.tmpl | 2 +- templates/repo/issue/view_content/pull.tmpl | 3 +- templates/repo/pulls/status.tmpl | 3 +- templates/repo/release/label.tmpl | 3 +- templates/shared/combomarkdowneditor.tmpl | 3 +- 16 files changed, 108 insertions(+), 132 deletions(-) diff --git a/routers/common/middleware.go b/routers/common/middleware.go index 047d327ce8..12b0c67b01 100644 --- a/routers/common/middleware.go +++ b/routers/common/middleware.go @@ -24,7 +24,7 @@ import ( func ProtocolMiddlewares() (handlers []any) { // the order is important handlers = append(handlers, ChiRoutePathHandler()) // make sure chi has correct paths - handlers = append(handlers, RequestContextHandler()) // // prepare the context and panic recovery + handlers = append(handlers, RequestContextHandler()) // prepare the context and panic recovery if setting.ReverseProxyLimit > 0 && len(setting.ReverseProxyTrustedProxies) > 0 { handlers = append(handlers, ForwardedHeadersHandler(setting.ReverseProxyLimit, setting.ReverseProxyTrustedProxies)) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 6c59421bda..8c4003690a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -864,7 +864,7 @@ func ExcerptBlob(ctx *context.Context) { direction := ctx.FormString("direction") filePath := ctx.FormString("path") gitRepo := ctx.Repo.GitRepo - if ctx.FormBool("wiki") { + if ctx.Data["PageIsWiki"] == true { var err error gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { diff --git a/routers/web/web.go b/routers/web/web.go index 4f2a8b72c0..5e0995545e 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1166,7 +1166,7 @@ func registerRoutes(m *web.Router) { Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, web.Bind(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) }, optSignIn, context.RepoAssignment, reqRepoCodeReader) - // end "/{username}/{reponame}": find, compare, list (code related) + // end "/{username}/{reponame}": repo code: find, compare, list m.Group("/{username}/{reponame}", func() { m.Get("/issues/posters", repo.IssuePosters) // it can't use {type:issues|pulls} because it would conflict with other routes like "/pulls/{index}" @@ -1443,6 +1443,7 @@ func registerRoutes(m *web.Router) { m.Combo("/*"). Get(repo.Wiki). Post(context.RepoMustNotBeArchived(), reqSignIn, reqRepoWikiWriter, web.Bind(forms.NewWikiForm{}), repo.WikiPost) + m.Get("/blob_excerpt/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob) m.Get("/commit/{sha:[a-f0-9]{7,64}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff) m.Get("/commit/{sha:[a-f0-9]{7,64}}.{ext:patch|diff}", repo.RawDiff) m.Get("/raw/*", repo.WikiRaw) @@ -1518,27 +1519,6 @@ func registerRoutes(m *web.Router) { m.Get("", repo.Branches) }, repo.MustBeNotEmpty, context.RepoRef()) - m.Group("/blob_excerpt", func() { - m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob) - }, func(ctx *context.Context) { - // FIXME: refactor this function, use separate routes for wiki/code - if ctx.FormBool("wiki") { - ctx.Data["PageIsWiki"] = true - repo.MustEnableWiki(ctx) - return - } - - if ctx.Written() { - return - } - context.RepoRef()(ctx) - if ctx.Written() { - return - } - - repo.MustBeNotEmpty(ctx) - }) - m.Group("/media", func() { m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownloadOrLFS) m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownloadOrLFS) @@ -1578,6 +1558,8 @@ func registerRoutes(m *web.Router) { m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RefBlame) }, repo.MustBeNotEmpty) + m.Get("/blob_excerpt/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob) + m.Group("", func() { m.Get("/graph", repo.Graph) m.Get("/commit/{sha:([a-f0-9]{7,64})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index c0c7235e3b..7c931e7404 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -1,5 +1,4 @@ -{{/* -==== DO NOT EDIT ==== +{{/* ==== DO NOT EDIT ==== If you are customizing Gitea, please do not change this file. If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. */}} diff --git a/templates/base/modal_actions_confirm.tmpl b/templates/base/modal_actions_confirm.tmpl index 9f7eb4adf2..ec6942b9e7 100644 --- a/templates/base/modal_actions_confirm.tmpl +++ b/templates/base/modal_actions_confirm.tmpl @@ -1,5 +1,4 @@ -{{/* -Two buttons (negative, positive): +{{/* Two buttons (negative, positive): * ModalButtonTypes: "yes" (default) or "confirm" * ModalButtonCancelText * ModalButtonOkText diff --git a/templates/repo/diff/blob_excerpt.tmpl b/templates/repo/diff/blob_excerpt.tmpl index cc2237029b..4089d8fb33 100644 --- a/templates/repo/diff/blob_excerpt.tmpl +++ b/templates/repo/diff/blob_excerpt.tmpl @@ -1,3 +1,4 @@ +{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) (QueryBuild "?" "anchor" $.Anchor)}} {{if $.IsSplitStyle}} {{range $k, $line := $.section.Lines}} @@ -6,42 +7,48 @@
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}} - {{end}} {{if or (eq $expandDirection 3) (eq $expandDirection 4)}} - {{end}} {{if eq $expandDirection 2}} - {{end}}
- {{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}} + + {{- $inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{else}} {{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}} {{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}{{end}} {{if $line.LeftIdx}}{{end}} - {{/* - */}}{{if $line.LeftIdx}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if $line.LeftIdx -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{- else -}} + + {{- end -}} + {{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}{{end}} {{if $line.RightIdx}}{{end}} - {{/* - */}}{{if $line.RightIdx}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if $line.RightIdx -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{- else -}} + + {{- end -}} + {{end}} {{end}} @@ -53,17 +60,17 @@
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}} - {{end}} {{if or (eq $expandDirection 3) (eq $expandDirection 4)}} - {{end}} {{if eq $expandDirection 2}} - {{end}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 53ea4fd2e3..0dfb1fd5a1 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -36,10 +36,7 @@ {{template "repo/diff/options_dropdown" .}} {{if .PageIsPullFiles}}
- {{/* - the following will be replaced by vue component - but this avoids any loading artifacts till the vue component is initialized - */}} + {{/* the following will be replaced by vue component, but this avoids any loading artifacts till the vue component is initialized */}} diff --git a/templates/repo/diff/escape_title.tmpl b/templates/repo/diff/escape_title.tmpl index e70f4021c7..9787ae1d42 100644 --- a/templates/repo/diff/escape_title.tmpl +++ b/templates/repo/diff/escape_title.tmpl @@ -1,2 +1,2 @@ -{{if .diff.EscapeStatus.HasInvisible}}{{ctx.Locale.Tr "repo.invisible_runes_line"}} {{end}}{{/* -*/}}{{if .diff.EscapeStatus.HasAmbiguous}}{{ctx.Locale.Tr "repo.ambiguous_runes_line"}}{{end}} +{{if .diff.EscapeStatus.HasInvisible}}{{ctx.Locale.Tr "repo.invisible_runes_line"}} {{end -}} +{{- if .diff.EscapeStatus.HasAmbiguous}}{{ctx.Locale.Tr "repo.ambiguous_runes_line"}}{{end}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 37b42bcb37..9953db5eb2 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -1,5 +1,5 @@ {{$file := .file}} -{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}} +{{$blobExcerptLink := print (or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink) (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.root.AfterCommitID) "?"}} @@ -20,26 +20,24 @@
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}} - {{end}} {{if or (eq $expandDirection 3) (eq $expandDirection 4)}} - {{end}} {{if eq $expandDirection 2}} - {{end}}
{{$inlineDiff := $section.GetComputedInlineDiffFor $line ctx.Locale}} {{if $inlineDiff.EscapeStatus.Escaped}}{{end}} - {{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{/* - */}} + {{template "repo/diff/section_code" dict "diff" $inlineDiff}} {{else if and (eq .GetType 3) $hasmatch}}{{/* DEL */}} {{$match := index $section.Lines $line.Match}} {{- $leftDiff := ""}}{{if $line.LeftIdx}}{{$leftDiff = $section.GetComputedInlineDiffFor $line ctx.Locale}}{{end}} @@ -47,65 +45,65 @@ {{if $line.LeftIdx}}{{if $leftDiff.EscapeStatus.Escaped}}{{end}}{{end}} - {{/* - */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}}{{if $line.LeftIdx}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $leftDiff}}{{/* - */}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if and $.root.SignedUserID $.root.PageIsPullFiles -}} + + {{- end -}} + {{- if $line.LeftIdx -}} + {{- template "repo/diff/section_code" dict "diff" $leftDiff -}} + {{- else -}} + + {{- end -}} + {{if $match.RightIdx}}{{if $rightDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $match.RightIdx}}{{end}} - {{/* - */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}}{{if $match.RightIdx}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $rightDiff}}{{/* - */}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if and $.root.SignedUserID $.root.PageIsPullFiles -}} + + {{- end -}} + {{- if $match.RightIdx -}} + {{- template "repo/diff/section_code" dict "diff" $rightDiff -}} + {{- else -}} + + {{- end -}} + {{else}} {{$inlineDiff := $section.GetComputedInlineDiffFor $line ctx.Locale}} {{if $line.LeftIdx}}{{if $inlineDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $line.LeftIdx}}{{end}} - {{/* - */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}}{{if $line.LeftIdx}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{/* - */}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2)) -}} + + {{- end -}} + {{- if $line.LeftIdx -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{- else -}} + + {{- end -}} + {{if $line.RightIdx}}{{if $inlineDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $line.RightIdx}}{{end}} - {{/* - */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}}{{if $line.RightIdx}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{/* - */}}{{else}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}} + + {{- if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3)) -}} + + {{- end -}} + {{- if $line.RightIdx -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{- else -}} + + {{- end -}} + {{end}} {{if and (eq .GetType 3) $hasmatch}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index 708b333291..a06cd2ddd1 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -1,5 +1,5 @@ {{$file := .file}} -{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}} +{{$blobExcerptLink := print (or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink) (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.root.AfterCommitID) "?"}} @@ -16,17 +16,17 @@
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}} - {{end}} {{if or (eq $expandDirection 3) (eq $expandDirection 4)}} - {{end}} {{if eq $expandDirection 2}} - {{end}} @@ -48,18 +48,16 @@ {{if eq .GetType 4}} - {{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{/* - */}} + {{template "repo/diff/section_code" dict "diff" $inlineDiff}} {{else}} - {{/* - */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* - */}}{{end}}{{/* - */}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{/* - */}} + + {{- if and $.root.SignedUserID $.root.PageIsPullFiles -}} + + {{- end -}} + {{- template "repo/diff/section_code" dict "diff" $inlineDiff -}} + {{end}} {{if $line.Comments}} diff --git a/templates/repo/issue/filter_item_label.tmpl b/templates/repo/issue/filter_item_label.tmpl index 88e2e43120..0883d93804 100644 --- a/templates/repo/issue/filter_item_label.tmpl +++ b/templates/repo/issue/filter_item_label.tmpl @@ -1,4 +1,4 @@ -{{/* +{{/* Template Attributes: * "labels" from query string (needed by JS) * QueryLink * Labels diff --git a/templates/repo/issue/sidebar/milestone_list.tmpl b/templates/repo/issue/sidebar/milestone_list.tmpl index 0e926f7b03..8b3e5b0eee 100644 --- a/templates/repo/issue/sidebar/milestone_list.tmpl +++ b/templates/repo/issue/sidebar/milestone_list.tmpl @@ -6,7 +6,7 @@ {{if $pageMeta.Issue}}data-update-url="{{$pageMeta.RepoLink}}/issues/milestone?issue_ids={{$pageMeta.Issue.ID}}"{{end}} > -