Skip to content

Commit

Permalink
Clean template/helper.go (#23922)
Browse files Browse the repository at this point in the history
The first step of #23328

This PR cleans:

1. Dead function like `SubStr`
2. Unnecessary function like `UseHTTPS`, it should be provided by
`window.origin`
3. Duplicate function like `IsShowFullName`, there was already a
`DeafultShowFullName`

I have searched these removed functions globally, no use in code.
  • Loading branch information
wxiaoguang authored Apr 7, 2023
1 parent 17623bb commit ff2f479
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 77 deletions.
76 changes: 3 additions & 73 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
texttmpl "text/template"
"time"
Expand Down Expand Up @@ -56,12 +55,6 @@ var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}[\s]*$`)
// NewFuncMap returns functions for injecting to templates
func NewFuncMap() []template.FuncMap {
return []template.FuncMap{map[string]interface{}{
"GoVer": func() string {
return util.ToTitleCase(runtime.Version())
},
"UseHTTPS": func() bool {
return strings.HasPrefix(setting.AppURL, "https")
},
"AppName": func() string {
return setting.AppName
},
Expand All @@ -81,10 +74,7 @@ func NewFuncMap() []template.FuncMap {
"AppVer": func() string {
return setting.AppVer
},
"AppBuiltWith": func() string {
return setting.AppBuiltWith
},
"AppDomain": func() string {
"AppDomain": func() string { // documented in mail-templates.md
return setting.Domain
},
"AssetVersion": func() string {
Expand All @@ -108,11 +98,7 @@ func NewFuncMap() []template.FuncMap {
"CustomEmojis": func() map[string]string {
return setting.UI.CustomEmojisMap
},
"IsShowFullName": func() bool {
return setting.UI.DefaultShowFullName
},
"Safe": Safe,
"SafeJS": SafeJS,
"JSEscape": JSEscape,
"Str2html": Str2html,
"TimeSince": timeutil.TimeSince,
Expand Down Expand Up @@ -140,33 +126,15 @@ func NewFuncMap() []template.FuncMap {
"DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z)
},
"DateFmtShort": func(t time.Time) string {
return t.Format("Jan 02, 2006")
},
"CountFmt": base.FormatNumberSI,
"SubStr": func(str string, start, length int) string {
if len(str) == 0 {
return ""
}
end := start + length
if length == -1 {
end = len(str)
}
if len(str) < end {
return str
}
return str[start:end]
},
"CountFmt": base.FormatNumberSI,
"EllipsisString": base.EllipsisString,
"DiffTypeToStr": DiffTypeToStr,
"DiffLineTypeToStr": DiffLineTypeToStr,
"ShortSha": base.ShortSha,
"ActionContent2Commits": ActionContent2Commits,
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
"URLJoin": util.URLJoin,
"RenderCommitMessage": RenderCommitMessage,
"RenderCommitMessageLink": RenderCommitMessageLink,
"RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
"RenderCommitBody": RenderCommitBody,
"RenderCodeBlock": RenderCodeBlock,
Expand Down Expand Up @@ -429,19 +397,13 @@ func NewFuncMap() []template.FuncMap {
curBranch,
)
},
"RefShortName": func(ref string) string {
return git.RefName(ref).ShortName()
},
}}
}

// NewTextFuncMap returns functions for injecting to text templates
// It's a subset of those used for HTML and other templates
func NewTextFuncMap() []texttmpl.FuncMap {
return []texttmpl.FuncMap{map[string]interface{}{
"GoVer": func() string {
return util.ToTitleCase(runtime.Version())
},
"AppName": func() string {
return setting.AppName
},
Expand All @@ -454,33 +416,14 @@ func NewTextFuncMap() []texttmpl.FuncMap {
"AppVer": func() string {
return setting.AppVer
},
"AppBuiltWith": func() string {
return setting.AppBuiltWith
},
"AppDomain": func() string {
"AppDomain": func() string { // documented in mail-templates.md
return setting.Domain
},
"TimeSince": timeutil.TimeSince,
"TimeSinceUnix": timeutil.TimeSinceUnix,
"DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z)
},
"DateFmtShort": func(t time.Time) string {
return t.Format("Jan 02, 2006")
},
"SubStr": func(str string, start, length int) string {
if len(str) == 0 {
return ""
}
end := start + length
if length == -1 {
end = len(str)
}
if len(str) < end {
return str
}
return str[start:end]
},
"EllipsisString": base.EllipsisString,
"URLJoin": util.URLJoin,
"Dict": func(values ...interface{}) (map[string]interface{}, error) {
Expand Down Expand Up @@ -624,11 +567,6 @@ func Safe(raw string) template.HTML {
return template.HTML(raw)
}

// SafeJS renders raw as JS
func SafeJS(raw string) template.JS {
return template.JS(raw)
}

// Str2html render Markdown text to HTML
func Str2html(raw string) template.HTML {
return template.HTML(markup.Sanitize(raw))
Expand Down Expand Up @@ -925,14 +863,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits {
return push
}

// DiffTypeToStr returns diff type name
func DiffTypeToStr(diffType int) string {
diffTypes := map[int]string{
1: "add", 2: "modify", 3: "del", 4: "rename", 5: "copy",
}
return diffTypes[diffType]
}

// DiffLineTypeToStr returns diff line type name
func DiffLineTypeToStr(diffType int) string {
switch diffType {
Expand Down
1 change: 1 addition & 0 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func Config(ctx *context.Context) {

ctx.Data["CustomConf"] = setting.CustomConf
ctx.Data["AppUrl"] = setting.AppURL
ctx.Data["AppBuiltWith"] = setting.AppBuiltWith
ctx.Data["Domain"] = setting.Domain
ctx.Data["OfflineMode"] = setting.OfflineMode
ctx.Data["DisableRouterLog"] = setting.Log.DisableRouterLog
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<dt>{{.locale.Tr "admin.config.app_name"}}</dt>
<dd>{{AppName}}</dd>
<dt>{{.locale.Tr "admin.config.app_ver"}}</dt>
<dd>{{AppVer}}{{AppBuiltWith}}</dd>
<dd>{{AppVer}}{{.AppBuiltWith}}</dd>
<dt>{{.locale.Tr "admin.config.custom_conf"}}</dt>
<dd>{{.CustomConf}}</dd>
<dt>{{.locale.Tr "admin.config.app_url"}}</dt>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/clone_buttons.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- there is always at least one button (by context/repo.go) -->
{{if $.CloneButtonShowHTTPS}}
<button class="ui basic small compact clone button gt-no-transition" id="repo-clone-https" data-link="{{$.CloneButtonOriginLink.HTTPS}}">
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
HTTPS
</button>
{{end}}
{{if $.CloneButtonShowSSH}}
Expand Down
5 changes: 4 additions & 1 deletion templates/repo/clone_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
const value = localStorage.getItem('repo-clone-protocol') || 'https';
const isSSH = value === 'ssh' && sshBtn || value !== 'ssh' && !httpsBtn;
if (httpsBtn) httpsBtn.classList[!isSSH ? 'add' : 'remove']('primary');
if (httpsBtn) {
httpsBtn.textContent = window.origin.split(':')[0].toUpperCase();
httpsBtn.classList[!isSSH ? 'add' : 'remove']('primary');
}
if (sshBtn) sshBtn.classList[isSSH ? 'add' : 'remove']('primary');
const btn = isSSH ? sshBtn : httpsBtn;
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/search_name.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<span class="gt-ellipsis">{{.Name}}{{if IsShowFullName}}<span class="search-fullname"> {{.FullName}}</span>{{end}}</span>
<span class="gt-ellipsis">{{.Name}}{{if DefaultShowFullName}}<span class="search-fullname"> {{.FullName}}</span>{{end}}</span>

0 comments on commit ff2f479

Please sign in to comment.