Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 05:06:11 +01:00

Clean template/helper.go (#23922)

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.
This commit is contained in:
wxiaoguang 2023-04-07 15:31:41 +08:00 committed by GitHub
parent 17623bba0d
commit ff2f479a4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 77 deletions

View file

@ -18,7 +18,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"runtime"
"strings" "strings"
texttmpl "text/template" texttmpl "text/template"
"time" "time"
@ -56,12 +55,6 @@ var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}[\s]*$`)
// NewFuncMap returns functions for injecting to templates // NewFuncMap returns functions for injecting to templates
func NewFuncMap() []template.FuncMap { func NewFuncMap() []template.FuncMap {
return []template.FuncMap{map[string]interface{}{ 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 { "AppName": func() string {
return setting.AppName return setting.AppName
}, },
@ -81,10 +74,7 @@ func NewFuncMap() []template.FuncMap {
"AppVer": func() string { "AppVer": func() string {
return setting.AppVer return setting.AppVer
}, },
"AppBuiltWith": func() string { "AppDomain": func() string { // documented in mail-templates.md
return setting.AppBuiltWith
},
"AppDomain": func() string {
return setting.Domain return setting.Domain
}, },
"AssetVersion": func() string { "AssetVersion": func() string {
@ -108,11 +98,7 @@ func NewFuncMap() []template.FuncMap {
"CustomEmojis": func() map[string]string { "CustomEmojis": func() map[string]string {
return setting.UI.CustomEmojisMap return setting.UI.CustomEmojisMap
}, },
"IsShowFullName": func() bool {
return setting.UI.DefaultShowFullName
},
"Safe": Safe, "Safe": Safe,
"SafeJS": SafeJS,
"JSEscape": JSEscape, "JSEscape": JSEscape,
"Str2html": Str2html, "Str2html": Str2html,
"TimeSince": timeutil.TimeSince, "TimeSince": timeutil.TimeSince,
@ -140,25 +126,8 @@ func NewFuncMap() []template.FuncMap {
"DateFmtLong": func(t time.Time) string { "DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z) return t.Format(time.RFC1123Z)
}, },
"DateFmtShort": func(t time.Time) string {
return t.Format("Jan 02, 2006")
},
"CountFmt": base.FormatNumberSI, "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]
},
"EllipsisString": base.EllipsisString, "EllipsisString": base.EllipsisString,
"DiffTypeToStr": DiffTypeToStr,
"DiffLineTypeToStr": DiffLineTypeToStr, "DiffLineTypeToStr": DiffLineTypeToStr,
"ShortSha": base.ShortSha, "ShortSha": base.ShortSha,
"ActionContent2Commits": ActionContent2Commits, "ActionContent2Commits": ActionContent2Commits,
@ -166,7 +135,6 @@ func NewFuncMap() []template.FuncMap {
"PathEscapeSegments": util.PathEscapeSegments, "PathEscapeSegments": util.PathEscapeSegments,
"URLJoin": util.URLJoin, "URLJoin": util.URLJoin,
"RenderCommitMessage": RenderCommitMessage, "RenderCommitMessage": RenderCommitMessage,
"RenderCommitMessageLink": RenderCommitMessageLink,
"RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject, "RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
"RenderCommitBody": RenderCommitBody, "RenderCommitBody": RenderCommitBody,
"RenderCodeBlock": RenderCodeBlock, "RenderCodeBlock": RenderCodeBlock,
@ -429,9 +397,6 @@ func NewFuncMap() []template.FuncMap {
curBranch, curBranch,
) )
}, },
"RefShortName": func(ref string) string {
return git.RefName(ref).ShortName()
},
}} }}
} }
@ -439,9 +404,6 @@ func NewFuncMap() []template.FuncMap {
// It's a subset of those used for HTML and other templates // It's a subset of those used for HTML and other templates
func NewTextFuncMap() []texttmpl.FuncMap { func NewTextFuncMap() []texttmpl.FuncMap {
return []texttmpl.FuncMap{map[string]interface{}{ return []texttmpl.FuncMap{map[string]interface{}{
"GoVer": func() string {
return util.ToTitleCase(runtime.Version())
},
"AppName": func() string { "AppName": func() string {
return setting.AppName return setting.AppName
}, },
@ -454,10 +416,7 @@ func NewTextFuncMap() []texttmpl.FuncMap {
"AppVer": func() string { "AppVer": func() string {
return setting.AppVer return setting.AppVer
}, },
"AppBuiltWith": func() string { "AppDomain": func() string { // documented in mail-templates.md
return setting.AppBuiltWith
},
"AppDomain": func() string {
return setting.Domain return setting.Domain
}, },
"TimeSince": timeutil.TimeSince, "TimeSince": timeutil.TimeSince,
@ -465,22 +424,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
"DateFmtLong": func(t time.Time) string { "DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z) 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, "EllipsisString": base.EllipsisString,
"URLJoin": util.URLJoin, "URLJoin": util.URLJoin,
"Dict": func(values ...interface{}) (map[string]interface{}, error) { "Dict": func(values ...interface{}) (map[string]interface{}, error) {
@ -624,11 +567,6 @@ func Safe(raw string) template.HTML {
return template.HTML(raw) 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 // Str2html render Markdown text to HTML
func Str2html(raw string) template.HTML { func Str2html(raw string) template.HTML {
return template.HTML(markup.Sanitize(raw)) return template.HTML(markup.Sanitize(raw))
@ -925,14 +863,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits {
return push 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 // DiffLineTypeToStr returns diff line type name
func DiffLineTypeToStr(diffType int) string { func DiffLineTypeToStr(diffType int) string {
switch diffType { switch diffType {

View file

@ -115,6 +115,7 @@ func Config(ctx *context.Context) {
ctx.Data["CustomConf"] = setting.CustomConf ctx.Data["CustomConf"] = setting.CustomConf
ctx.Data["AppUrl"] = setting.AppURL ctx.Data["AppUrl"] = setting.AppURL
ctx.Data["AppBuiltWith"] = setting.AppBuiltWith
ctx.Data["Domain"] = setting.Domain ctx.Data["Domain"] = setting.Domain
ctx.Data["OfflineMode"] = setting.OfflineMode ctx.Data["OfflineMode"] = setting.OfflineMode
ctx.Data["DisableRouterLog"] = setting.Log.DisableRouterLog ctx.Data["DisableRouterLog"] = setting.Log.DisableRouterLog

View file

@ -11,7 +11,7 @@
<dt>{{.locale.Tr "admin.config.app_name"}}</dt> <dt>{{.locale.Tr "admin.config.app_name"}}</dt>
<dd>{{AppName}}</dd> <dd>{{AppName}}</dd>
<dt>{{.locale.Tr "admin.config.app_ver"}}</dt> <dt>{{.locale.Tr "admin.config.app_ver"}}</dt>
<dd>{{AppVer}}{{AppBuiltWith}}</dd> <dd>{{AppVer}}{{.AppBuiltWith}}</dd>
<dt>{{.locale.Tr "admin.config.custom_conf"}}</dt> <dt>{{.locale.Tr "admin.config.custom_conf"}}</dt>
<dd>{{.CustomConf}}</dd> <dd>{{.CustomConf}}</dd>
<dt>{{.locale.Tr "admin.config.app_url"}}</dt> <dt>{{.locale.Tr "admin.config.app_url"}}</dt>

View file

@ -1,7 +1,7 @@
<!-- there is always at least one button (by context/repo.go) --> <!-- there is always at least one button (by context/repo.go) -->
{{if $.CloneButtonShowHTTPS}} {{if $.CloneButtonShowHTTPS}}
<button class="ui basic small compact clone button gt-no-transition" id="repo-clone-https" data-link="{{$.CloneButtonOriginLink.HTTPS}}"> <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> </button>
{{end}} {{end}}
{{if $.CloneButtonShowSSH}} {{if $.CloneButtonShowSSH}}

View file

@ -11,7 +11,10 @@
const value = localStorage.getItem('repo-clone-protocol') || 'https'; const value = localStorage.getItem('repo-clone-protocol') || 'https';
const isSSH = value === 'ssh' && sshBtn || value !== 'ssh' && !httpsBtn; 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'); if (sshBtn) sshBtn.classList[isSSH ? 'add' : 'remove']('primary');
const btn = isSSH ? sshBtn : httpsBtn; const btn = isSSH ? sshBtn : httpsBtn;

View file

@ -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>