重构列表文档页插件 优化日志及去掉一些无用代码

This commit is contained in:
xing 2023-04-09 21:51:12 +08:00
parent df7bd8c1c6
commit e505ee2e03
8 changed files with 76 additions and 137 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea .idea
wp-go.iml wp-go.iml
config.yaml config.yaml
err.log

View File

@ -12,8 +12,10 @@ import (
) )
var logs = safety.NewVar[*log.Logger](nil) var logs = safety.NewVar[*log.Logger](nil)
var logFile = safety.NewVar[*os.File](nil)
func InitLogger() error { func InitLogger() error {
preFD := logFile.Load()
l := &log.Logger{} l := &log.Logger{}
c := config.GetConfig() c := config.GetConfig()
if c.LogOutput == "" { if c.LogOutput == "" {
@ -31,6 +33,10 @@ func InitLogger() error {
return err return err
} }
out = file out = file
logFile.Store(file)
}
if preFD != nil {
_ = preFD.Close()
} }
logs.Store(l) logs.Store(l)
l.SetFlags(log.Ldate | log.Ltime) l.SetFlags(log.Ldate | log.Ltime)

View File

@ -7,6 +7,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle" "github.com/fthvgb1/wp-go/internal/plugins/wphandle"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components" "github.com/fthvgb1/wp-go/internal/theme/wp/components"
@ -62,12 +63,15 @@ func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
} }
func configs(h *wp.Handle) { func configs(h *wp.Handle) {
conf := config.GetConfig()
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string { h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
}) })
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) h.Index.SetPageEle(plugins.TwentyFifteenPagination())
wphandle.RegisterPlugins(h, conf.Plugins...)
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
h.CommonComponents() h.CommonComponents()
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20)) h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings" 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/config"
@ -50,7 +49,8 @@ func Hook(h *wp.Handle) {
} }
func configs(h *wp.Handle) { func configs(h *wp.Handle) {
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) conf := config.GetConfig()
wphandle.RegisterPlugins(h, conf.Plugins...)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h) components.WidgetArea(h)
@ -64,6 +64,8 @@ func configs(h *wp.Handle) {
}) })
h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreTemplate, errorsHandle) h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreTemplate, errorsHandle)
h.CommonComponents() h.CommonComponents()
h.Index.SetPageEle(paginate)
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10)) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
@ -85,12 +87,6 @@ var searchForm = `<form role="search" method="get" class="search-form" action="/
</button> </button>
</form>` </form>`
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
"twentyseventeen_postThumbnail": postThumbnail,
})
}()
func errorsHandle(h *wp.Handle) { func errorsHandle(h *wp.Handle) {
switch h.Stats { switch h.Stats {
case constraints.Error404, constraints.InternalErr, constraints.ParamError: case constraints.Error404, constraints.InternalErr, constraints.ParamError:
@ -120,8 +116,6 @@ func index(h *wp.Handle) {
i.SetErr(err) i.SetErr(err)
} }
h.SetData("scene", h.Scene()) h.SetData("scene", h.Scene())
i.SetPageEle(paginate)
i.SetPostsPlugins(listPostsPlugins)
} }
func detail(h *wp.Handle) { func detail(h *wp.Handle) {
@ -159,14 +153,13 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
} }
func postThumbnail(next wp.Fn[models.Posts], h *wp.Handle, t models.Posts) models.Posts { func postThumbnail(h *wp.Handle, posts *models.Posts) {
if t.Thumbnail.Path != "" { if posts.Thumbnail.Path != "" {
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" posts.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
if h.Scene() == constraints.Detail { if h.Scene() == constraints.Detail {
t.Thumbnail.Sizes = "100vw" posts.Thumbnail.Sizes = "100vw"
} }
} }
return next(t)
} }
var header = reload.Vars(models.PostThumbnail{}) var header = reload.Vars(models.PostThumbnail{})

View File

@ -100,21 +100,6 @@ func (d *DetailHandle) ContextPost() {
d.ginH["prev"] = prev d.ginH["prev"] = prev
} }
func (d *DetailHandle) Render() {
d.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
d.PasswordProject()
d.RenderComment()
d.ginH["post"] = d.Post
reply := ""
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
}
d.PushGroupFooterScript(10, reply)
}, 10))
d.Handle.Render()
}
func DetailRender(h *Handle) { func DetailRender(h *Handle) {
if h.Stats != constraints.Ok { if h.Stats != constraints.Ok {
return return

View File

@ -6,7 +6,6 @@ import (
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"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/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
@ -21,7 +20,15 @@ type IndexHandle struct {
Posts []models.Posts Posts []models.Posts
pageEle pagination.Elements pageEle pagination.Elements
TotalRows int TotalRows int
postsPlugins map[string]Plugin[models.Posts, *Handle] postsPlugin PostsPlugin
}
func (i *IndexHandle) ListPlugin() func(*Handle, *models.Posts) {
return i.postsPlugin
}
func (i *IndexHandle) SetListPlugin(listPlugin func(*Handle, *models.Posts)) {
i.postsPlugin = listPlugin
} }
func (i *IndexHandle) PageEle() pagination.Elements { func (i *IndexHandle) PageEle() pagination.Elements {
@ -32,14 +39,6 @@ func (i *IndexHandle) SetPageEle(pageEle pagination.Elements) {
i.pageEle = pageEle i.pageEle = pageEle
} }
func (i *IndexHandle) PostsPlugins() map[string]Plugin[models.Posts, *Handle] {
return i.postsPlugins
}
func (i *IndexHandle) SetPostsPlugins(postsPlugins map[string]Plugin[models.Posts, *Handle]) {
i.postsPlugins = postsPlugins
}
func NewIndexHandle(handle *Handle) *IndexHandle { func NewIndexHandle(handle *Handle) *IndexHandle {
return &IndexHandle{Handle: handle} return &IndexHandle{Handle: handle}
} }
@ -126,34 +125,16 @@ func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
} }
i.Posts = posts i.Posts = posts
i.TotalRows = totalRows i.TotalRows = totalRows
i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize) i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize)
return return
} }
func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) { func (i *IndexHandle) ExecPostsPlugin() {
if i.postsPlugin != nil {
pluginConf := config.GetConfig().ListPagePlugins for j := range i.Posts {
i.postsPlugin(i.Handle, &i.Posts[j])
postsPlugins := i.postsPlugins
if postsPlugins == nil {
postsPlugins = pluginFns
} }
plugin := GetListPostPlugins(pluginConf, postsPlugins)
i.Posts = slice.Map(i.Posts, PluginFn[models.Posts](plugin, i.Handle, Defaults(calls...)))
} }
func (i *IndexHandle) Render() {
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
i.ExecPostsPlugin()
i.Pagination()
i.ginH["posts"] = i.Posts
}, 10))
i.Handle.Render()
} }
func IndexRender(h *Handle) { func IndexRender(h *Handle) {

View File

@ -1,7 +1,6 @@
package wp package wp
import ( import (
"context"
"errors" "errors"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
@ -13,47 +12,53 @@ import (
"github.com/fthvgb1/wp-go/internal/plugins/wpposts" "github.com/fthvgb1/wp-go/internal/plugins/wpposts"
) )
type Fn[T any] func(T) T type PostsPlugin func(*Handle, *models.Posts)
type Plugin[T, H any] func(initialFn Fn[T], h H, t T) T
func PluginFn[T, H any](a []Plugin[T, H], h H, fn Fn[T]) Fn[T] { func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *models.Posts)) PostsPlugin {
return slice.ReverseReduce(a, func(next Plugin[T, H], fn Fn[T]) Fn[T] { return slice.ReverseReduce(calls, func(t func(PostsPlugin, *Handle, *models.Posts), r PostsPlugin) PostsPlugin {
return func(t T) T { return func(handle *Handle, posts *models.Posts) {
return next(fn, h, t) t(r, handle, posts)
} }
}, fn) }, initial)
} }
var pluginFns = map[string]Plugin[models.Posts, *Handle]{ var pluginFns = map[string]func(PostsPlugin, *Handle, *models.Posts){
"passwordProject": PasswordProject, "passwordProject": PasswordProject,
"digest": Digest, "digest": Digest,
} }
// PasswordProject 标题和内容密码保护 // PasswordProject 标题和内容密码保护
func PasswordProject(next Fn[models.Posts], h *Handle, post models.Posts) (r models.Posts) { func PasswordProject(next PostsPlugin, h *Handle, post *models.Posts) {
r = post r := post
if post.PostPassword != "" { if post.PostPassword != "" {
wpposts.PasswordProjectTitle(&r) wpposts.PasswordProjectTitle(r)
if h.password != post.PostPassword { if h.password != post.PostPassword {
wpposts.PasswdProjectContent(&r) wpposts.PasswdProjectContent(r)
return return
} }
} }
r = next(r) next(h, r)
return
} }
// Digest 生成摘要 注意放到最后,不继续往下执行 // Digest 生成摘要
func Digest(next Fn[models.Posts], h *Handle, post models.Posts) models.Posts { func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
if post.PostExcerpt != "" { if post.PostExcerpt != "" {
plugins.PostExcerpt(&post) plugins.PostExcerpt(post)
} else { } else {
plugins.Digest(h.C, &post, config.GetConfig().DigestWordCount) plugins.Digest(h.C, post, config.GetConfig().DigestWordCount)
} }
return post next(h, post)
} }
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] { func PostPlugin(calls ...func(h *Handle, posts *models.Posts)) PostsPlugin {
return func(h *Handle, posts *models.Posts) {
for _, call := range calls {
call(h, posts)
}
}
}
func ListPostPlugins() map[string]func(PostsPlugin, *Handle, *models.Posts) {
return maps.Copy(pluginFns) return maps.Copy(pluginFns)
} }
@ -64,8 +69,8 @@ func ProjectTitle(t models.Posts) models.Posts {
return t return t
} }
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] { func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, *models.Posts)) []func(PostsPlugin, *Handle, *models.Posts) {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) { return slice.FilterAndMap(name, func(t string) (func(PostsPlugin, *Handle, *models.Posts), bool) {
v, ok := m[t] v, ok := m[t]
if ok { if ok {
return v, true return v, true
@ -74,38 +79,3 @@ func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle
return nil, false 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
}
}

View File

@ -66,6 +66,7 @@ type HandleCall struct {
} }
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) { func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
var inited = false
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle { hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
h.components = make(map[string][]Components[string]) h.components = make(map[string][]Components[string])
h.handleFns = make(map[int][]HandleCall) h.handleFns = make(map[int][]HandleCall)
@ -73,6 +74,7 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string) h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
h.ginH = gin.H{} h.ginH = gin.H{}
fn(h) fn(h)
inited = true
return *h return *h
}) })
m := make(map[string][]Components[string]) m := make(map[string][]Components[string])
@ -82,10 +84,16 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
m[k] = vv m[k] = vv
} }
h.components = m h.components = m
h.ginH = maps.Copy(hh.ginH)
if inited {
return
}
h.Index.postsPlugin = hh.Index.postsPlugin
h.Index.pageEle = hh.Index.pageEle
h.Detail.CommentRender = hh.Detail.CommentRender
h.handleFns = hh.handleFns h.handleFns = hh.handleFns
h.componentsArgs = hh.componentsArgs h.componentsArgs = hh.componentsArgs
h.componentFilterFn = hh.componentFilterFn h.componentFilterFn = hh.componentFilterFn
h.ginH = maps.Copy(hh.ginH)
} }
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) { func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
@ -430,12 +438,3 @@ func ExecuteHandleFn(h *Handle) {
} }
} }
} }
func Render(h *Handle) {
switch h.scene {
case constraints.Detail:
h.Detail.Render()
default:
h.Index.Render()
}
}