Convert some functions and methods

This commit is contained in:
VnPower
2024-06-06 10:50:22 +07:00
parent 60437bbb01
commit df2a71d77a
6 changed files with 48 additions and 54 deletions
+4 -44
View File
@@ -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,
}
}
+1 -1
View File
@@ -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,
})
}
+1 -2
View File
@@ -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})
}
+35
View File
@@ -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
},
}
}
+2 -2
View File
@@ -1,13 +1,13 @@
<div class="container">
<h1>Discover artworks</h1>
<div class="switcher">
<div class="switch-title">Filter</div>
<div class="switch-title">Type</div>
<a href="/discovery" class="switch-button">Artworks</a>
<a href="/discovery/novel" class="switch-button">Novels</a>
</div>
<div class="switcher">
<div class="switch-title">Filter</div>
{{ URL := URLC("mode") }}
{{ URL := URLC(Queries, "mode") }}
<a href='{{URL}}all' class="switch-button">All</a>
<a href='{{URL}}safe' class="switch-button">Safe</a>
<a href='{{URL}}r18' class="switch-button">R-18</a>
+5 -5
View File
@@ -29,27 +29,27 @@
<div class="switcher">
<div class="switch-title">Type</div>
{{ URL := URLC("category") }}
{{ URL := URLC(Queries, "category") }}
<a href='{{URL}}artworks#checkpoint' class="switch-button">All</a>
<a href='{{URL}}illustrations#checkpoint' class="switch-button">Illustrations</a>
<a href='{{URL}}manga#checkpoint' class="switch-button">Manga</a>
</div>
<div class="switcher">
<div class="switch-title">Order</div>
{{ URL := URLC("order") }}
{{ URL := URLC(Queries, "order") }}
<a href='{{URL}}date_d#checkpoint' class="switch-button">Newest</a>
<a href='{{URL}}date#checkpoint' class="switch-button">Oldest</a>
</div>
<div class="switcher">
<div class="switch-title">Filter</div>
{{ URL := URLC("mode") }}
{{ URL := URLC(Queries, "mode") }}
<a href='{{URL}}all#checkpoint' class="switch-button">All</a>
<a href='{{URL}}safe#checkpoint' class="switch-button">Safe</a>
<a href='{{URL}}r18#checkpoint' class="switch-button">R-18</a>
</div>
<div class="switcher">
<div class="switch-title">Ratio</div>
{{ URL := URLC("ratio") }}
{{ URL := URLC(Queries, "ratio") }}
<a href='{{URL}}#checkpoint' class="switch-button">All</a>
<a href='{{URL}}-0.5#checkpoint' class="switch-button">Portrait</a>
<a href='{{URL}}0#checkpoint' class="switch-button">Square</a>
@@ -74,7 +74,7 @@
</div>
<div class="pagination">
{{ url := URLC("page") }}
{{ url := URLC(Queries, "page") }}
<small>Page {{ Page }}</small>
<br />
{{ raw: createPaginator(url, "#checkpoint", Page, -1) }}