调整代码

This commit is contained in:
xing 2023-02-22 22:11:25 +08:00
parent 2aacb682e1
commit 4aa54b1ca7
4 changed files with 86 additions and 68 deletions

View File

@ -1,17 +1,10 @@
package common package common
import ( import (
"context"
"errors"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -30,6 +23,7 @@ type Handle struct {
Templ string Templ string
Class []string Class []string
ThemeMods wpconfig.ThemeMods ThemeMods wpconfig.ThemeMods
Plugins []HandlePluginFn[*Handle]
} }
func NewHandle(c *gin.Context, scene int, theme string) *Handle { func NewHandle(c *gin.Context, scene int, theme string) *Handle {
@ -47,13 +41,6 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
} }
} }
func (h *Handle) GetPassword() {
pw := h.Session.Get("post_password")
if pw != nil {
h.Password = pw.(string)
}
}
func (h *Handle) AutoCal(name string, fn func() string) { func (h *Handle) AutoCal(name string, fn func() string) {
v, ok := reload.GetStr(name) v, ok := reload.GetStr(name)
if !ok { if !ok {
@ -63,63 +50,30 @@ func (h *Handle) AutoCal(name string, fn func() string) {
h.GinH[name] = v h.GinH[name] = v
} }
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins
plugin := GetListPostPlugins(pluginConf, m)
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
}
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
return maps.Copy(pluginFns)
}
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
return func(posts models.Posts) models.Posts {
for _, fn := range call {
fn(&posts)
}
return posts
}
}
func Default[T any](t T) T { func Default[T any](t T) T {
return t return t
} }
func ProjectTitle(t models.Posts) models.Posts { func (h *Handle) GetPassword() {
if t.PostPassword != "" { pw := h.Session.Get("post_password")
plugins.PasswordProjectTitle(&t) if pw != nil {
h.Password = pw.(string)
} }
return t
} }
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] { func (h *Handle) ExecHandlePlugin() {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) { if len(h.Plugins) > 0 {
v, ok := m[t] HandlePlugin(h.Plugins, h)
if ok {
return v, true
} }
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
return nil, false
})
} }
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] { type HandleFn[T any] func(T)
return func(post models.Posts) models.Posts {
if post.PostExcerpt != "" { type HandlePluginFn[T any] func(HandleFn[T], T) HandleFn[T]
plugins.PostExcerpt(&post)
} else { // HandlePlugin 方便把功能写在其它包里
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount) func HandlePlugin[T any](fns []HandlePluginFn[T], h T) HandleFn[T] {
} return slice.ReverseReduce(fns, func(t HandlePluginFn[T], r HandleFn[T]) HandleFn[T] {
if len(calls) > 0 { return t(r, h)
for _, call := range calls { }, func(t T) {})
call(&post)
}
}
return post
}
} }

View File

@ -117,5 +117,6 @@ func (d *DetailHandle) Details() {
d.C.HTML(d.Code, d.Templ, d.GinH) d.C.HTML(d.Code, d.Templ, d.GinH)
return return
} }
d.ExecHandlePlugin()
d.Render() d.Render()
} }

View File

@ -130,5 +130,6 @@ func (i *IndexHandle) Indexs() {
i.C.HTML(i.Code, i.Templ, i.GinH) i.C.HTML(i.Code, i.Templ, i.GinH)
return return
} }
i.ExecHandlePlugin()
i.Render() i.Render()
} }

View File

@ -1,8 +1,13 @@
package common package common
import ( import (
"context"
"errors"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
) )
@ -46,3 +51,60 @@ func Digest(next Fn[models.Posts], h *Handle, post models.Posts) models.Posts {
} }
return post return post
} }
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
return maps.Copy(pluginFns)
}
func ProjectTitle(t models.Posts) models.Posts {
if t.PostPassword != "" {
plugins.PasswordProjectTitle(&t)
}
return t
}
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
v, ok := m[t]
if ok {
return v, true
}
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
return nil, false
})
}
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] {
return func(post models.Posts) models.Posts {
if post.PostExcerpt != "" {
plugins.PostExcerpt(&post)
} else {
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
}
if len(calls) > 0 {
for _, call := range calls {
call(&post)
}
}
return post
}
}
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins
plugin := GetListPostPlugins(pluginConf, m)
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
}
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
return func(posts models.Posts) models.Posts {
for _, fn := range call {
fn(&posts)
}
return posts
}
}