2020-07-12 05:10:56 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-07-12 05:10:56 -04:00
|
|
|
|
|
|
|
package svg
|
|
|
|
|
2022-11-08 10:13:58 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2023-04-12 06:16:45 -04:00
|
|
|
"path"
|
2022-11-08 10:13:58 -05:00
|
|
|
"strings"
|
|
|
|
|
2024-11-18 00:25:42 -05:00
|
|
|
gitea_html "code.gitea.io/gitea/modules/htmlutil"
|
2023-04-12 06:16:45 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/public"
|
2022-11-08 10:13:58 -05:00
|
|
|
)
|
|
|
|
|
2023-08-05 00:34:59 -04:00
|
|
|
var svgIcons map[string]string
|
2022-11-08 10:13:58 -05:00
|
|
|
|
|
|
|
const defaultSize = 16
|
2020-07-12 05:10:56 -04:00
|
|
|
|
2023-08-05 00:34:59 -04:00
|
|
|
// Init discovers SVG icons and populates the `svgIcons` variable
|
2023-04-12 06:16:45 -04:00
|
|
|
func Init() error {
|
2023-08-05 00:34:59 -04:00
|
|
|
const svgAssetsPath = "assets/img/svg"
|
|
|
|
files, err := public.AssetFS().ListFiles(svgAssetsPath)
|
2023-04-12 06:16:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-21 01:39:27 -04:00
|
|
|
|
2023-08-05 00:34:59 -04:00
|
|
|
svgIcons = make(map[string]string, len(files))
|
2023-04-12 06:16:45 -04:00
|
|
|
for _, file := range files {
|
|
|
|
if path.Ext(file) != ".svg" {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-05 00:34:59 -04:00
|
|
|
bs, err := public.AssetFS().ReadFile(svgAssetsPath, file)
|
2023-04-12 06:16:45 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read SVG file %s: %v", file, err)
|
|
|
|
} else {
|
2023-08-05 00:34:59 -04:00
|
|
|
svgIcons[file[:len(file)-4]] = string(Normalize(bs, defaultSize))
|
2023-04-12 06:16:45 -04:00
|
|
|
}
|
2023-03-21 01:39:27 -04:00
|
|
|
}
|
2023-04-12 06:16:45 -04:00
|
|
|
return nil
|
2020-07-12 05:10:56 -04:00
|
|
|
}
|
2022-11-08 10:13:58 -05:00
|
|
|
|
2024-03-22 08:16:23 -04:00
|
|
|
func MockIcon(icon string) func() {
|
|
|
|
if svgIcons == nil {
|
|
|
|
svgIcons = make(map[string]string)
|
|
|
|
}
|
|
|
|
orig, exist := svgIcons[icon]
|
|
|
|
svgIcons[icon] = fmt.Sprintf(`<svg class="svg %s" width="%d" height="%d"></svg>`, icon, defaultSize, defaultSize)
|
|
|
|
return func() {
|
|
|
|
if exist {
|
|
|
|
svgIcons[icon] = orig
|
|
|
|
} else {
|
|
|
|
delete(svgIcons, icon)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 06:16:45 -04:00
|
|
|
// RenderHTML renders icons - arguments icon name (string), size (int), class (string)
|
2023-07-04 14:36:08 -04:00
|
|
|
func RenderHTML(icon string, others ...any) template.HTML {
|
2023-08-05 00:34:59 -04:00
|
|
|
size, class := gitea_html.ParseSizeAndClass(defaultSize, "", others...)
|
|
|
|
if svgStr, ok := svgIcons[icon]; ok {
|
|
|
|
// the code is somewhat hacky, but it just works, because the SVG contents are all normalized
|
2022-11-08 10:13:58 -05:00
|
|
|
if size != defaultSize {
|
2023-08-05 00:34:59 -04:00
|
|
|
svgStr = strings.Replace(svgStr, fmt.Sprintf(`width="%d"`, defaultSize), fmt.Sprintf(`width="%d"`, size), 1)
|
|
|
|
svgStr = strings.Replace(svgStr, fmt.Sprintf(`height="%d"`, defaultSize), fmt.Sprintf(`height="%d"`, size), 1)
|
2022-11-08 10:13:58 -05:00
|
|
|
}
|
|
|
|
if class != "" {
|
|
|
|
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
|
|
|
|
}
|
|
|
|
return template.HTML(svgStr)
|
|
|
|
}
|
2024-03-22 08:16:23 -04:00
|
|
|
// during test (or something wrong happens), there is no SVG loaded, so use a dummy span to tell that the icon is missing
|
|
|
|
return template.HTML(fmt.Sprintf("<span>%s(%d/%s)</span>", template.HTMLEscapeString(icon), size, template.HTMLEscapeString(class)))
|
2022-11-08 10:13:58 -05:00
|
|
|
}
|