2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-05-11 18:16:15 +02:00
|
|
|
package funcmap
|
2023-05-11 04:32:39 +02:00
|
|
|
|
2023-05-11 18:16:15 +02:00
|
|
|
import (
|
|
|
|
"html/template"
|
2023-05-12 00:11:23 +02:00
|
|
|
"io/fs"
|
2023-08-24 18:43:24 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-05-11 18:16:15 +02:00
|
|
|
|
|
|
|
modbluemonday "git.dotya.ml/mirre-mt/pcmt/modules/bluemonday"
|
|
|
|
)
|
2023-05-11 04:32:39 +02:00
|
|
|
|
2023-05-12 00:11:23 +02:00
|
|
|
var (
|
|
|
|
embedAssets *fs.FS
|
|
|
|
isLive = true
|
|
|
|
)
|
|
|
|
|
2023-05-11 04:32:39 +02:00
|
|
|
func FuncMap() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
|
|
|
"ifIE": func() template.HTML { return template.HTML("<!--[if IE]>") },
|
|
|
|
"endifIE": func() template.HTML { return template.HTML("<![endif]>") },
|
|
|
|
"htmlSafe": func(html string) template.HTML {
|
|
|
|
return template.HTML( //nolint:gosec
|
2023-05-11 18:16:15 +02:00
|
|
|
modbluemonday.Policy.Sanitize(html),
|
2023-05-11 04:32:39 +02:00
|
|
|
)
|
|
|
|
},
|
2023-08-24 18:43:24 +02:00
|
|
|
// a voluntarily unsafe func.
|
|
|
|
"htmlRaw": func(value string) template.HTML {
|
|
|
|
// nolint:gosec
|
|
|
|
return template.HTML(value)
|
|
|
|
},
|
|
|
|
"htmlLinkStyle": func(value string) string {
|
|
|
|
value = strings.ReplaceAll(value, "<a ", `<span><a class="w-auto py-1 mt-2 text-center text-blue-500 md:mt-0 mx-0 hover:underline dark:text-blue-400"`)
|
|
|
|
return strings.ReplaceAll(value, "</a>", `</a></span>`)
|
|
|
|
},
|
2023-05-11 04:32:39 +02:00
|
|
|
"pageIs": func(want, got string) bool {
|
|
|
|
return want == got
|
|
|
|
},
|
2023-05-12 00:11:23 +02:00
|
|
|
"sha256": func(path string) string {
|
|
|
|
t := New("sha256")
|
|
|
|
|
|
|
|
r, err := t.Integrity(path, isLive)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:53:25 +02:00
|
|
|
return *r
|
|
|
|
},
|
|
|
|
"sha384": func(path string) string {
|
|
|
|
t := New("sha384")
|
|
|
|
|
|
|
|
r, err := t.Integrity(path, isLive)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return *r
|
|
|
|
},
|
|
|
|
"sha512": func(path string) string {
|
|
|
|
t := New("sha512")
|
|
|
|
|
|
|
|
r, err := t.Integrity(path, isLive)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-05-12 00:11:23 +02:00
|
|
|
return *r
|
|
|
|
},
|
2023-08-24 18:43:24 +02:00
|
|
|
"usrFinishedSetup": func(lastLogin time.Time) bool {
|
|
|
|
return lastLogin.After(time.Unix(0, 0))
|
|
|
|
},
|
2023-05-11 04:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 00:11:23 +02:00
|
|
|
// SetEmbeds saves the pointer to embedded assets (and toggles the isLive var).
|
|
|
|
func SetEmbeds(embeds *fs.FS) {
|
|
|
|
embedAssets = embeds
|
|
|
|
isLive = false
|
|
|
|
}
|
|
|
|
|
2023-05-11 04:32:39 +02:00
|
|
|
// TODO: mimic https://github.com/drone/funcmap/blob/master/funcmap.go
|
|
|
|
func setFuncMap(t *template.Template) { //nolint:unused
|
|
|
|
t.Funcs(FuncMap())
|
|
|
|
}
|