package template
import (
"fmt"
"math"
"math/rand"
"net/url"
"regexp"
"strings"
"time"
"codeberg.org/vnpower/pixivfe/v2/core"
)
type HTML = core.HTML
func GetRandomColor() string {
// Some color shade I stole
colors := []string{
// Green
"#C8847E",
"#C8A87E",
"#C8B87E",
"#C8C67E",
"#C7C87E",
"#C2C87E",
"#BDC87E",
"#82C87E",
"#82C87E",
"#7EC8AF",
"#7EAEC8",
"#7EA6C8",
"#7E99C8",
"#7E87C8",
"#897EC8",
"#967EC8",
"#AE7EC8",
"#B57EC8",
"#C87EA5",
}
// Randomly choose one and return
return colors[rand.Intn(len(colors))]
}
func ParseEmojis(s string) HTML {
emojiList := map[string]string{
"normal": "101",
"surprise": "102",
"serious": "103",
"heaven": "104",
"happy": "105",
"excited": "106",
"sing": "107",
"cry": "108",
"normal2": "201",
"shame2": "202",
"love2": "203",
"interesting2": "204",
"blush2": "205",
"fire2": "206",
"angry2": "207",
"shine2": "208",
"panic2": "209",
"normal3": "301",
"satisfaction3": "302",
"surprise3": "303",
"smile3": "304",
"shock3": "305",
"gaze3": "306",
"wink3": "307",
"happy3": "308",
"excited3": "309",
"love3": "310",
"normal4": "401",
"surprise4": "402",
"serious4": "403",
"love4": "404",
"shine4": "405",
"sweat4": "406",
"shame4": "407",
"sleep4": "408",
"heart": "501",
"teardrop": "502",
"star": "503",
}
regex := regexp.MustCompile(`\(([^)]+)\)`)
parsedString := regex.ReplaceAllStringFunc(s, func(s string) string {
s = s[1 : len(s)-1] // Get the string inside
id := emojiList[s]
return fmt.Sprintf(`
`, id, s)
})
return HTML(parsedString)
}
func ParsePixivRedirect(s string) HTML {
regex := regexp.MustCompile(`\/jump\.php\?(http[^"]+)`)
parsedString := regex.ReplaceAllStringFunc(s, func(s string) string {
s = s[10:]
return s
})
escaped, err := url.QueryUnescape(parsedString)
if err != nil {
return HTML(s)
}
return HTML(escaped)
}
func EscapeString(s string) string {
escaped := url.QueryEscape(s)
return escaped
}
func ParseTime(date time.Time) string {
return date.Format("2006-01-02 15:04")
}
func ParseTimeCustomFormat(date time.Time, format string) string {
return date.Format(format)
}
func CreatePaginator(base, ending string, current_page, max_page int) HTML {
pageUrl := func(page int) string {
return fmt.Sprintf(`%s%d%s`, base, page, ending)
}
const (
peek = 5 // this can be changed freely
limit = peek*2 + 1 // tied to the algorithm below, do not change
)
hasMaxPage := max_page != -1
count := 0
pages := ""
pages += `