Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d719d7

Browse files
committedApr 22, 2023
improve
1 parent fb32b4c commit 4d719d7

File tree

11 files changed

+96
-40
lines changed

11 files changed

+96
-40
lines changed
 

‎modules/templates/helper.go

+5-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"mime"
1616
"net/url"
1717
"path/filepath"
18-
"reflect"
1918
"regexp"
2019
"strings"
2120
"time"
@@ -68,11 +67,15 @@ func NewFuncMap() []template.FuncMap {
6867
"PathEscape": url.PathEscape,
6968
"PathEscapeSegments": util.PathEscapeSegments,
7069

70+
// utils
71+
"StringUtils": NewStringUtils,
72+
"SliceUtils": NewSliceUtils,
73+
7174
// -----------------------------------------------------------------
7275
// string / json
76+
// TODO: move string helper functions to StringUtils
7377
"Join": strings.Join,
7478
"DotEscape": DotEscape,
75-
"HasPrefix": strings.HasPrefix,
7679
"EllipsisString": base.EllipsisString,
7780

7881
"Json": func(in interface{}) string {
@@ -143,35 +146,6 @@ func NewFuncMap() []template.FuncMap {
143146
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
144147
},
145148

146-
// -----------------------------------------------------------------
147-
// slice
148-
"containGeneric": func(arr, v interface{}) bool {
149-
arrV := reflect.ValueOf(arr)
150-
if arrV.Kind() == reflect.String && reflect.ValueOf(v).Kind() == reflect.String {
151-
return strings.Contains(arr.(string), v.(string))
152-
}
153-
if arrV.Kind() == reflect.Slice {
154-
for i := 0; i < arrV.Len(); i++ {
155-
iV := arrV.Index(i)
156-
if !iV.CanInterface() {
157-
continue
158-
}
159-
if iV.Interface() == v {
160-
return true
161-
}
162-
}
163-
}
164-
return false
165-
},
166-
"contain": func(s []int64, id int64) bool {
167-
for i := 0; i < len(s); i++ {
168-
if s[i] == id {
169-
return true
170-
}
171-
}
172-
return false
173-
},
174-
175149
// -----------------------------------------------------------------
176150
// setting
177151
"AppName": func() string {
File renamed without changes.

‎modules/templates/util_slice.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import (
7+
"reflect"
8+
)
9+
10+
type SliceUtils struct{}
11+
12+
func NewSliceUtils() *SliceUtils {
13+
return &SliceUtils{}
14+
}
15+
16+
func (su *SliceUtils) Contains(s, v any) bool {
17+
if s == nil {
18+
return false
19+
}
20+
sv := reflect.ValueOf(s)
21+
if sv.Kind() != reflect.Slice && sv.Kind() != reflect.Array {
22+
panic("invalid type, expected slice or array")
23+
}
24+
for i := 0; i < sv.Len(); i++ {
25+
it := sv.Index(i)
26+
if !it.CanInterface() {
27+
continue
28+
}
29+
if it.Interface() == v {
30+
return true
31+
}
32+
}
33+
return false
34+
}

‎modules/templates/util_string.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import "strings"
7+
8+
type StringUtils struct{}
9+
10+
func NewStringUtils() *StringUtils {
11+
return &StringUtils{}
12+
}
13+
14+
func (su *StringUtils) HasPrefix(s, prefix string) bool {
15+
return strings.HasPrefix(s, prefix)
16+
}
17+
18+
func (su *StringUtils) Contains(s, substr string) bool {
19+
return strings.Contains(s, substr)
20+
}

‎modules/templates/util_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package templates
55

66
import (
7+
"html/template"
8+
"strings"
79
"testing"
810

911
"github.com/stretchr/testify/assert"
@@ -41,3 +43,29 @@ func TestDict(t *testing.T) {
4143
assert.Error(t, err)
4244
}
4345
}
46+
47+
func TestUtils(t *testing.T) {
48+
execTmpl := func(code string, data any) string {
49+
tmpl := template.New("test")
50+
tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils})
51+
template.Must(tmpl.Parse(code))
52+
w := &strings.Builder{}
53+
assert.NoError(t, tmpl.Execute(w, data))
54+
return w.String()
55+
}
56+
57+
actual := execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "a"})
58+
assert.Equal(t, "true", actual)
59+
60+
actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "x"})
61+
assert.Equal(t, "false", actual)
62+
63+
actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []int64{1, 2}, "Value": int64(2)})
64+
assert.Equal(t, "true", actual)
65+
66+
actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "b"})
67+
assert.Equal(t, "true", actual)
68+
69+
actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "x"})
70+
assert.Equal(t, "false", actual)
71+
}

