Skip to content

Commit e71b692

Browse files
authored
Breaking summary for template refactoring (go-gitea#29395)
go-gitea#29395
1 parent 22b4f0c commit e71b692

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

docs/content/administration/mail-templates.en-us.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@ This template produces something along these lines:
259259
The template system contains several functions that can be used to further process and format
260260
the messages. Here's a list of some of them:
261261

262-
| Name | Parameters | Available | Usage |
263-
| ---------------- | ----------- | --------- |-----------------------------------------------------------------------------|
264-
| `AppUrl` | - | Any | Gitea's URL |
265-
| `AppName` | - | Any | Set from `app.ini`, usually "Gitea" |
266-
| `AppDomain` | - | Any | Gitea's host name |
267-
| `EllipsisString` | string, int | Any | Truncates a string to the specified length; adds ellipsis as needed |
268-
| `SanitizeHTML` | string | Body only | Sanitizes text by removing any dangerous HTML tags from it. |
269-
| `SafeHTML` | string | Body only | Takes the input as HTML; can be used for `.ReviewComments.RenderedContent`. |
262+
| Name | Parameters | Available | Usage |
263+
| ---------------- | ----------- | --------- | ------------------------------------------------------------------- |
264+
| `AppUrl` | - | Any | Gitea's URL |
265+
| `AppName` | - | Any | Set from `app.ini`, usually "Gitea" |
266+
| `AppDomain` | - | Any | Gitea's host name |
267+
| `EllipsisString` | string, int | Any | Truncates a string to the specified length; adds ellipsis as needed |
268+
| `SanitizeHTML` | string | Body only | Sanitizes text by removing any dangerous HTML tags from it |
269+
| `SafeHTML` | string | Body only | Takes the input as HTML, can be used for outputing raw HTML content |
270270

271271
These are _functions_, not metadata, so they have to be used:
272272

docs/content/administration/mail-templates.zh-cn.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,14 @@ _主题_ 和 _邮件正文_ 由 [Golang的模板引擎](https://go.dev/pkg/text/
242242

243243
模板系统包含一些函数,可用于进一步处理和格式化消息。以下是其中一些函数的列表:
244244

245-
| 函数名 | 参数 | 可用于 | 用法 |
246-
|------------------| ----------- | ------------ |---------------------------------------------------------|
247-
| `AppUrl` | - | 任何地方 | Gitea 的 URL |
248-
| `AppName` | - | 任何地方 |`app.ini` 中设置,通常为 "Gitea" |
249-
| `AppDomain` | - | 任何地方 | Gitea 的主机名 |
250-
| `EllipsisString` | string, int | 任何地方 | 将字符串截断为指定长度;根据需要添加省略号 |
251-
| `SanitizeHTML` | string | 仅正文部分 | 通过删除其中的危险 HTML 标签对文本进行清理 |
252-
| `SafeHTML` | string | 仅正文部分 | 将输入作为 HTML 处理;可用于 `.ReviewComments.RenderedContent` 等字段 |
245+
| 函数名 | 参数 | 可用于 | 用法 |
246+
|------------------| ----------- | ------------ | ------------------------------ |
247+
| `AppUrl` | - | 任何地方 | Gitea 的 URL |
248+
| `AppName` | - | 任何地方 |`app.ini` 中设置,通常为 "Gitea" |
249+
| `AppDomain` | - | 任何地方 | Gitea 的主机名 |
250+
| `EllipsisString` | string, int | 任何地方 | 将字符串截断为指定长度;根据需要添加省略号 |
251+
| `SanitizeHTML` | string | 仅正文部分 | 通过删除其中的危险 HTML 标签对文本进行清理 |
252+
| `SafeHTML` | string | 仅正文部分 | 将输入作为 HTML 处理;可用于输出原始的 HTML 内容 |
253253

254254
这些都是 _函数_,而不是元数据,因此必须按以下方式使用:
255255

modules/templates/mailer.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package templates
55

66
import (
77
"context"
8+
"fmt"
89
"html/template"
910
"regexp"
1011
"strings"
@@ -33,7 +34,7 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap {
3334
}
3435
}
3536

36-
func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) {
37+
func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) error {
3738
// Split template into subject and body
3839
var subjectContent []byte
3940
bodyContent := content
@@ -42,20 +43,13 @@ func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template,
4243
subjectContent = content[0:loc[0]]
4344
bodyContent = content[loc[1]:]
4445
}
45-
if _, err := stpl.New(name).
46-
Parse(string(subjectContent)); err != nil {
47-
log.Error("Failed to parse template [%s/subject]: %v", name, err)
48-
if !setting.IsProd {
49-
log.Fatal("Please fix the mail template error")
50-
}
46+
if _, err := stpl.New(name).Parse(string(subjectContent)); err != nil {
47+
return fmt.Errorf("failed to parse template [%s/subject]: %w", name, err)
5148
}
52-
if _, err := btpl.New(name).
53-
Parse(string(bodyContent)); err != nil {
54-
log.Error("Failed to parse template [%s/body]: %v", name, err)
55-
if !setting.IsProd {
56-
log.Fatal("Please fix the mail template error")
57-
}
49+
if _, err := btpl.New(name).Parse(string(bodyContent)); err != nil {
50+
return fmt.Errorf("failed to parse template [%s/body]: %w", name, err)
5851
}
52+
return nil
5953
}
6054

6155
// Mailer provides the templates required for sending notification mails.
@@ -87,7 +81,13 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
8781
if firstRun {
8882
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
8983
}
90-
buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content)
84+
if err = buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content); err != nil {
85+
if firstRun {
86+
log.Fatal("Failed to parse mail template, err: %v", err)
87+
} else {
88+
log.Error("Failed to parse mail template, err: %v", err)
89+
}
90+
}
9191
}
9292
}
9393

templates/mail/issue/default.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
{{$.locale.Tr "mail.issue.in_tree_path" .TreePath}}
6666
<div class="review">
6767
<pre>{{.Patch}}</pre>
68-
<div>{{.RenderedContent | SafeHTML}}</div>
68+
<div>{{.RenderedContent}}</div>
6969
</div>
7070
{{end -}}
7171
{{if eq .ActionName "push"}}

0 commit comments

Comments
 (0)