diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 3a5eb5904f..bde50fc4b7 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -25,8 +25,8 @@ import ( "code.gitea.io/gitea/services/gitdiff" ) -// NewFuncMap returns functions for injecting to templates -func NewFuncMap() template.FuncMap { +// newFuncMapWebPage returns functions for injecting to templates +func newFuncMapWebPage() template.FuncMap { return map[string]any{ "DumpVar": dumpVar, "NIL": func() any { return nil }, @@ -40,7 +40,6 @@ func NewFuncMap() template.FuncMap { "QueryEscape": queryEscape, "QueryBuild": QueryBuild, "SanitizeHTML": SanitizeHTML, - "DotEscape": dotEscape, "PathEscape": url.PathEscape, "PathEscapeSegments": util.PathEscapeSegments, @@ -61,6 +60,7 @@ func NewFuncMap() template.FuncMap { // ----------------------------------------------------------------- // time / number / format + "ShortSha": base.ShortSha, "FileSize": base.FileSize, "CountFmt": countFmt, "Sec2Hour": util.SecToHours, @@ -73,6 +73,7 @@ func NewFuncMap() template.FuncMap { "AssetURI": public.AssetURI, "ScriptImport": scriptImport, + // ----------------------------------------------------------------- // setting "AppName": func() string { @@ -84,17 +85,10 @@ func NewFuncMap() template.FuncMap { "AssetUrlPrefix": func() string { return setting.StaticURLPrefix + "/assets" }, - "AppUrl": func() string { - // The usage of AppUrl should be avoided as much as possible, - // because the AppURL(ROOT_URL) may not match user's visiting site and the ROOT_URL in app.ini may be incorrect. - // And it's difficult for Gitea to guess absolute URL correctly with zero configuration, - // because Gitea doesn't know whether the scheme is HTTP or HTTPS unless the reverse proxy could tell Gitea. - return setting.AppURL - }, "AppVer": func() string { return setting.AppVer }, - "AppDomain": func() string { // documented in mail-templates.md + "AppDomain": func() string { // TODO: helm char registry still uses it, need to use current host return setting.Domain }, "ShowFooterTemplateLoadTime": func() bool { @@ -143,7 +137,6 @@ func NewFuncMap() template.FuncMap { // ----------------------------------------------------------------- // misc (TODO: move them to MiscUtils to avoid bloating the main func map) - "ShortSha": base.ShortSha, "ActionContent2Commits": ActionContent2Commits, "IsMultilineCommitMessage": isMultilineCommitMessage, "CommentMustAsDiff": gitdiff.CommentMustAsDiff, diff --git a/modules/templates/mail.go b/modules/templates/mail.go index ca13626468..b2be212b18 100644 --- a/modules/templates/mail.go +++ b/modules/templates/mail.go @@ -6,12 +6,14 @@ package templates import ( "html/template" "io" + "net/url" "regexp" "slices" "strings" "sync" texttmpl "text/template" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -41,6 +43,7 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap { "Eval": evalTokens, "EllipsisString": util.EllipsisDisplayString, + "AppName": func() string { return setting.AppName }, @@ -50,6 +53,48 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap { } } +func mailBodyFuncMap() template.FuncMap { + // Some of them are documented in mail-templates.md + return template.FuncMap{ + "DumpVar": dumpVar, + "NIL": func() any { return nil }, + + // html/template related functions + "dict": dict, + "Iif": iif, + "Eval": evalTokens, + "HTMLFormat": htmlFormat, + "QueryEscape": queryEscape, + "QueryBuild": QueryBuild, + "SanitizeHTML": SanitizeHTML, + + "PathEscape": url.PathEscape, + "PathEscapeSegments": util.PathEscapeSegments, + + "DotEscape": dotEscape, + + // utils + "StringUtils": NewStringUtils, + "SliceUtils": NewSliceUtils, + "JsonUtils": NewJsonUtils, + + // time / number / format + "ShortSha": base.ShortSha, + "FileSize": base.FileSize, + + // setting + "AppName": func() string { + return setting.AppName + }, + "AppUrl": func() string { + return setting.AppURL + }, + "AppDomain": func() string { + return setting.Domain + }, + } +} + var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}\s*$`) func newMailRenderer() (*MailRender, error) { @@ -103,7 +148,7 @@ func newMailRenderer() (*MailRender, error) { return renderer.tmplRenderer.Templates().HasTemplate(name) } - staticFuncMap := NewFuncMap() + staticFuncMap := mailBodyFuncMap() renderer.BodyTemplates.ExecuteTemplate = func(w io.Writer, name string, data any) error { if t, ok := renderer.mockedBodyTemplates[name]; ok { return t.Execute(w, data) @@ -131,7 +176,7 @@ func (r *MailRender) MockTemplate(name, subject, body string) func() { texttmpl.Must(r.SubjectTemplates.New(name).Parse(subject)) oldBody, hasOldBody := r.mockedBodyTemplates[name] - mockFuncMap := NewFuncMap() + mockFuncMap := mailBodyFuncMap() r.mockedBodyTemplates[name] = template.Must(template.New(name).Funcs(mockFuncMap).Parse(body)) return func() { r.SubjectTemplates = oldSubject diff --git a/modules/templates/page.go b/modules/templates/page.go index 8f6c82fc4b..32e52bb68e 100644 --- a/modules/templates/page.go +++ b/modules/templates/page.go @@ -24,13 +24,13 @@ type pageRenderer struct { } func (r *pageRenderer) funcMap(ctx context.Context) template.FuncMap { - pageFuncMap := NewFuncMap() + pageFuncMap := newFuncMapWebPage() pageFuncMap["ctx"] = func() any { return ctx } return pageFuncMap } func (r *pageRenderer) funcMapDummy() template.FuncMap { - dummyFuncMap := NewFuncMap() + dummyFuncMap := newFuncMapWebPage() dummyFuncMap["ctx"] = func() any { return nil } // for template compilation only, no context available return dummyFuncMap } diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index e796064ce3..c7b77e5724 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -3223,10 +3223,8 @@ "admin.config.server_config": "Server Configuration", "admin.config.app_name": "Site Title", "admin.config.app_ver": "Gitea Version", - "admin.config.app_url": "Gitea Base URL", "admin.config.custom_conf": "Configuration File Path", "admin.config.custom_file_root_path": "Custom File Root Path", - "admin.config.domain": "Server Domain", "admin.config.disable_router_log": "Disable Router Log", "admin.config.run_user": "Run As Username", "admin.config.run_mode": "Run Mode", diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index a449796ec1..bf48e554df 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -123,9 +123,7 @@ func Config(ctx *context.Context) { ctx.Data["PageIsAdminConfigSummary"] = true ctx.Data["CustomConf"] = setting.CustomConf - ctx.Data["AppUrl"] = setting.AppURL ctx.Data["AppBuiltWith"] = setting.AppBuiltWith - ctx.Data["Domain"] = setting.Domain ctx.Data["RunUser"] = setting.RunUser ctx.Data["RunMode"] = util.ToTitleCase(setting.RunMode) ctx.Data["GitVersion"] = git.DefaultFeatures().VersionInfo() diff --git a/services/context/context_template.go b/services/context/context_template.go index 52c7461187..8fce301ca7 100644 --- a/services/context/context_template.go +++ b/services/context/context_template.go @@ -5,10 +5,13 @@ package context import ( "context" + "html/template" "net/http" "strconv" + "strings" "time" + "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web/middleware" "code.gitea.io/gitea/services/webtheme" @@ -69,3 +72,9 @@ func (c TemplateContext) CurrentWebBanner() *setting.WebBannerType { } return nil } + +func (c TemplateContext) MakeAppURL(link string) template.URL { + s := httplib.GuessCurrentAppURL(c.parentContext()) + s = strings.TrimSuffix(s, "/") + return template.URL(s + strings.TrimPrefix(link, "/")) +} diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index 6dc6e0d5ea..b68f2c1a7a 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -11,10 +11,6 @@