summaryrefslogtreecommitdiffstats
path: root/modules/svg/svg.go
blob: 8132978caca996ae0d58788d55e35e146d141249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package svg

import (
	"fmt"
	"html/template"
	"path"
	"strings"

	gitea_html "code.gitea.io/gitea/modules/html"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/public"
)

var svgIcons map[string]string

const defaultSize = 16

// Init discovers SVG icons and populates the `svgIcons` variable
func Init() error {
	const svgAssetsPath = "assets/img/svg"
	files, err := public.AssetFS().ListFiles(svgAssetsPath)
	if err != nil {
		return err
	}

	svgIcons = make(map[string]string, len(files))
	for _, file := range files {
		if path.Ext(file) != ".svg" {
			continue
		}
		bs, err := public.AssetFS().ReadFile(svgAssetsPath, file)
		if err != nil {
			log.Error("Failed to read SVG file %s: %v", file, err)
		} else {
			svgIcons[file[:len(file)-4]] = string(Normalize(bs, defaultSize))
		}
	}
	return nil
}

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)
		}
	}
}

// RenderHTML renders icons - arguments icon name (string), size (int), class (string)
func RenderHTML(icon string, others ...any) template.HTML {
	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
		if size != defaultSize {
			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)
		}
		if class != "" {
			svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
		}
		return template.HTML(svgStr)
	}
	// 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)))
}