Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (
"code.gitea.io/gitea/services/gitdiff"
)

// NewFuncMap returns functions for injecting to templates
func NewFuncMap() template.FuncMap {
func newFuncMapWebPage() template.FuncMap {
return map[string]any{
"DumpVar": dumpVar,
"NIL": func() any { return nil },
Expand All @@ -40,7 +39,6 @@ func NewFuncMap() template.FuncMap {
"QueryEscape": queryEscape,
"QueryBuild": QueryBuild,
"SanitizeHTML": SanitizeHTML,
"DotEscape": dotEscape,

"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
Expand All @@ -61,6 +59,7 @@ func NewFuncMap() template.FuncMap {

// -----------------------------------------------------------------
// time / number / format
"ShortSha": base.ShortSha,
"FileSize": base.FileSize,
"CountFmt": countFmt,
"Sec2Hour": util.SecToHours,
Expand All @@ -73,6 +72,7 @@ func NewFuncMap() template.FuncMap {

"AssetURI": public.AssetURI,
"ScriptImport": scriptImport,

// -----------------------------------------------------------------
// setting
"AppName": func() string {
Expand All @@ -84,17 +84,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 registry still uses it, need to use current request host in the future
return setting.Domain
},
"ShowFooterTemplateLoadTime": func() bool {
Expand Down Expand Up @@ -143,7 +136,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,
Expand Down Expand Up @@ -177,11 +169,6 @@ func queryEscape(s string) template.URL {
return template.URL(url.QueryEscape(s))
}

// dotEscape wraps a dots in names with ZWJ [U+200D] in order to prevent auto-linkers from detecting these as urls
func dotEscape(raw string) string {
return strings.ReplaceAll(raw, ".", "\u200d.\u200d")
}

// iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
// and it could be simply used as "{{iif expr trueVal}}" (omit the falseVal).
func iif(condition any, vals ...any) any {
Expand Down
54 changes: 52 additions & 2 deletions modules/templates/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,13 +36,19 @@ type MailRender struct {
mockedBodyTemplates map[string]*template.Template
}

// dotEscape wraps a dots in names with ZWJ [U+200D] in order to prevent auto-linkers from detecting these as urls
func dotEscape(raw string) string {
return strings.ReplaceAll(raw, ".", "\u200d.\u200d")
}

// mailSubjectTextFuncMap returns functions for injecting to text templates, it's only used for mail subject
func mailSubjectTextFuncMap() texttmpl.FuncMap {
return texttmpl.FuncMap{
"dict": dict,
"Eval": evalTokens,

"EllipsisString": util.EllipsisDisplayString,

"AppName": func() string {
return setting.AppName
},
Expand All @@ -50,6 +58,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) {
Expand Down Expand Up @@ -103,7 +153,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)
Expand Down Expand Up @@ -131,7 +181,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
Expand Down
4 changes: 2 additions & 2 deletions modules/templates/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3224,10 +3224,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",
Expand Down
2 changes: 0 additions & 2 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions services/context/context_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -69,3 +72,14 @@ func (c TemplateContext) CurrentWebBanner() *setting.WebBannerType {
}
return nil
}

// AppFullLink returns a full URL link with AppSubURL for the given app link (no AppSubURL)
// If no link is given, it returns the current app full URL with sub-path but without trailing slash (that's why it is not named as AppURL)
func (c TemplateContext) AppFullLink(link ...string) template.URL {
Comment thread
silverwind marked this conversation as resolved.
s := httplib.GuessCurrentAppURL(c.parentContext())
s = strings.TrimSuffix(s, "/")
if len(link) == 0 {
return template.URL(s)
}
return template.URL(s + strings.TrimPrefix(link[0], "/"))
}
4 changes: 0 additions & 4 deletions templates/admin/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<dd>{{AppVer}}{{.AppBuiltWith}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.custom_conf"}}</dt>
<dd>{{.CustomConf}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.app_url"}}</dt>
<dd>{{.AppUrl}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.domain"}}</dt>
<dd>{{.Domain}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.disable_router_log"}}</dt>
<dd>{{svg (Iif .DisableRouterLog "octicon-check" "octicon-x")}}</dd>

Expand Down
4 changes: 2 additions & 2 deletions templates/base/head_opengraph.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{{end}}
{{else if or .PageIsDiff .IsViewFile}}
<meta property="og:title" content="{{.Title}}">
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
<meta property="og:url" content="{{ctx.AppFullLink $.Link}}">
{{if and .PageIsDiff .Commit}}
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}}
Expand All @@ -41,7 +41,7 @@
<meta property="og:title" content="{{AppName}}">
<meta property="og:type" content="website">
<meta property="og:image" content="{{AssetUrlPrefix}}/img/logo.png">
<meta property="og:url" content="{{AppUrl}}">
<meta property="og:url" content="{{ctx.AppFullLink}}">
<meta property="og:description" content="{{MetaDescription}}">
{{end}}
<meta property="og:site_name" content="{{AppName}}">
2 changes: 1 addition & 1 deletion templates/base/head_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
window.addEventListener('error', function(e) {window._globalHandlerErrors=window._globalHandlerErrors||[]; window._globalHandlerErrors.push(e);});
window.addEventListener('unhandledrejection', function(e) {window._globalHandlerErrors=window._globalHandlerErrors||[]; window._globalHandlerErrors.push(e);});
window.config = {
appUrl: '{{AppUrl}}',
appUrl: '{{ctx.AppFullLink "/"}}',
appSubUrl: '{{AppSubUrl}}',
assetUrlPrefix: '{{AssetUrlPrefix}}',
runModeIsProd: {{.RunModeIsProd}},
Expand Down
6 changes: 0 additions & 6 deletions templates/devtest/gitea-ui.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@
</div>
</div>

<div>
<h1>&lt;origin-url&gt;</h1>
<div><origin-url data-url="test/url"></origin-url></div>
<div><origin-url data-url="/test/url"></origin-url></div>
</div>

<div>
<h1>&lt;overflow-menu&gt;</h1>
<overflow-menu class="ui secondary pointing tabular borderless menu">
Expand Down
4 changes: 2 additions & 2 deletions templates/package/content/alpine.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.alpine.registry"}}</label>
<div class="markup"><pre class="code-block"><code><origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/alpine"></origin-url>/$branch/$repository</code></pre></div>
<div class="markup"><pre class="code-block"><code>{{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/alpine/$branch/$repository</code></pre></div>
<p>{{ctx.Locale.Tr "packages.alpine.registry.info"}}</p>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.alpine.registry.key"}}</label>
<div class="markup"><pre class="code-block"><code>curl -JO <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/alpine/key"></origin-url></code></pre></div>
<div class="markup"><pre class="code-block"><code>curl -JO {{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/alpine/key</code></pre></div>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.alpine.install"}}</label>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/arch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="markup"><pre class="code-block"><code>{{range $i, $repo := .Repositories}}{{if $i}}
{{end}}[{{$repo}}]
SigLevel = Optional TrustAll
Server = <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/arch/$repo/$arch"></origin-url>
Server = {{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/arch/$repo/$arch
{{end}}</code></pre></div>
</div>
<div class="field">
Expand Down
4 changes: 2 additions & 2 deletions templates/package/content/cargo.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
default = "gitea"

[registries.gitea]
index = "sparse+<origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/cargo/"></origin-url>" # Sparse index
# index = "<origin-url data-url="{{AppSubUrl}}/{{.PackageDescriptor.Owner.Name}}/_cargo-index.git"></origin-url>" # Git
index = "sparse+{{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/cargo/" # Sparse index
# index = "{{ctx.AppFullLink}}/{{.PackageDescriptor.Owner.Name}}/_cargo-index.git" # Git

[net]
git-fetch-with-cli = true</code></pre></div>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/chef.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.chef.registry"}}</label>
<div class="markup"><pre class="code-block"><code>knife[:supermarket_site] = '<origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/chef"></origin-url>'</code></pre></div>
<div class="markup"><pre class="code-block"><code>knife[:supermarket_site] = '{{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/chef'</code></pre></div>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.chef.install"}}</label>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/composer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="markup"><pre class="code-block"><code>{
"repositories": [{
"type": "composer",
"url": "<origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/composer"></origin-url>"
"url": "{{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/composer"
}
]
}</code></pre></div>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/conan.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.conan.registry"}}</label>
<div class="markup"><pre class="code-block"><code>conan remote add gitea <origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conan"></origin-url></code></pre></div>
<div class="markup"><pre class="code-block"><code>conan remote add gitea {{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conan</code></pre></div>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.conan.install"}}</label>
Expand Down
6 changes: 3 additions & 3 deletions templates/package/content/conda.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.conda.registry"}}</label>
<div class="markup"><pre class="code-block"><code>channel_alias: <origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda"></origin-url>
<div class="markup"><pre class="code-block"><code>channel_alias: {{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda
channels:
&#32;&#32;- <origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda"></origin-url>
&#32;&#32;- {{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda
default_channels:
&#32;&#32;- <origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda"></origin-url></code></pre></div>
&#32;&#32;- {{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/conda</code></pre></div>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.conda.install"}}</label>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/cran.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.cran.registry"}}</label>
<div class="markup"><pre class="code-block"><code>options("repos" = c(getOption("repos"), c(gitea="<origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/cran"></origin-url>")))</code></pre></div>
<div class="markup"><pre class="code-block"><code>options("repos" = c(getOption("repos"), c(gitea="{{ctx.AppFullLink}}/api/packages/{{.PackageDescriptor.Owner.Name}}/cran")))</code></pre></div>
</div>
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.cran.install"}}</label>
Expand Down
4 changes: 2 additions & 2 deletions templates/package/content/debian.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<div class="ui form">
<div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.debian.registry"}}</label>
<div class="markup"><pre class="code-block"><code>sudo curl <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/debian/repository.key"></origin-url> -o /etc/apt/keyrings/gitea-{{$.PackageDescriptor.Owner.Name}}.asc
echo "deb [signed-by=/etc/apt/keyrings/gitea-{{$.PackageDescriptor.Owner.Name}}.asc] <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/debian"></origin-url> $distribution $component" | sudo tee -a /etc/apt/sources.list.d/gitea.list
<div class="markup"><pre class="code-block"><code>sudo curl {{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/debian/repository.key -o /etc/apt/keyrings/gitea-{{$.PackageDescriptor.Owner.Name}}.asc
echo "deb [signed-by=/etc/apt/keyrings/gitea-{{$.PackageDescriptor.Owner.Name}}.asc] {{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/debian $distribution $component" | sudo tee -a /etc/apt/sources.list.d/gitea.list
sudo apt update</code></pre></div>
<p>{{ctx.Locale.Tr "packages.debian.registry.info"}}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/package/content/generic.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.generic.download"}}</label>
<div class="markup"><pre class="code-block"><code>
{{- range .PackageDescriptor.Files -}}
curl -OJ <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/generic/{{$.PackageDescriptor.Package.Name}}/{{$.PackageDescriptor.Version.Version}}/{{.File.Name}}"></origin-url>
curl -OJ {{ctx.AppFullLink}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/generic/{{$.PackageDescriptor.Package.Name}}/{{$.PackageDescriptor.Version.Version}}/{{.File.Name}}
{{end -}}
</code></pre></div>
</div>
Expand Down
Loading