diff --git a/core/http/constructurl.go b/core/http/constructurl.go index d57b518..83cc5d0 100644 --- a/core/http/constructurl.go +++ b/core/http/constructurl.go @@ -1,53 +1,13 @@ package core -import ( - "fmt" - "strings" -) - type URLConstructor struct { - path string - hash map[string]string -} - -func lowercaseFirstChar(s string) string { - return strings.ToLower(s[0:1]) + s[1:] -} - -func (obj *URLConstructor) Replace(name string) string { - url := fmt.Sprintf("/%s", obj.path) - first := true - - for k, v := range obj.hash { - k = lowercaseFirstChar(k) - - if k == name { - continue - } - if first { - url += "?" - first = false - } else { - url += "&" - } - url += fmt.Sprintf("%s=%s", k, v) - } - - // This is to move the matched query to the end of the URL - var t string - if first { - t = "?" - } else { - t = "&" - } - url += fmt.Sprintf("%s%s=", t, name) - - return url + Path string + Hash map[string]string } func NewURLConstruct(path string, obj map[string]string) URLConstructor { return URLConstructor{ - path: path, - hash: obj, + Path: path, + Hash: obj, } } diff --git a/pages/discovery.go b/pages/discovery.go index 88b0675..f8d7053 100644 --- a/pages/discovery.go +++ b/pages/discovery.go @@ -19,7 +19,7 @@ func DiscoveryPage(c *fiber.Ctx) error { return c.Render("discovery", fiber.Map{ "Artworks": works, "Title": "Discovery", - "URLC": urlc.Replace, + "Queries": urlc, }) } diff --git a/pages/tag.go b/pages/tag.go index 0b2e1a9..89ea38e 100644 --- a/pages/tag.go +++ b/pages/tag.go @@ -44,6 +44,5 @@ func TagPage(c *fiber.Ctx) error { urlc := site.NewURLConstruct("tags", queries.ReturnMap()) - return c.Render("tag", fiber.Map{"Title": "Results for " + name, "Tag": tag, "Data": result, "Queries": queries.ReturnMap(), "TrueTag": param, "Page": pageInt, "URLC": urlc.Replace}) - + return c.Render("tag", fiber.Map{"Title": "Results for " + name, "Tag": tag, "Data": result, "Queries": urlc, "TrueTag": param, "Page": pageInt}) } diff --git a/serve/template.go b/serve/template.go index fdc4935..20cefd0 100644 --- a/serve/template.go +++ b/serve/template.go @@ -10,6 +10,7 @@ import ( "strings" "time" + site "codeberg.org/vnpower/pixivfe/v2/core/http" core "codeberg.org/vnpower/pixivfe/v2/core/webapi" ) @@ -234,6 +235,10 @@ func GetNovelGenre(s string) string { return fmt.Sprintf("(Unknown Genre %s)", s) } +func lowercaseFirstChar(s string) string { + return strings.ToLower(s[0:1]) + s[1:] +} + func GetTemplateFunctions() template.FuncMap { return template.FuncMap{ "parseEmojis": func(s string) template.HTML { @@ -296,5 +301,35 @@ func GetTemplateFunctions() template.FuncMap { "floor": func(i float64) int { return int(math.Floor(i)) }, + "URLC": func(obj site.URLConstructor, name string) string { + url := fmt.Sprintf("/%s", obj.Path) + first := true + + for k, v := range obj.Hash { + k = lowercaseFirstChar(k) + + if k == name { + continue + } + if first { + url += "?" + first = false + } else { + url += "&" + } + url += fmt.Sprintf("%s=%s", k, v) + } + + // This is to move the matched query to the end of the URL + var t string + if first { + t = "?" + } else { + t = "&" + } + url += fmt.Sprintf("%s%s=", t, name) + + return url + }, } } diff --git a/views/pages/discovery.jet.html b/views/pages/discovery.jet.html index cd88ac7..542b787 100644 --- a/views/pages/discovery.jet.html +++ b/views/pages/discovery.jet.html @@ -1,13 +1,13 @@