73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package funcmap
|
|
|
|
import (
|
|
"html/template"
|
|
"io/fs"
|
|
|
|
modbluemonday "git.dotya.ml/mirre-mt/pcmt/modules/bluemonday"
|
|
)
|
|
|
|
var (
|
|
embedAssets *fs.FS
|
|
isLive = true
|
|
)
|
|
|
|
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
|
|
modbluemonday.Policy.Sanitize(html),
|
|
)
|
|
},
|
|
"pageIs": func(want, got string) bool {
|
|
return want == got
|
|
},
|
|
"sha256": func(path string) string {
|
|
t := New("sha256")
|
|
|
|
r, err := t.Integrity(path, isLive)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
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 ""
|
|
}
|
|
|
|
return *r
|
|
},
|
|
}
|
|
}
|
|
|
|
// SetEmbeds saves the pointer to embedded assets (and toggles the isLive var).
|
|
func SetEmbeds(embeds *fs.FS) {
|
|
embedAssets = embeds
|
|
isLive = false
|
|
}
|
|
|
|
// TODO: mimic https://github.com/drone/funcmap/blob/master/funcmap.go
|
|
func setFuncMap(t *template.Template) { //nolint:unused
|
|
t.Funcs(FuncMap())
|
|
}
|