Skip to content

Commit b726040

Browse files
authored
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
1 parent 2a828e2 commit b726040

16 files changed

+108
-132
lines changed

routers/common/middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
func ProtocolMiddlewares() (handlers []any) {
2525
// the order is important
2626
handlers = append(handlers, ChiRoutePathHandler()) // make sure chi has correct paths
27-
handlers = append(handlers, RequestContextHandler()) // // prepare the context and panic recovery
27+
handlers = append(handlers, RequestContextHandler()) // prepare the context and panic recovery
2828

2929
if setting.ReverseProxyLimit > 0 && len(setting.ReverseProxyTrustedProxies) > 0 {
3030
handlers = append(handlers, ForwardedHeadersHandler(setting.ReverseProxyLimit, setting.ReverseProxyTrustedProxies))

routers/web/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ func ExcerptBlob(ctx *context.Context) {
864864
direction := ctx.FormString("direction")
865865
filePath := ctx.FormString("path")
866866
gitRepo := ctx.Repo.GitRepo
867-
if ctx.FormBool("wiki") {
867+
if ctx.Data["PageIsWiki"] == true {
868868
var err error
869869
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
870870
if err != nil {

routers/web/web.go

+4-22
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ func registerRoutes(m *web.Router) {
11661166
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
11671167
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, web.Bind(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
11681168
}, optSignIn, context.RepoAssignment, reqRepoCodeReader)
1169-
// end "/{username}/{reponame}": find, compare, list (code related)
1169+
// end "/{username}/{reponame}": repo code: find, compare, list
11701170

11711171
m.Group("/{username}/{reponame}", func() {
11721172
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) {
14431443
m.Combo("/*").
14441444
Get(repo.Wiki).
14451445
Post(context.RepoMustNotBeArchived(), reqSignIn, reqRepoWikiWriter, web.Bind(forms.NewWikiForm{}), repo.WikiPost)
1446+
m.Get("/blob_excerpt/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
14461447
m.Get("/commit/{sha:[a-f0-9]{7,64}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
14471448
m.Get("/commit/{sha:[a-f0-9]{7,64}}.{ext:patch|diff}", repo.RawDiff)
14481449
m.Get("/raw/*", repo.WikiRaw)
@@ -1518,27 +1519,6 @@ func registerRoutes(m *web.Router) {
15181519
m.Get("", repo.Branches)
15191520
}, repo.MustBeNotEmpty, context.RepoRef())
15201521

1521-
m.Group("/blob_excerpt", func() {
1522-
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
1523-
}, func(ctx *context.Context) {
1524-
// FIXME: refactor this function, use separate routes for wiki/code
1525-
if ctx.FormBool("wiki") {
1526-
ctx.Data["PageIsWiki"] = true
1527-
repo.MustEnableWiki(ctx)
1528-
return
1529-
}
1530-
1531-
if ctx.Written() {
1532-
return
1533-
}
1534-
context.RepoRef()(ctx)
1535-
if ctx.Written() {
1536-
return
1537-
}
1538-
1539-
repo.MustBeNotEmpty(ctx)
1540-
})
1541-
15421522
m.Group("/media", func() {
15431523
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownloadOrLFS)
15441524
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownloadOrLFS)
@@ -1578,6 +1558,8 @@ func registerRoutes(m *web.Router) {
15781558
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RefBlame)
15791559
}, repo.MustBeNotEmpty)
15801560

1561+
m.Get("/blob_excerpt/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
1562+
15811563
m.Group("", func() {
15821564
m.Get("/graph", repo.Graph)
15831565
m.Get("/commit/{sha:([a-f0-9]{7,64})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)

templates/base/head_script.tmpl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{{/*
2-
==== DO NOT EDIT ====
1+
{{/* ==== DO NOT EDIT ====
32
If you are customizing Gitea, please do not change this file.
43
If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
54
*/}}

templates/base/modal_actions_confirm.tmpl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{{/*
2-
Two buttons (negative, positive):
1+
{{/* Two buttons (negative, positive):
32
* ModalButtonTypes: "yes" (default) or "confirm"
43
* ModalButtonCancelText
54
* ModalButtonOkText

templates/repo/diff/blob_excerpt.tmpl

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) (QueryBuild "?" "anchor" $.Anchor)}}
12
{{if $.IsSplitStyle}}
23
{{range $k, $line := $.section.Lines}}
34
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded">
@@ -6,42 +7,48 @@
67
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}">
78
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
89
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
9-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=down&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
10+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=down">
1011
{{svg "octicon-fold-down"}}
1112
</button>
1213
{{end}}
1314
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
14-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=up&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
15+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split&direction=up">
1516
{{svg "octicon-fold-up"}}
1617
</button>
1718
{{end}}
1819
{{if eq $expandDirection 2}}
19-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
20+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=split">
2021
{{svg "octicon-fold"}}
2122
</button>
2223
{{end}}
2324
</div>
2425
</td>
25-
<td colspan="7" class="lines-code lines-code-old ">{{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}{{/*
26-
*/}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}</td>
26+
<td colspan="7" class="lines-code lines-code-old">
27+
{{- $inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale -}}
28+
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
29+
</td>
2730
{{else}}
2831
{{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}
2932
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{$.FileNameHash}}L{{$line.LeftIdx}}{{end}}"></span></td>
3033
<td class="lines-escape lines-escape-old">{{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
3134
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
32-
<td class="lines-code lines-code-old">{{/*
33-
*/}}{{if $line.LeftIdx}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{else}}{{/*
34-
*/}}<code class="code-inner"></code>{{/*
35-
*/}}{{end}}{{/*
36-
*/}}</td>
35+
<td class="lines-code lines-code-old">
36+
{{- if $line.LeftIdx -}}
37+
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
38+
{{- else -}}
39+
<code class="code-inner"></code>
40+
{{- end -}}
41+
</td>
3742
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{$.FileNameHash}}R{{$line.RightIdx}}{{end}}"></span></td>
3843
<td class="lines-escape lines-escape-new">{{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
3944
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
40-
<td class="lines-code lines-code-new">{{/*
41-
*/}}{{if $line.RightIdx}}{{template "repo/diff/section_code" dict "diff" $inlineDiff}}{{else}}{{/*
42-
*/}}<code class="code-inner"></code>{{/*
43-
*/}}{{end}}{{/*
44-
*/}}</td>
45+
<td class="lines-code lines-code-new">
46+
{{- if $line.RightIdx -}}
47+
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
48+
{{- else -}}
49+
<code class="code-inner"></code>
50+
{{- end -}}
51+
</td>
4552
{{end}}
4653
</tr>
4754
{{end}}
@@ -53,17 +60,17 @@
5360
<td colspan="2" class="lines-num">
5461
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
5562
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
56-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
63+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down">
5764
{{svg "octicon-fold-down"}}
5865
</button>
5966
{{end}}
6067
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
61-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
68+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up">
6269
{{svg "octicon-fold-up"}}
6370
</button>
6471
{{end}}
6572
{{if eq $expandDirection 2}}
66-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.RepoLink}}/blob_excerpt/{{PathEscape $.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=&wiki={{$.PageIsWiki}}&anchor={{$.Anchor}}">
73+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified">
6774
{{svg "octicon-fold"}}
6875
</button>
6976
{{end}}

templates/repo/diff/box.tmpl

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
{{template "repo/diff/options_dropdown" .}}
3737
{{if .PageIsPullFiles}}
3838
<div id="diff-commit-select" data-issuelink="{{$.Issue.Link}}" data-queryparams="?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}" data-filter_changes_by_commit="{{ctx.Locale.Tr "repo.pulls.filter_changes_by_commit"}}">
39-
{{/*
40-
the following will be replaced by vue component
41-
but this avoids any loading artifacts till the vue component is initialized
42-
*/}}
39+
{{/* the following will be replaced by vue component, but this avoids any loading artifacts till the vue component is initialized */}}
4340
<div class="ui jump dropdown basic button custom">
4441
{{svg "octicon-git-commit"}}
4542
</div>

templates/repo/diff/escape_title.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{{if .diff.EscapeStatus.HasInvisible}}{{ctx.Locale.Tr "repo.invisible_runes_line"}} {{end}}{{/*
2-
*/}}{{if .diff.EscapeStatus.HasAmbiguous}}{{ctx.Locale.Tr "repo.ambiguous_runes_line"}}{{end}}
1+
{{if .diff.EscapeStatus.HasInvisible}}{{ctx.Locale.Tr "repo.invisible_runes_line"}} {{end -}}
2+
{{- if .diff.EscapeStatus.HasAmbiguous}}{{ctx.Locale.Tr "repo.ambiguous_runes_line"}}{{end}}

0 commit comments

Comments
 (0)