@@ -24,13 +24,21 @@ import (
2424 "code.gitea.io/gitea/modules/util"
2525)
2626
27+ type RenderUtils struct {
28+ ctx context.Context
29+ }
30+
31+ func NewRenderUtils (ctx context.Context ) * RenderUtils {
32+ return & RenderUtils {ctx : ctx }
33+ }
34+
2735// 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 {
2937 cleanMsg := template .HTMLEscapeString (msg )
3038 // we can safely assume that it will not return any error, since there
3139 // shouldn't be any special HTML.
3240 fullMessage , err := markup .RenderCommitMessage (& markup.RenderContext {
33- Ctx : ctx ,
41+ Ctx : ut . ctx ,
3442 Metas : metas ,
3543 }, cleanMsg )
3644 if err != nil {
@@ -44,9 +52,9 @@ func RenderCommitMessage(ctx context.Context, msg string, metas map[string]strin
4452 return renderCodeBlock (template .HTML (msgLines [0 ]))
4553}
4654
47- // renderCommitMessageLinkSubject renders commit message as a XSS-safe link to
55+ // RenderCommitMessageLinkSubject renders commit message as a XSS-safe link to
4856// 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 {
5058 msgLine := strings .TrimLeftFunc (msg , unicode .IsSpace )
5159 lineEnd := strings .IndexByte (msgLine , '\n' )
5260 if lineEnd > 0 {
@@ -60,7 +68,7 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
6068 // we can safely assume that it will not return any error, since there
6169 // shouldn't be any special HTML.
6270 renderedMessage , err := markup .RenderCommitMessageSubject (& markup.RenderContext {
63- Ctx : ctx ,
71+ Ctx : ut . ctx ,
6472 DefaultLink : urlDefault ,
6573 Metas : metas ,
6674 }, template .HTMLEscapeString (msgLine ))
@@ -71,8 +79,8 @@ func renderCommitMessageLinkSubject(ctx context.Context, msg, urlDefault string,
7179 return renderCodeBlock (template .HTML (renderedMessage ))
7280}
7381
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 {
7684 msgLine := strings .TrimSpace (msg )
7785 lineEnd := strings .IndexByte (msgLine , '\n' )
7886 if lineEnd > 0 {
@@ -86,7 +94,7 @@ func renderCommitBody(ctx context.Context, msg string, metas map[string]string)
8694 }
8795
8896 renderedMessage , err := markup .RenderCommitMessage (& markup.RenderContext {
89- Ctx : ctx ,
97+ Ctx : ut . ctx ,
9098 Metas : metas ,
9199 }, template .HTMLEscapeString (msgLine ))
92100 if err != nil {
@@ -105,22 +113,22 @@ func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
105113 return template .HTML (htmlWithCodeTags )
106114}
107115
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 {
110118 renderedText , err := markup .RenderIssueTitle (& markup.RenderContext {
111- Ctx : ctx ,
119+ Ctx : ut . ctx ,
112120 Metas : metas ,
113121 }, template .HTMLEscapeString (text ))
114122 if err != nil {
115123 log .Error ("RenderIssueTitle: %v" , err )
116- return template . HTML ( "" )
124+ return ""
117125 }
118126 return template .HTML (renderedText )
119127}
120128
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 )
124132 var extraCSSClasses string
125133 textColor := util .ContrastColor (label .Color )
126134 labelScope := label .ExclusiveScope ()
@@ -134,12 +142,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
134142 if labelScope == "" {
135143 // Regular label
136144 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 ))
138146 }
139147
140148 // 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 :])
143151
144152 // Make scope and item background colors slightly darker and lighter respectively.
145153 // More contrast needed with higher luminance, empirically tweaked.
@@ -176,13 +184,12 @@ func renderLabel(ctx context.Context, locale translation.Locale, label *issues_m
176184 textColor , itemColor , itemHTML )
177185}
178186
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 ))
183190 if err != nil {
184191 log .Error ("RenderEmoji: %v" , err )
185- return template . HTML ( "" )
192+ return ""
186193 }
187194 return template .HTML (renderedText )
188195}
@@ -200,9 +207,9 @@ func reactionToEmoji(reaction string) template.HTML {
200207 return template .HTML (fmt .Sprintf (`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>` , reaction , setting .StaticURLPrefix , url .PathEscape (reaction )))
201208}
202209
203- func RenderMarkdownToHtml ( ctx context. Context , input string ) template.HTML { //nolint:revive
210+ func ( ut * RenderUtils ) MarkdownToHtml ( input string ) template.HTML { //nolint:revive
204211 output , err := markdown .RenderString (& markup.RenderContext {
205- Ctx : ctx ,
212+ Ctx : ut . ctx ,
206213 Metas : map [string ]string {"mode" : "document" },
207214 }, input )
208215 if err != nil {
@@ -211,7 +218,7 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
211218 return output
212219}
213220
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 {
215222 isPullRequest := issue != nil && issue .IsPull
216223 baseLink := fmt .Sprintf ("%s/%s" , repoLink , util .Iif (isPullRequest , "pulls" , "issues" ))
217224 htmlCode := `<span class="labels-list">`
@@ -220,7 +227,7 @@ func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issu
220227 if label == nil {
221228 continue
222229 }
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 ))
224231 }
225232 htmlCode += "</span>"
226233 return template .HTML (htmlCode )
0 commit comments