‎templates/repo/graph/commits.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{{range $commit.Refs}}
3636
{{$refGroup := .RefGroup}}
3737
{{if eq $refGroup "pull"}}
38-
{{if or (not $.HidePRRefs) (containGeneric $.SelectedBranches .Name)}}
38+
{{if or (not $.HidePRRefs) (SliceUtils.Contains $.SelectedBranches .Name)}}
3939
<!-- it's intended to use issues not pulls, if it's a pull you will get redirected -->
4040
<a class="ui labelled icon button basic tiny gt-mr-2" href="{{$.RepoLink}}/{{if $.Repository.UnitEnabled $.Context $.UnitTypePullRequests}}pulls{{else}}issues{{end}}/{{.ShortName|PathEscape}}">
4141
{{svg "octicon-git-pull-request" 16 "gt-mr-2"}}#{{.ShortName}}

‎templates/repo/header.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
{{end}}
218218

219219
{{if or (.Permission.CanRead $.UnitTypeWiki) (.Permission.CanRead $.UnitTypeExternalWiki)}}
220-
<a class="{{if .PageIsWiki}}active {{end}}item" href="{{.RepoLink}}/wiki" {{if and (.Permission.CanRead $.UnitTypeExternalWiki) (not (HasPrefix ((.Repository.MustGetUnit $.Context $.UnitTypeExternalWiki).ExternalWikiConfig.ExternalWikiURL) (.Repository.Link)))}} target="_blank" rel="noopener noreferrer" {{end}}>
220+
<a class="{{if .PageIsWiki}}active {{end}}item" href="{{.RepoLink}}/wiki" {{if and (.Permission.CanRead $.UnitTypeExternalWiki) (not (StringUtils.HasPrefix ((.Repository.MustGetUnit $.Context $.UnitTypeExternalWiki).ExternalWikiConfig.ExternalWikiURL) (.Repository.Link)))}} target="_blank" rel="noopener noreferrer" {{end}}>
221221
{{svg "octicon-book"}} {{.locale.Tr "repo.wiki"}}
222222
</a>
223223
{{end}}

‎templates/repo/issue/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
{{end}}
228228
{{$previousExclusiveScope = $exclusiveScope}}
229229
<div class="item issue-action" data-action="toggle" data-element-id="{{.ID}}" data-url="{{$.RepoLink}}/issues/labels">
230-
{{if contain $.SelLabelIDs .ID}}{{if $exclusiveScope}}{{svg "octicon-dot-fill"}}{{else}}{{svg "octicon-check"}}{{end}}{{end}} {{RenderLabel $.Context .}}
230+
{{if SliceUtils.Contains $.SelLabelIDs .ID}}{{if $exclusiveScope}}{{svg "octicon-dot-fill"}}{{else}}{{svg "octicon-check"}}{{end}}{{end}} {{RenderLabel $.Context .}}
231231
</div>
232232
{{end}}
233233
</div>

