Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move svg html render to modules/svg #21716

Merged
merged 8 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions modules/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,67 @@

package svg

// SVGs contains discovered SVGs
var SVGs map[string]string
import (
"fmt"
"html/template"
"regexp"
"strings"
)

var (
// SVGs contains discovered SVGs
SVGs map[string]string

widthRe = regexp.MustCompile(`width="[0-9]+?"`)
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
)

// Init discovers SVGs and populates the `SVGs` variable
func Init() {
SVGs = Discover()
}

// ParseOthers get size and class from string with default values
lunny marked this conversation as resolved.
Show resolved Hide resolved
func ParseOthers(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
lunny marked this conversation as resolved.
Show resolved Hide resolved
if len(others) == 0 {
return defaultSize, defaultClass
}

size := defaultSize
_size, ok := others[0].(int)
if ok && _size != 0 {
size = _size
}

if len(others) == 1 {
return size, defaultClass
}

class := defaultClass
if _class, ok := others[1].(string); ok && _class != "" {
if defaultClass == "" {
class = _class
} else {
class = defaultClass + " " + _class
}
}

return size, class
}

// Render render icons - arguments icon name (string), size (int), class (string)
func RenderHTML(icon string, others ...interface{}) template.HTML {
size, class := ParseOthers(16, "", others...)

if svgStr, ok := SVGs[icon]; ok {
if size != 16 {
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
}
if class != "" {
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
}
return template.HTML(svgStr)
}
return template.HTML("")
}
54 changes: 7 additions & 47 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func NewFuncMap() []template.FuncMap {
}
return false
},
"svg": SVG,
"svg": svg.RenderHTML,
"avatar": Avatar,
"avatarHTML": AvatarHTML,
"avatarByAction": AvatarByAction,
Expand All @@ -363,17 +363,17 @@ func NewFuncMap() []template.FuncMap {
if len(urlSort) == 0 && isDefault {
// if sort is sorted as default add arrow tho this table header
if isDefault {
return SVG("octicon-triangle-down", 16)
return svg.RenderHTML("octicon-triangle-down", 16)
}
} else {
// if sort arg is in url test if it correlates with column header sort arguments
// the direction of the arrow should indicate the "current sort order", up means ASC(normal), down means DESC(rev)
if urlSort == normSort {
// the table is sorted with this header normal
return SVG("octicon-triangle-up", 16)
return svg.RenderHTML("octicon-triangle-up", 16)
} else if urlSort == revSort {
// the table is sorted with this header reverse
return SVG("octicon-triangle-down", 16)
return svg.RenderHTML("octicon-triangle-down", 16)
}
}
// the table is NOT sorted with this header
Expand Down Expand Up @@ -594,29 +594,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
}}
}

var (
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
)

func parseOthers(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
size := defaultSize
if len(others) > 0 && others[0].(int) != 0 {
size = others[0].(int)
}

class := defaultClass
if len(others) > 1 && others[1].(string) != "" {
if defaultClass == "" {
class = others[1].(string)
} else {
class = defaultClass + " " + others[1].(string)
}
}

return size, class
}

// AvatarHTML creates the HTML for an avatar
func AvatarHTML(src string, size int, class, name string) template.HTML {
sizeStr := fmt.Sprintf(`%d`, size)
Expand All @@ -628,26 +605,9 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
return template.HTML(`<img class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
}

// SVG render icons - arguments icon name (string), size (int), class (string)
func SVG(icon string, others ...interface{}) template.HTML {
size, class := parseOthers(16, "", others...)

if svgStr, ok := svg.SVGs[icon]; ok {
if size != 16 {
svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
}
if class != "" {
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
}
return template.HTML(svgStr)
}
return template.HTML("")
}

// Avatar renders user avatars. args: user, size (int), class (string)
func Avatar(item interface{}, others ...interface{}) template.HTML {
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)
size, class := svg.ParseOthers(avatars.DefaultAvatarPixelSize, "ui avatar vm", others...)

switch t := item.(type) {
case *user_model.User:
Expand Down Expand Up @@ -678,7 +638,7 @@ func AvatarByAction(action *activities_model.Action, others ...interface{}) temp

// RepoAvatar renders repo avatars. args: repo, size(int), class (string)
func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTML {
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
size, class := svg.ParseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)

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

// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
func AvatarByEmail(email, name string, others ...interface{}) template.HTML {
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
size, class := svg.ParseOthers(avatars.DefaultAvatarPixelSize, "ui avatar", others...)
src := avatars.GenerateEmailAvatarFastLink(email, size*setting.Avatar.RenderedSizeFactor)

if src != "" {
Expand Down