From 20ea36c727b6bbc202e9f3f6da15b287c413f7a3 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 23 Jan 2024 22:59:15 +0800 Subject: [PATCH] fix bug and optimize code and update dependence package version --- app/route/route.go | 2 +- app/theme/fs.go | 44 ++++++++++++---- app/theme/theme.go | 12 ++--- app/theme/wp/pipe.go | 88 +++++++++++++++----------------- app/theme/wp/wp.go | 23 +++++---- go.mod | 18 +++---- go.sum | 18 +++++++ multipTemplate/multiptemplate.go | 54 +++++++++++++++----- 8 files changed, 163 insertions(+), 96 deletions(-) diff --git a/app/route/route.go b/app/route/route.go index 9c2af38..14cdbb1 100644 --- a/app/route/route.go +++ b/app/route/route.go @@ -35,7 +35,7 @@ func SetupRouter() *gin.Engine { } } - r.HTMLRender = theme.Template() + r.HTMLRender = theme.BuildTemplate() wpconfig.SetTemplateFs(theme.TemplateFs) siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) r.Use( diff --git a/app/theme/fs.go b/app/theme/fs.go index 9a1be26..2e7efb4 100644 --- a/app/theme/fs.go +++ b/app/theme/fs.go @@ -3,6 +3,7 @@ package theme import ( "embed" "github.com/fthvgb1/wp-go/multipTemplate" + "github.com/fthvgb1/wp-go/safety" "html/template" "io/fs" "path/filepath" @@ -12,20 +13,37 @@ import ( //go:embed *[^.go] var TemplateFs embed.FS -var templates map[string]*template.Template //方便外部获取模板render后的字符串,不然在gin中获取不了 +var templates = safety.NewMap[string, *template.Template]() //方便外部获取模板render后的字符串,不然在gin中获取不了 -func Template() *multipTemplate.MultipleFsTemplate { - t := multipTemplate.NewFsTemplate(TemplateFs) - templates = t.Template - t.FuncMap = FuncMap() - commonTemplate(t) - /*t.AddTemplate("twentyfifteen/*[^layout]/*.gohtml", FuncMap(), "twentyfifteen/layout/*.gohtml"). //单个主题设置 - AddTemplate("twentyseventeen/*[^layout]/*.gohtml", FuncMap(), "twentyseventeen/layout/*.gohtml")*/ - return t +var multiple *multipTemplate.MultipleFsTemplate + +func BuildTemplate() *multipTemplate.MultipleFsTemplate { + if multiple != nil { + tt := multipTemplate.NewFsTemplate(TemplateFs) + commonTemplate(tt) + for k, v := range map[string]*template.Template(any(tt.Template).(multipTemplate.TemplateMaps)) { + multiple.Template.Store(k, v) + } + } else { + multiple = multipTemplate.NewFsTemplates(TemplateFs, templates) + multiple.FuncMap = FuncMap() + commonTemplate(multiple) + } + + /*t.AddTemplate("twentyfifteen/*[^layout]/*.gohtml", FuncMap(), "twentyfifteen/layout/*.gohtml","wp/template.gohtml"). //单个主题设置 + AddTemplate("twentyseventeen/*[^layout]/*.gohtml", FuncMap(), "twentyseventeen/layout/*.gohtml","wp/template.gohtml")*/ + return multiple +} + +func GetMultipleTemplate() *multipTemplate.MultipleFsTemplate { + if multiple == nil { + BuildTemplate() + } + return multiple } func GetTemplate(name string) (*template.Template, bool) { - t, ok := templates[name] + t, ok := templates.Load(name) return t, ok } @@ -55,6 +73,10 @@ func IsTemplateDirExists(tml string) bool { } func IsTemplateExists(tml string) bool { - t, ok := templates[tml] + t, ok := templates.Load(tml) return ok && t != nil } + +func SetTemplate(name string, val *template.Template) { + templates.Store(name, val) +} diff --git a/app/theme/theme.go b/app/theme/theme.go index 1a11c44..03676f5 100644 --- a/app/theme/theme.go +++ b/app/theme/theme.go @@ -13,12 +13,12 @@ func InitTheme() { } func GetCurrentTemplateName() string { - tmlp := config.GetConfig().Theme - if tmlp == "" { - tmlp = wpconfig.GetOption("template") + templateName := config.GetConfig().Theme + if templateName == "" { + templateName = wpconfig.GetOption("template") } - if !IsTemplateDirExists(tmlp) { - tmlp = "twentyfifteen" + if !IsTemplateDirExists(templateName) { + templateName = "twentyfifteen" } - return tmlp + return templateName } diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index 4983cbb..5e269ae 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -67,10 +67,11 @@ func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) { } func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string, - handleHook func(*Handle, map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool), - handlesFn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(HandleFn[*Handle], *Handle) { + handleHookFn func(*Handle, string, + map[string][]func(HandleCall) (HandleCall, bool), + map[string][]HandleCall) []HandleCall) func(HandleFn[*Handle], *Handle) { - pipeHandlerFn := reload.BuildMapFn[string]("pipeHandlers", BuildHandler(pipeScene, keyFn, handleHook, handlesFn)) + pipeHandlerFn := reload.BuildMapFn[string]("pipeHandlers", BuildHandler(pipeScene, keyFn, handleHookFn)) return func(next HandleFn[*Handle], h *Handle) { key := keyFn(h, pipeScene) @@ -88,8 +89,9 @@ func BuildHandlers(pipeScene string, keyFn func(*Handle, string) string, } func BuildHandler(pipeScene string, keyFn func(*Handle, string) string, - handleHook func(*Handle, map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool), - handlesFn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(*Handle) []HandleCall { + handleHook func(*Handle, string, + map[string][]func(HandleCall) (HandleCall, bool), + map[string][]HandleCall) []HandleCall) func(*Handle) []HandleCall { return func(h *Handle) []HandleCall { key := keyFn(h, pipeScene) @@ -98,18 +100,7 @@ func BuildHandler(pipeScene string, keyFn func(*Handle, string) string, pipeHookers, _ := handleHooks.Load(pipeScene) pipeHandlers, _ := handlerss.Load(pipeScene) mut.Unlock() - hookers := handleHook(h, pipeHookers) - calls := handlesFn(h, pipeHandlers, key) - calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { - ok := true - for _, hook := range hookers { - call, ok = hook(call) - if !ok { - break - } - } - return call, ok - }) + calls := handleHook(h, key, pipeHookers, pipeHandlers) slice.SimpleSort(calls, slice.DESC, func(t HandleCall) float64 { return t.Order }) @@ -117,6 +108,22 @@ func BuildHandler(pipeScene string, keyFn func(*Handle, string) string, } } +func HookHandles(hooks map[string][]func(HandleCall) (HandleCall, bool), + handlers map[string][]HandleCall, sceneOrStats ...string) []HandleCall { + hookedHandlers := slice.FilterAndMap(sceneOrStats, func(k string) ([]HandleCall, bool) { + r := handlers[k] + for _, hook := range hooks[k] { + r = HookHandler(hook, r) + } + return r, true + }) + return slice.Decompress(hookedHandlers) +} + +func HookHandler(hookFn func(HandleCall) (HandleCall, bool), handlers []HandleCall) []HandleCall { + return slice.FilterAndMap(handlers, hookFn) +} + func PipeKey(h *Handle, pipScene string) string { key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats) return h.DoActionFilter("pipeKey", key, pipScene) @@ -163,36 +170,25 @@ func MiddlewareKey(h *Handle, pipScene string) string { return h.DoActionFilter("middleware", str.Join("pipe-middleware-", h.scene), pipScene) } -func PipeMiddlewareHandle(h *Handle, middlewares map[string][]HandleCall, key string) (handlers []HandleCall) { - handlers = append(handlers, middlewares[h.scene]...) - handlers = append(handlers, middlewares[constraints.AllScene]...) - handlers = h.PipeHandleHook("PipeMiddlewareHandle", handlers, middlewares, key) - return +func PipeMiddlewareHandle(h *Handle, key string, + hooks map[string][]func(HandleCall) (HandleCall, bool), + handlers map[string][]HandleCall) []HandleCall { + hookedHandles := HookHandles(hooks, handlers, h.scene, constraints.AllScene) + return h.PipeHandleHook("PipeMiddlewareHandle", hookedHandles, handlers, key) } -func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall, key string) (handlers []HandleCall) { - handlers = append(handlers, dataHandlers[h.scene]...) - handlers = append(handlers, dataHandlers[constraints.AllScene]...) - handlers = h.PipeHandleHook("PipeDataHandle", handlers, dataHandlers, key) - return +func PipeDataHandle(h *Handle, key string, + hooks map[string][]func(HandleCall) (HandleCall, bool), + handlers map[string][]HandleCall) []HandleCall { + hookedHandles := HookHandles(hooks, handlers, h.scene, constraints.AllScene) + return h.PipeHandleHook("PipeDataHandle", hookedHandles, handlers, key) } -func PipeRender(h *Handle, renders map[string][]HandleCall, key string) (handlers []HandleCall) { - handlers = append(handlers, renders[h.Stats]...) - handlers = append(handlers, renders[h.scene]...) - handlers = append(handlers, renders[constraints.AllStats]...) - handlers = append(handlers, renders[constraints.AllScene]...) - handlers = h.PipeHandleHook("PipeRender", handlers, renders, key) - return -} - -func HandleHook(h *Handle, hooks map[string][]func(HandleCall) (HandleCall, bool)) []func(HandleCall) (HandleCall, bool) { - var r []func(HandleCall) (HandleCall, bool) - r = append(r, hooks[h.scene]...) - r = append(r, hooks[h.Stats]...) - r = append(r, hooks[constraints.AllScene]...) - r = append(r, hooks[constraints.AllStats]...) - return r +func PipeRender(h *Handle, key string, + hooks map[string][]func(HandleCall) (HandleCall, bool), + handlers map[string][]HandleCall) []HandleCall { + hookedHandles := HookHandles(hooks, handlers, h.scene, h.Stats, constraints.AllScene, constraints.AllStats) + return h.PipeHandleHook("PipeRender", hookedHandles, handlers, key) } // DeleteHandle 写插件的时候用 @@ -245,10 +241,10 @@ func (h *Handle) PipeHandleHook(name string, calls []HandleCall, m map[string][] func InitPipe(h *Handle) { h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeMiddleware, 300, - BuildHandlers(constraints.PipeMiddleware, MiddlewareKey, HandleHook, PipeMiddlewareHandle))) + BuildHandlers(constraints.PipeMiddleware, MiddlewareKey, PipeMiddlewareHandle))) h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeData, 200, - BuildHandlers(constraints.PipeData, PipeKey, HandleHook, PipeDataHandle))) + BuildHandlers(constraints.PipeData, PipeKey, PipeDataHandle))) h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeRender, 100, - BuildHandlers(constraints.PipeRender, PipeKey, HandleHook, PipeRender))) + BuildHandlers(constraints.PipeRender, PipeKey, PipeRender))) } diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index 168b55c..a5ca158 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -120,6 +120,9 @@ type ConfigParm struct { func InitHandle(configFn func(*Handle), h *Handle) { hh := GetInitHandleFn(configFn, h) + mods, err := wpconfig.GetThemeMods(h.theme) + logs.IfError(err, "获取mods失败") + h.themeMods = mods h.ginH = maps.Copy(hh.ginH) h.ginH["calPostClass"] = postClass(h) h.ginH["calBodyClass"] = bodyClass(h) @@ -170,15 +173,12 @@ func (h *Handle) SetData(k string, v any) { } func NewHandle(c *gin.Context, scene string, theme string) *Handle { - mods, err := wpconfig.GetThemeMods(theme) - logs.IfError(err, "获取mods失败") return &Handle{ - C: c, - theme: theme, - Session: sessions.Default(c), - scene: scene, - Stats: constraints.Ok, - themeMods: mods, + C: c, + theme: theme, + Session: sessions.Default(c), + scene: scene, + Stats: constraints.Ok, } } @@ -223,7 +223,12 @@ func (h *Handle) RenderHtml(t *template.Template, statsCode int, name string) { header["Content-Type"] = htmlContentType } h.C.Status(statsCode) - err := t.ExecuteTemplate(h.C.Writer, name, h.ginH) + var err error + if name == "" { + err = t.Execute(h.C.Writer, h.ginH) + } else { + err = t.ExecuteTemplate(h.C.Writer, name, h.ginH) + } h.Abort() if err != nil { panic(err) diff --git a/go.mod b/go.mod index 0b2bfe8..7128ec7 100644 --- a/go.mod +++ b/go.mod @@ -11,12 +11,12 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 - github.com/go-playground/validator/v10 v10.16.0 + github.com/go-playground/validator/v10 v10.17.0 github.com/go-sql-driver/mysql v1.7.1 github.com/goccy/go-json v0.10.2 github.com/jmoiron/sqlx v1.3.5 - golang.org/x/crypto v0.15.0 - golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa + golang.org/x/crypto v0.18.0 + golang.org/x/exp v0.0.0-20240119083558-1b970713d09a gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 ) @@ -36,14 +36,14 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.6.0 // indirect - golang.org/x/net v0.18.0 // indirect - golang.org/x/sys v0.14.0 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.7.0 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 626afb8..fa8960d 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7N github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= +github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= @@ -112,6 +114,8 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -136,6 +140,8 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6 github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -143,6 +149,8 @@ golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y= golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= +golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= @@ -150,12 +158,16 @@ golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= @@ -163,6 +175,8 @@ golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -175,6 +189,8 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -190,6 +206,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/multipTemplate/multiptemplate.go b/multipTemplate/multiptemplate.go index 4c886c9..bc98261 100644 --- a/multipTemplate/multiptemplate.go +++ b/multipTemplate/multiptemplate.go @@ -9,7 +9,7 @@ import ( ) type MultipleFileTemplate struct { - Template map[string]*template.Template + Template maps FuncMap template.FuncMap } type MultipleFsTemplate struct { @@ -17,32 +17,57 @@ type MultipleFsTemplate struct { Fs embed.FS } +type TemplateMaps map[string]*template.Template + +func (m TemplateMaps) Load(name string) (*template.Template, bool) { + v, ok := m[name] + return v, ok +} + +func (m TemplateMaps) Store(name string, v *template.Template) { + m[name] = v +} + +type maps interface { + Load(name string) (*template.Template, bool) + Store(name string, v *template.Template) +} + func (t *MultipleFileTemplate) AppendTemplate(name string, templates ...string) *MultipleFileTemplate { - tmpl, ok := t.Template[name] + tmpl, ok := t.Template.Load(name) if ok { - t.Template[name] = template.Must(tmpl.ParseFiles(templates...)) + t.Template.Store(name, template.Must(tmpl.ParseFiles(templates...))) } return t } func (t *MultipleFsTemplate) AppendTemplate(name string, templates ...string) *MultipleFsTemplate { - tmpl, ok := t.Template[name] + tmpl, ok := t.Template.Load(name) if ok { - t.Template[name] = template.Must(tmpl.ParseFS(t.Fs, templates...)) + t.Template.Store(name, template.Must(tmpl.ParseFS(t.Fs, templates...))) } return t } -func NewFileTemplate() *MultipleFileTemplate { +func NewFileTemplates(m maps) *MultipleFileTemplate { return &MultipleFileTemplate{ - Template: make(map[string]*template.Template), + Template: m, FuncMap: make(template.FuncMap), } } func NewFsTemplate(f embed.FS) *MultipleFsTemplate { return &MultipleFsTemplate{ MultipleFileTemplate: MultipleFileTemplate{ - Template: make(map[string]*template.Template), + Template: TemplateMaps(make(map[string]*template.Template)), + FuncMap: make(template.FuncMap), + }, + Fs: f, + } +} +func NewFsTemplates(f embed.FS, m maps) *MultipleFsTemplate { + return &MultipleFsTemplate{ + MultipleFileTemplate: MultipleFileTemplate{ + Template: m, FuncMap: make(template.FuncMap), }, Fs: f, @@ -50,10 +75,10 @@ func NewFsTemplate(f embed.FS) *MultipleFsTemplate { } func (t *MultipleFileTemplate) SetTemplate(name string, templ *template.Template) *MultipleFileTemplate { - if _, ok := t.Template[name]; ok { + if _, ok := t.Template.Load(name); ok { panic("exists same template " + name) } - t.Template[name] = templ + t.Template.Store(name, templ) return t } @@ -65,14 +90,15 @@ func (t *MultipleFileTemplate) AddTemplate(mainTemplatePattern string, fnMap tem for _, mainTemplate := range mainTemplates { file := filepath.Base(mainTemplate) pattern := append([]string{mainTemplate}, layoutTemplatePattern...) - t.Template[mainTemplate] = template.Must(template.New(file).Funcs(fnMap).ParseFiles(pattern...)) + t.Template.Store(mainTemplate, template.Must(template.New(file).Funcs(fnMap).ParseFiles(pattern...))) } return t } func (t *MultipleFileTemplate) Instance(name string, data any) render.Render { + v, _ := t.Template.Load(name) return render.HTML{ - Template: t.Template[name], + Template: v, Data: data, } } @@ -83,12 +109,12 @@ func (t *MultipleFsTemplate) AddTemplate(mainTemplatePattern string, fnMap templ panic(err) } for _, mainTemplate := range mainTemplates { - if _, ok := t.Template[mainTemplate]; ok { + if _, ok := t.Template.Load(mainTemplate); ok { panic("exists same Template " + mainTemplate) } file := filepath.Base(mainTemplate) pattern := append([]string{mainTemplate}, layoutTemplatePattern...) - t.Template[mainTemplate] = template.Must(template.New(file).Funcs(fnMap).ParseFS(t.Fs, pattern...)) + t.Template.Store(mainTemplate, template.Must(template.New(file).Funcs(fnMap).ParseFS(t.Fs, pattern...))) } return t }