1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-06 10:37:20 +00:00

better rendering code

This commit is contained in:
wxiaoguang
2024-04-30 10:06:42 +08:00
parent b3dd10f7b9
commit bf338dbb6f

View File

@ -121,29 +121,25 @@ func RenderIssueTitle(ctx context.Context, text string, metas map[string]string)
// RenderLabel renders a label // RenderLabel renders a label
// locale is needed due to an import cycle with our context providing the `Tr` function // locale is needed due to an import cycle with our context providing the `Tr` function
func RenderLabel(ctx context.Context, locale translation.Locale, label *issues_model.Label) template.HTML { func RenderLabel(ctx context.Context, locale translation.Locale, label *issues_model.Label) template.HTML {
var ( var extraCSSClasses string
archivedCSSClass string textColor := util.ContrastColor(label.Color)
textColor = util.ContrastColor(label.Color) labelScope := label.ExclusiveScope()
labelScope = label.ExclusiveScope() descriptionText := emoji.ReplaceAliases(label.Description)
)
description := emoji.ReplaceAliases(template.HTMLEscapeString(label.Description))
if label.IsArchived() { if label.IsArchived() {
archivedCSSClass = "archived-label" extraCSSClasses = "archived-label"
description = fmt.Sprintf("(%s) %s", locale.TrString("archived"), description) descriptionText = fmt.Sprintf("(%s) %s", locale.TrString("archived"), descriptionText)
} }
if labelScope == "" { if labelScope == "" {
// Regular label // Regular label
s := fmt.Sprintf("<div class='ui label %s' style='color: %s !important; background-color: %s !important;' data-tooltip-content title='%s'>%s</div>", return HTMLFormat(`<div class="ui label %s" style="color: %s !important; background-color: %s !important;" data-tooltip-content title="%s">%s</div>`,
archivedCSSClass, textColor, label.Color, description, RenderEmoji(ctx, label.Name)) extraCSSClasses, textColor, label.Color, descriptionText, RenderEmoji(ctx, label.Name))
return template.HTML(s)
} }
// Scoped label // Scoped label
scopeText := RenderEmoji(ctx, labelScope) scopeHTML := RenderEmoji(ctx, labelScope)
itemText := RenderEmoji(ctx, label.Name[len(labelScope)+1:]) itemHTML := RenderEmoji(ctx, label.Name[len(labelScope)+1:])
// Make scope and item background colors slightly darker and lighter respectively. // Make scope and item background colors slightly darker and lighter respectively.
// More contrast needed with higher luminance, empirically tweaked. // More contrast needed with higher luminance, empirically tweaked.
@ -171,14 +167,13 @@ func RenderLabel(ctx context.Context, locale translation.Locale, label *issues_m
itemColor := "#" + hex.EncodeToString(itemBytes) itemColor := "#" + hex.EncodeToString(itemBytes)
scopeColor := "#" + hex.EncodeToString(scopeBytes) scopeColor := "#" + hex.EncodeToString(scopeBytes)
s := fmt.Sprintf("<span class='ui label %s scope-parent' data-tooltip-content title='%s'>"+ return HTMLFormat(`<span class="ui label %s scope-parent" data-tooltip-content title="%s">`+
"<div class='ui label scope-left' style='color: %s !important; background-color: %s !important'>%s</div>"+ `<div class="ui label scope-left" style="color: %s !important; background-color: %s !important">%s</div>`+
"<div class='ui label scope-right' style='color: %s !important; background-color: %s !important'>%s</div>"+ `<div class="ui label scope-right" style="color: %s !important; background-color: %s !important">%s</div>`+
"</span>", `</span>`,
archivedCSSClass, description, extraCSSClasses, descriptionText,
textColor, scopeColor, scopeText, textColor, scopeColor, scopeHTML,
textColor, itemColor, itemText) textColor, itemColor, itemHTML)
return template.HTML(s)
} }
// RenderEmoji renders html text with emoji post processors // RenderEmoji renders html text with emoji post processors