‎templates/repo/issue/milestone_issues.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<span class="info">{{.locale.Tr "repo.issues.filter_label_exclude" | Safe}}</span>
6666
<a class="item" href="{{$.Link}}?q={{$.Keyword}}&type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&assignee={{$.AssigneeID}}&poster={{$.PosterID}}">{{.locale.Tr "repo.issues.filter_label_no_select"}}</a>
6767
{{range .Labels}}
68-
<a class="item label-filter-item" href="{{$.Link}}?q={{$.Keyword}}&type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&labels={{.QueryString}}&assignee={{$.AssigneeID}}&poster={{$.PosterID}}" data-label-id="{{.ID}}">{{if .IsExcluded}}{{svg "octicon-circle-slash"}}{{else if contain $.SelLabelIDs .ID}}{{svg "octicon-check"}}{{end}} {{RenderLabel $.Context .}}</a>
68+
<a class="item label-filter-item" href="{{$.Link}}?q={{$.Keyword}}&type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&labels={{.QueryString}}&assignee={{$.AssigneeID}}&poster={{$.PosterID}}" data-label-id="{{.ID}}">{{if .IsExcluded}}{{svg "octicon-circle-slash"}}{{else if SliceUtils.Contains $.SelLabelIDs .ID}}{{svg "octicon-check"}}{{end}} {{RenderLabel $.Context .}}</a>
6969
{{end}}
7070
</div>
7171
</div>
@@ -171,7 +171,7 @@
171171
<div class="menu">
172172
{{range .Labels}}
173173
<div class="item issue-action" data-action="toggle" data-element-id="{{.ID}}" data-url="{{$.RepoLink}}/issues/labels">
174-
{{if contain $.SelLabelIDs .ID}}{{svg "octicon-check"}}{{end}} {{RenderLabel $.Context .}}
174+
{{if SliceUtils.Contains $.SelLabelIDs .ID}}{{svg "octicon-check"}}{{end}} {{RenderLabel $.Context .}}
175175
</div>
176176
{{end}}
177177
</div>

‎templates/repo/issue/view_content/attachments.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div class="gt-f1 gt-p-3">
99
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}" title='{{$.ctxData.locale.Tr "repo.issues.attachment.open_tab" .Name}}'>
1010
{{if FilenameIsImage .Name}}
11-
{{if not (containGeneric $.Content .UUID)}}
11+
{{if not (StringUtils.Contains $.Content .UUID)}}
1212
{{$hasThumbnails = true}}
1313
{{end}}
1414
{{svg "octicon-file"}}
@@ -29,7 +29,7 @@
2929
<div class="ui small thumbnails">
3030
{{- range .Attachments -}}
3131
{{if FilenameIsImage .Name}}
32-
{{if not (containGeneric $.Content .UUID)}}
32+
{{if not (StringUtils.Contains $.Content .UUID)}}
3333
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
3434
<img alt="{{.Name}}" src="{{.DownloadURL}}" title='{{$.ctxData.locale.Tr "repo.issues.attachment.open_tab" .Name}}'>
3535
</a>

‎templates/repo/settings/tags.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@
9292
{{if or .AllowlistUserIDs (and $.Owner.IsOrganization .AllowlistTeamIDs)}}
9393
{{$userIDs := .AllowlistUserIDs}}
9494
{{range $.Users}}
95-
{{if contain $userIDs .ID}}
95+
{{if SliceUtils.Contains $userIDs .ID}}
9696
<a class="ui basic label" href="{{.HomeLink}}">{{avatar $.Context . 26}} {{.GetDisplayName}}</a>
9797
{{end}}
9898
{{end}}
9999
{{if $.Owner.IsOrganization}}
100100
{{$teamIDs := .AllowlistTeamIDs}}
101101
{{range $.Teams}}
102-
{{if contain $teamIDs .ID}}
102+
{{if SliceUtils.Contains $teamIDs .ID}}
103103
<a class="ui basic label" href="{{$.Owner.OrganisationLink}}/teams/{{PathEscape .LowerName}}">{{.Name}}</a>
104104
{{end}}
105105
{{end}}

0 commit comments

Comments
 (0)
Please sign in to comment.