go: create funcmap,bluemonday modules
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-11 18:16:15 +02:00
parent 1fb7479d8e
commit adeb6f5720
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 18 additions and 10 deletions

@ -0,0 +1,5 @@
package bluemonday
import "github.com/microcosm-cc/bluemonday"
var Policy = bluemonday.UGCPolicy()

@ -1,6 +1,10 @@
package template package funcmap
import "html/template" import (
"html/template"
modbluemonday "git.dotya.ml/mirre-mt/pcmt/modules/bluemonday"
)
func FuncMap() template.FuncMap { func FuncMap() template.FuncMap {
return template.FuncMap{ return template.FuncMap{
@ -8,7 +12,7 @@ func FuncMap() template.FuncMap {
"endifIE": func() template.HTML { return template.HTML("<![endif]>") }, "endifIE": func() template.HTML { return template.HTML("<![endif]>") },
"htmlSafe": func(html string) template.HTML { "htmlSafe": func(html string) template.HTML {
return template.HTML( //nolint:gosec return template.HTML( //nolint:gosec
bluemondayPolicy.Sanitize(html), modbluemonday.Policy.Sanitize(html),
) )
}, },
"pageIs": func(want, got string) bool { "pageIs": func(want, got string) bool {

@ -7,8 +7,8 @@ import (
"strings" "strings"
"git.dotya.ml/mirre-mt/pcmt/app/settings" "git.dotya.ml/mirre-mt/pcmt/app/settings"
"git.dotya.ml/mirre-mt/pcmt/modules/funcmap"
"git.dotya.ml/mirre-mt/pcmt/slogging" "git.dotya.ml/mirre-mt/pcmt/slogging"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
) )
@ -17,9 +17,8 @@ type tplMap map[string]*template.Template
var ( var (
templateMap tplMap templateMap tplMap
// embedded templates. // embedded templates.
tmplFS fs.FS tmplFS fs.FS
bluemondayPolicy = bluemonday.UGCPolicy() setting *settings.Settings
setting *settings.Settings
// global logger. // global logger.
slogger *slogging.Slogger slogger *slogging.Slogger
// local logger copy. // local logger copy.
@ -68,7 +67,7 @@ func Get(name string) *template.Template {
allTmpls[i] = strings.TrimPrefix(v, tmplPath+"/") allTmpls[i] = strings.TrimPrefix(v, tmplPath+"/")
t = t.New(allTmpls[i]).Funcs(FuncMap()) t = t.New(allTmpls[i]).Funcs(funcmap.FuncMap())
template.Must(t.Parse(string(rawfile))) template.Must(t.Parse(string(rawfile)))
} }
@ -99,7 +98,7 @@ func initTemplates(f fs.FS) {
allTmpls[i] = strings.TrimPrefix(v, "templates/") allTmpls[i] = strings.TrimPrefix(v, "templates/")
t := Renderer.tmpls.New(allTmpls[i]).Funcs(FuncMap()) t := Renderer.tmpls.New(allTmpls[i]).Funcs(funcmap.FuncMap())
template.Must(t.Parse(string(rawfile))) template.Must(t.Parse(string(rawfile)))
} }
@ -112,7 +111,7 @@ func initTemplates(f fs.FS) {
} }
allTmpls[i] = strings.TrimPrefix(v, tmplPath+"/") allTmpls[i] = strings.TrimPrefix(v, tmplPath+"/")
t = Renderer.tmpls.New(allTmpls[i]).Funcs(FuncMap()) t = Renderer.tmpls.New(allTmpls[i]).Funcs(funcmap.FuncMap())
template.Must(t.Parse(string(rawfile))) template.Must(t.Parse(string(rawfile)))
} }