@@ -24,13 +24,21 @@ import (
24
24
"code.gitea.io/gitea/modules/util"
25
25
)
26
26
27
+ type RenderUtils struct {
28
+ ctx context.Context
29
+ }
30
+
31
+ func NewRenderUtils (ctx context.Context ) * RenderUtils {
32
+ return & RenderUtils {ctx : ctx }
33
+ }
34
+
27
35
// RenderCommitMessage renders commit message with XSS-safe and special links.
28
- func RenderCommitMessage ( ctx context. Context , msg string , metas map [string ]string ) template.HTML {
36
+ func ( ut * RenderUtils ) RenderCommitMessage ( msg string , metas map [string ]string ) template.HTML {
29
37
cleanMsg := template .HTMLEscapeString (msg )
30
38
// we can safely assume that it will not return any error, since there
31
39
// shouldn't be any special HTML.
32
40
fullMessage , err := markup .RenderCommitMessage (& markup.RenderContext {
33
- Ctx : ctx ,
41
+ Ctx : ut . ctx ,
34
42
Metas : metas ,
35
43
}, cleanMsg )
36
44
if err != nil {
@@ -44,9 +52,9 @@ func RenderCommitMessage(ctx context.Context, msg string, metas map[string]strin
44
52
return renderCodeBlock (template .HTML (msgLines [0 ]))
45
53
}
46
54
47
- // renderCommitMessageLinkSubject renders commit message as a XSS-safe link to
55
+ // RenderCommitMessageLinkSubject renders commit message as a XSS-safe link to
48
56
// the provided default url, handling for special links without email to links.
49
- func renderCommitMessageLinkSubject ( ctx context. Context , msg , urlDefault string , metas map [string ]string ) template.HTML {
57
+ func ( ut * RenderUtils ) RenderCommitMessageLinkSubject ( msg , urlDefault string , metas map [string ]string ) template.HTML {
50
58
msgLine := strings .TrimLeftFunc (msg , unicode .IsSpace )
51
59
lineEnd := strings .IndexByte (msgLine , '\n' )
52
60
if lineEnd > 0 {
@@ -60,7 +68,7 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
60
68
// we can safely assume that it will not return any error, since there
61
69
// shouldn't be any special HTML.
62
70
renderedMessage , err := markup .RenderCommitMessageSubject (& markup.RenderContext {
63
- Ctx : ctx ,
71
+ Ctx : ut . ctx ,
64
72
DefaultLink : urlDefault ,
65
73
Metas : metas ,
66
74
}, template .HTMLEscapeString (msgLine ))
@@ -71,8 +79,8 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
71
79
return renderCodeBlock (template .HTML (renderedMessage ))
72
80
}
73
81
74
- // renderCommitBody extracts the body of a commit message without its title.
75
- func renderCommitBody ( ctx context. Context , msg string , metas map [string ]string ) template.HTML {
82
+ // RenderCommitBody extracts the body of a commit message without its title.
83
+ func ( ut * RenderUtils ) RenderCommitBody ( msg string , metas map [string ]string ) template.HTML {
76
84
msgLine := strings .TrimSpace (msg )
77
85
lineEnd := strings .IndexByte (msgLine , '\n' )
78
86
if lineEnd > 0 {
@@ -86,7 +94,7 @@ func renderCommitBody(ctx context.Context, msg string, metas map[string]string)
86
94
}
87
95
88
96
renderedMessage , err := markup .RenderCommitMessage (& markup.RenderContext {
89
- Ctx : ctx ,
97
+ Ctx : ut . ctx ,
90
98
Metas : metas ,
91
99
}, template .HTMLEscapeString (msgLine ))
92
100
if err != nil {
@@ -105,22 +113,22 @@ func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
105
113
return template .HTML (htmlWithCodeTags )
106
114
}
107
115
108
- // renderIssueTitle renders issue/pull title with defined post processors
109
- func renderIssueTitle ( ctx context. Context , text string , metas map [string ]string ) template.HTML {
116
+ // RenderIssueTitle renders issue/pull title with defined post processors
117
+ func ( ut * RenderUtils ) RenderIssueTitle ( text string , metas map [string ]string ) template.HTML {
110
118
renderedText , err := markup .RenderIssueTitle (& markup.RenderContext {
111
- Ctx : ctx ,
119
+ Ctx : ut . ctx ,
112
120
Metas : metas ,
113
121
}, template .HTMLEscapeString (text ))
114
122
if err != nil {
115
123
log .Error ("RenderIssueTitle: %v" , err )
116
- return template . HTML ( "" )
124
+ return ""
117
125
}
118
126
return template .HTML (renderedText )
119
127
}
120
128
121
- // renderLabel renders a label
122
- // locale is needed due to an import cycle with our context providing the `Tr` function
123
- func renderLabel ( ctx context. Context , locale translation.Locale , label * issues_model. Label ) template. HTML {
129
+ // RenderLabel renders a label
130
+ func ( ut * RenderUtils ) RenderLabel ( label * issues_model. Label ) template. HTML {
131
+ locale := ut . ctx . Value ( translation .ContextKey ).(translation. Locale )
124
132
var extraCSSClasses string
125
133
textColor := util .ContrastColor (label .Color )
126
134
labelScope := label .ExclusiveScope ()
@@ -134,12 +142,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
134
142
if labelScope == "" {
135
143
// Regular label
136
144
return HTMLFormat (`<div class="ui label %s" style="color: %s !important; background-color: %s !important;" data-tooltip-content title="%s">%s</div>` ,
137
- extraCSSClasses , textColor , label .Color , descriptionText , renderEmoji ( ctx , label .Name ))
145
+ extraCSSClasses , textColor , label .Color , descriptionText , ut . RenderEmoji ( label .Name ))
138
146
}
139
147
140
148
// Scoped label
141
- scopeHTML := renderEmoji ( ctx , labelScope )
142
- itemHTML := renderEmoji ( ctx , label .Name [len (labelScope )+ 1 :])
149
+ scopeHTML := ut . RenderEmoji ( labelScope )
150
+ itemHTML := ut . RenderEmoji ( label .Name [len (labelScope )+ 1 :])
143
151
144
152
// Make scope and item background colors slightly darker and lighter respectively.
145
153
// More contrast needed with higher luminance, empirically tweaked.
@@ -176,13 +184,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
176
184
textColor , itemColor , itemHTML )
177
185
}
178
186
179
- // renderEmoji renders html text with emoji post processors
180
- func renderEmoji (ctx context.Context , text string ) template.HTML {
181
- renderedText , err := markup .RenderEmoji (& markup.RenderContext {Ctx : ctx },
182
- template .HTMLEscapeString (text ))
187
+ // RenderEmoji renders html text with emoji post processors
188
+ func (ut * RenderUtils ) RenderEmoji (text string ) template.HTML {
189
+ renderedText , err := markup .RenderEmoji (& markup.RenderContext {Ctx : ut .ctx }, template .HTMLEscapeString (text ))
183
190
if err != nil {
184
191
log .Error ("RenderEmoji: %v" , err )
185
- return template . HTML ( "" )
192
+ return ""
186
193
}
187
194
return template .HTML (renderedText )
188
195
}
@@ -200,9 +207,9 @@ func reactionToEmoji(reaction string) template.HTML {
200
207
return template .HTML (fmt .Sprintf (`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>` , reaction , setting .StaticURLPrefix , url .PathEscape (reaction )))
201
208
}
202
209
203
- func RenderMarkdownToHtml ( ctx context. Context , input string ) template.HTML { //nolint:revive
210
+ func ( ut * RenderUtils ) MarkdownToHtml ( input string ) template.HTML { //nolint:revive
204
211
output , err := markdown .RenderString (& markup.RenderContext {
205
- Ctx : ctx ,
212
+ Ctx : ut . ctx ,
206
213
Metas : map [string ]string {"mode" : "document" },
207
214
}, input )
208
215
if err != nil {
@@ -211,7 +218,7 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
211
218
return output
212
219
}
213
220
214
- func RenderLabels ( ctx context. Context , locale translation. Locale , labels []* issues_model.Label , repoLink string , issue * issues_model.Issue ) template.HTML {
221
+ func ( ut * RenderUtils ) RenderLabels ( labels []* issues_model.Label , repoLink string , issue * issues_model.Issue ) template.HTML {
215
222
isPullRequest := issue != nil && issue .IsPull
216
223
baseLink := fmt .Sprintf ("%s/%s" , repoLink , util .Iif (isPullRequest , "pulls" , "issues" ))
217
224
htmlCode := `<span class="labels-list">`
@@ -220,7 +227,7 @@ func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issu
220
227
if label == nil {
221
228
continue
222
229
}
223
- htmlCode += fmt .Sprintf (`<a href="%s?labels=%d">%s</a>` , baseLink , label .ID , renderLabel ( ctx , locale , label ))
230
+ htmlCode += fmt .Sprintf (`<a href="%s?labels=%d">%s</a>` , baseLink , label .ID , ut . RenderLabel ( label ))
224
231
}
225
232
htmlCode += "</span>"
226
233
return template .HTML (htmlCode )
0 commit comments