Skip to content

Commit 2ebab42

Browse files
lunnysilverwind
andauthored
Move svg html render to modules/svg (#21716)
Also added more checks for the render function. Co-authored-by: silverwind <[email protected]>
1 parent 91c7a3e commit 2ebab42

File tree

3 files changed

+77
-49
lines changed

3 files changed

+77
-49
lines changed

modules/html/html.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package html
6+
7+
// ParseSizeAndClass get size and class from string with default values
8+
// If present, "others" expects the new size first and then the classes to use
9+
func ParseSizeAndClass(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
10+
if len(others) == 0 {
11+
return defaultSize, defaultClass
12+
}
13+
14+
size := defaultSize
15+
_size, ok := others[0].(int)
16+
if ok && _size != 0 {
17+
size = _size
18+
}
19+
20+
if len(others) == 1 {
21+
return size, defaultClass
22+
}
23+
24+
class := defaultClass
25+
if _class, ok := others[1].(string); ok && _class != "" {
26+
if defaultClass == "" {
27+
class = _class
28+
} else {
29+
class = defaultClass + " " + _class
30+
}
31+
}
32+
33+
return size, class
34+
}

modules/svg/svg.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,43 @@
44

55
package svg
66

7-
// SVGs contains discovered SVGs
8-
var SVGs map[string]string
7+
import (
8+
"fmt"
9+
"html/template"
10+
"regexp"
11+
"strings"
12+
13+
"code.gitea.io/gitea/modules/html"
14+
)
15+
16+
var (
17+
// SVGs contains discovered SVGs
18+
SVGs map[string]string
19+
20+
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
21+
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
22+
)
23+
24+
const defaultSize = 16
925

1026
// Init discovers SVGs and populates the `SVGs` variable
1127
func Init() {
1228
SVGs = Discover()
1329
}
30+
31+
// Render render icons - arguments icon name (string), size (int), class (string)
32+
func RenderHTML(icon string, others ...interface{}) template.HTML {
33+
size, class := html.ParseSizeAndClass(defaultSize, "", others...)
34+
35+
if svgStr, ok := SVGs[icon]; ok {
36+
if size != defaultSize {
37+
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
38+
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
39+
}
40+
if class != "" {
41+
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
42+
}
43+
return template.HTML(svgStr)
44+
}
45+
return template.HTML("")
46+
}

modules/templates/helper.go

+8-47
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"code.gitea.io/gitea/modules/emoji"
3636
"code.gitea.io/gitea/modules/git"
3737
giturl "code.gitea.io/gitea/modules/git/url"
38+
gitea_html "code.gitea.io/gitea/modules/html"
3839
"code.gitea.io/gitea/modules/json"
3940
"code.gitea.io/gitea/modules/log"
4041
"code.gitea.io/gitea/modules/markup"
@@ -348,7 +349,7 @@ func NewFuncMap() []template.FuncMap {
348349
}
349350
return false
350351
},
351-
"svg": SVG,
352+
"svg": svg.RenderHTML,
352353
"avatar": Avatar,
353354
"avatarHTML": AvatarHTML,
354355
"avatarByAction": AvatarByAction,
@@ -363,17 +364,17 @@ func NewFuncMap() []template.FuncMap {
363364
if len(urlSort) == 0 && isDefault {
364365
// if sort is sorted as default add arrow tho this table header
365366
if isDefault {
366-
return SVG("octicon-triangle-down", 16)
367+
return svg.RenderHTML("octicon-triangle-down", 16)
367368
}
368369
} else {
369370
// if sort arg is in url test if it correlates with column header sort arguments
370371
// the direction of the arrow should indicate the "current sort order", up means ASC(normal), down means DESC(rev)
371372
if urlSort == normSort {
372373
// the table is sorted with this header normal
373-
return SVG("octicon-triangle-up", 16)
374+
return svg.RenderHTML("octicon-triangle-up", 16)
374375
} else if urlSort == revSort {
375376
// the table is sorted with this header reverse
376-
return SVG("octicon-triangle-down", 16)
377+
return svg.RenderHTML("octicon-triangle-down", 16)
377378
}
378379
}
379380
// the table is NOT sorted with this header
@@ -594,29 +595,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
594595
}}
595596
}
596597

597-
var (
598-
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
599-
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
600-
)
601-
602-
func parseOthers(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
603-
size := defaultSize
604-
if len(others) > 0 && others[0].(int) != 0 {
605-
size = others[0].(int)
606-
}
607-
608-
class := defaultClass
609-
if len(others) > 1 && others[1].(string) != "" {
610-
if defaultClass == "" {
611-
class = others[1].(string)
612-
} else {
613-
class = defaultClass + " " + others[1].(string)
614-
}
615-
}
616-
617-
return size, class
618-
}
619-
620598
// AvatarHTML creates the HTML for an avatar
621599
func AvatarHTML(src string, size int, class, name string) template.HTML {
622600
sizeStr := fmt.Sprintf(`%d`, size)
@@ -628,26 +606,9 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
628606
return template.HTML(`<img class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
629607
}
630608

631-
// SVG render icons - arguments icon name (string), size (int), class (string)
632-
func SVG(icon string, others ...interface{}) template.HTML {
633-
size, class := parseOthers(16, "", others...)
634-
635-
if svgStr, ok := svg.SVGs[icon]; ok {
636-
if size != 16 {
637-
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
638-
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
639-
}
640-
if class != "" {
641-
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
642-
}
643-
return template.HTML(svgStr)
644-
}
645-
return template.HTML("")
646-
}
647-
648609
// Avatar renders user avatars. args: user, size (int), class (string)
649610
func Avatar(item interface{}, others ...interface{}) template.HTML {
650-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)
611+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)
651612

652613
switch t := item.(type) {
653614
case *user_model.User:
@@ -678,7 +639,7 @@ func AvatarByAction(action *activities_model.Action, others ...interface{}) temp
678639

679640
// RepoAvatar renders repo avatars. args: repo, size(int), class (string)
680641
func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTML {
681-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
642+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
682643

683644
src := repo.RelAvatarLink()
684645
if src != "" {
@@ -689,7 +650,7 @@ func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTM
689650

690651
// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
691652
func AvatarByEmail(email, name string, others ...interface{}) template.HTML {
692-
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
653+
size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
693654
src := avatars.GenerateEmailAvatarFastLink(email, size*setting.Avatar.RenderedSizeFactor)
694655

695656
if src != "" {

0 commit comments

Comments
 (0)