From daa9a26e00edfbae3757aae36b4c6aa2435dc663 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 9 Feb 2023 23:15:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=94=AF=E6=8C=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E3=80=81=E7=83=AD=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 4 +- helper/maps/map.go | 23 ++++++++++ internal/pkg/config/config.go | 1 + internal/theme/common/common.go | 43 ++++++++++++++++++- internal/theme/twentyfifteen/twentyfifteen.go | 5 +-- .../theme/twentyseventeen/twentyseventeen.go | 13 +++--- 6 files changed, 75 insertions(+), 14 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 91cdb90..8a5b84e 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -88,4 +88,6 @@ postOrder: "desc" # 上传的目录 uploadDir: "" # pprof route path 为空表示不开启pprof,否则为pprof的路由 -pprof: "/debug/pprof" \ No newline at end of file +pprof: "/debug/pprof" +# 列表页面post使用的插件 +listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"] \ No newline at end of file diff --git a/helper/maps/map.go b/helper/maps/map.go index f4c2016..bd4e8f6 100644 --- a/helper/maps/map.go +++ b/helper/maps/map.go @@ -85,3 +85,26 @@ func Replace[K comparable, V any](m map[K]V, mm ...map[K]V) map[K]V { } return m } + +func Copy[K comparable, V any](m map[K]V) map[K]V { + r := make(map[K]V) + for k, v := range m { + r[k] = v + } + return r +} + +func Merge[K comparable, V any](m ...map[K]V) map[K]V { + if len(m) < 1 { + panic("no map") + } else if len(m) < 2 { + return m[0] + } + mm := m[0] + for _, m2 := range m[1:] { + for k, v := range m2 { + mm[k] = v + } + } + return mm +} diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 5901c6a..3a892d7 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -31,6 +31,7 @@ type Config struct { PostOrder string `yaml:"postOrder"` UploadDir string `yaml:"uploadDir"` Pprof string `yaml:"pprof"` + ListPagePlugins []string `yaml:"listPagePlugins"` } type CacheTime struct { diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 62feaec..84966c0 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -2,8 +2,12 @@ package common import ( "context" + "errors" + "github.com/fthvgb1/wp-go/helper/maps" "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/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" "github.com/gin-gonic/gin" @@ -26,6 +30,14 @@ func (h Handle) Index() { } +func (h Handle) ExecListPagePlugin(m map[string]Plugin[models.Posts], calls ...func(*models.Posts)) { + pluginConf := config.GetConfig().ListPagePlugins + plugin := GetPlugins(pluginConf, m) + h.GinH["posts"] = slice.Map( + h.GinH["posts"].([]models.Posts), + PluginFn[models.Posts](plugin, h, Defaults(calls...))) +} + type Fn[T any] func(T) T type Plugin[T any] func(next Fn[T], h Handle, t T) T @@ -41,14 +53,40 @@ var plugin = []Plugin[models.Posts]{ PasswordProject, } -func Plugins() []Plugin[models.Posts] { - return slice.Copy(plugin) +var pluginFns = map[string]Plugin[models.Posts]{ + "passwordProject": PasswordProject, + "digest": Digest, +} + +func Plugins() map[string]Plugin[models.Posts] { + 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 { return t } +func GetPlugins(name []string, m map[string]Plugin[models.Posts]) []Plugin[models.Posts] { + return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts], bool) { + v, ok := m[t] + if ok { + return v, true + } + logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "") + return nil, false + }) +} + +// PasswordProject 标题和内容密码保护 func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r models.Posts) { r = post if post.PostPassword != "" { @@ -69,6 +107,7 @@ func ProjectTitle(t models.Posts) models.Posts { return t } +// Digest 生成摘要 func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts { if post.PostExcerpt != "" { plugins.PostExcerpt(&post) diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index db9ee6e..39e3102 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -1,7 +1,6 @@ package twentyfifteen import ( - "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/theme/common" @@ -37,9 +36,7 @@ var plugin = common.Plugins() func (h handle) Index() { if h.Stats != plugins.Empty404 { - h.GinH["posts"] = slice.Map( - h.GinH["posts"].([]models.Posts), - common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C))) + h.ExecListPagePlugin(plugin) p, ok := h.GinH["pagination"] if ok { diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 394105e..20ec641 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -2,6 +2,7 @@ package twentyseventeen import ( "fmt" + "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" @@ -44,17 +45,15 @@ func Hook(cHandle common.Handle) { h.Index() } -var plugin = func() []common.Plugin[models.Posts] { - return append(common.Plugins(), postThumbnail) +var pluginFns = func() map[string]common.Plugin[models.Posts] { + return maps.Merge(common.Plugins(), map[string]common.Plugin[models.Posts]{ + "twentyseventeen_postThumbnail": postThumbnail, + }) }() func (h handle) Index() { if h.Stats != plugins.Empty404 { - - h.GinH["posts"] = slice.Map( - h.GinH["posts"].([]models.Posts), - common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C))) - + h.ExecListPagePlugin(pluginFns) p, ok := h.GinH["pagination"] if ok { pp, ok := p.(pagination.ParsePagination)