优化 hook pipe

This commit is contained in:
xing 2023-05-03 23:57:49 +08:00
parent 3ba2c02db5
commit 4323c508ba
16 changed files with 253 additions and 97 deletions

View File

@ -17,6 +17,7 @@ const (
AllScene = "AllScene"
PipeData = "PipeData"
PipeMiddleware = "PipeMiddleware"
PipeRender = "PipeRender"
Defaults = "default"

View File

@ -4,12 +4,14 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/enlightjs"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/tests"
"github.com/fthvgb1/wp-go/internal/theme/wp"
)
var plugins = wp.HandlePlugins{
"enlightjs": enlightjs.EnlighterJS,
"hiddenLogin": hiddenlogin.HiddenLogin,
"test": tests.Tt,
}
func RegisterPlugins(m wp.HandlePlugins) {

View File

@ -0,0 +1,22 @@
package tests
import (
"fmt"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
)
func Tt(h *wp.Handle) {
h.HookHandle(constraints.PipeMiddleware, func(call wp.HandleCall) (wp.HandleCall, bool) {
return call, false
})
/*h.PushPipeHook(constraints.Home, func(pipe wp.Pipe) (wp.Pipe, bool) {
return wp.Pipe{}, false
})*/
//h.DeletePipe(constraints.Home, constraints.PipeMiddleware)
h.ReplacePipe(constraints.Home, constraints.PipeMiddleware, wp.NewPipe("log", 500, func(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
fmt.Println("ffff")
next(h)
fmt.Println("iiiii")
}))
}

View File

@ -41,28 +41,21 @@ func Init(fs embed.FS) {
}
}
var pipe = wp.HandlePipe(wp.NothingToDo, widget.MiddleWare(ready,
wp.PipeHandle(constraints.PipeData, wp.PipeKey, wp.PipeDataHandle),
wp.PipeHandle(constraints.PipeRender, wp.PipeKey, wp.PipeRender),
)...)
func Hook(h *wp.Handle) {
pipe(h)
}
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
wp.InitThemeArgAndConfig(configs, h)
h.GetPassword()
next(h)
wp.Run(h, configs)
}
func configs(h *wp.Handle) {
wphandle.UsePlugins(h)
conf := config.GetConfig()
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"`)
})
wp.InitPipe(h)
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
wphandle.UsePlugins(h)
h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
h.CommonComponents()

View File

@ -42,18 +42,16 @@ var paginate = func() plugins.PageEle {
return p
}()
var pipe = wp.HandlePipe(wp.NothingToDo, widget.MiddleWare(ready,
wp.PipeHandle(constraints.PipeData, wp.PipeKey, wp.PipeDataHandle),
wp.PipeHandle(constraints.PipeRender, wp.PipeKey, wp.PipeRender),
)...)
func Hook(h *wp.Handle) {
pipe(h)
wp.Run(h, configs)
}
func configs(h *wp.Handle) {
conf := config.GetConfig()
wphandle.UsePlugins(h)
wp.InitPipe(h)
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
h.PushComponentFilterFn("bodyClass", calClass)
h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h)
@ -79,11 +77,6 @@ func configs(h *wp.Handle) {
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(index, 100, "index"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90, "wp.PreCodeAndStats"))
}
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
wp.InitThemeArgAndConfig(configs, h)
h.GetPassword()
next(h)
}
var searchForm = `<form role="search" method="get" class="search-form" action="/">
<label for="search-form-1">
@ -186,7 +179,7 @@ func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
return
}
func calClass(h *wp.Handle, s string, a ...any) string {
func calClass(h *wp.Handle, s string, _ ...any) string {
class := strings.Split(s, " ")
themeMods := h.CommonThemeMods()
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)

View File

@ -95,7 +95,7 @@ func (h *Handle) PostClass(posts models.Posts) string {
}
if posts.PostPassword != "" {
if h.password != posts.PostPassword {
if h.GetPassword() != posts.PostPassword {
class = append(class, "post-password-required")
} else {
class = append(class, "post-password-projected")

View File

@ -10,7 +10,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"strings"
)
@ -35,21 +34,14 @@ func archiveArgs() map[string]string {
}
}
func archivesConfig() map[any]any {
return map[any]any{
var archivesConfig = map[any]any{
"count": int64(0),
"dropdown": int64(0),
"title": "归档",
}
}
func Archive(h *wp.Handle, id string) string {
conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any {
archivesConfig := archivesConfig()
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
return maps.FilterZeroMerge(archivesConfig, conf)
})
conf := configs(archivesConfig, "widget_archives", int64(2))
args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string {
archiveArgs := archiveArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs())

View File

@ -12,7 +12,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"net/http"
"strings"
)
@ -24,14 +23,11 @@ var categoryTemplate = `{$before_widget}
{$navCloser}
{$after_widget}
`
func categoryConfig() map[any]any {
return map[any]any{
var categoryConfig = map[any]any{
"count": int64(0),
"dropdown": int64(0),
"hierarchical": int64(0),
"title": "分类",
}
}
func categoryArgs() map[string]string {
@ -50,12 +46,7 @@ func categoryArgs() map[string]string {
}
func Category(h *wp.Handle, id string) string {
conf := reload.GetAnyValBys("widget-category-conf", h, func(a *wp.Handle) map[any]any {
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
conf = maps.FilterZeroMerge(categoryConfig(), conf)
return conf
})
conf := configs(categoryConfig, "widget_categories", int64(2))
args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string {
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
@ -244,17 +235,13 @@ func DropdownCategories(h *wp.Handle, args map[string]string, conf map[any]any,
return h.ComponentFilterFnHook("wp_dropdown_cats", s.String())
}
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
if h.Scene() != constraints.Home {
next(h)
return
}
func IsCategory(h *wp.Handle) {
name, ok := parseDropdownCate(h)
if !ok {
next(h)
return
}
if ok {
h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name))
h.Abort()
h.StopPipe()
}
}
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {

View File

@ -1,9 +1,22 @@
package widget
import "github.com/fthvgb1/wp-go/internal/theme/wp"
import (
"github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func Fn(id string, fn func(*wp.Handle, string) string) func(h *wp.Handle) string {
return func(h *wp.Handle) string {
return fn(h, id)
}
}
func configs[M ~map[K]V, K comparable, V any](m M, key string, a ...any) M {
return reload.GetAnyValBys(str.Join("widget-config-", key), key, func(_ string) M {
c := wpconfig.GetPHPArrayVal[M](key, nil, a...)
return maps.FilterZeroMerge(maps.Copy(m), c)
})
}

View File

@ -1,9 +0,0 @@
package widget
import "github.com/fthvgb1/wp-go/internal/theme/wp"
func MiddleWare(call ...wp.HandlePipeFn[*wp.Handle]) []wp.HandlePipeFn[*wp.Handle] {
return append([]wp.HandlePipeFn[*wp.Handle]{
IsCategory,
}, call...)
}

View File

@ -10,7 +10,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"strings"
)
@ -25,11 +24,9 @@ func recentCommentsArgs() map[string]string {
}
}
func recentCommentConf() map[any]any {
return map[any]any{
var recentCommentConf = map[any]any{
"number": int64(5),
"title": "近期评论",
}
}
var recentCommentsTemplate = `{$before_widget}
@ -43,12 +40,8 @@ var recentCommentsTemplate = `{$before_widget}
`
func RecentComments(h *wp.Handle, id string) string {
conf := reload.GetAnyValBys("widget-recent-comment-conf", h, func(h *wp.Handle) map[any]any {
commentConf := recentCommentConf()
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", commentConf, int64(2))
conf = maps.FilterZeroMerge(commentConf, conf)
return conf
})
conf := configs(recentCommentConf, "widget_recent-comments", int64(2))
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
commentsArgs := recentCommentsArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})

View File

@ -63,7 +63,7 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
func (d *DetailHandle) PasswordProject() {
if d.Post.PostPassword != "" {
wpposts.PasswordProjectTitle(&d.Post)
if d.password != d.Post.PostPassword {
if d.GetPassword() != d.Post.PostPassword {
wpposts.PasswdProjectContent(&d.Post)
}
}
@ -82,7 +82,7 @@ func (d *DetailHandle) RenderComment() {
}
ableComment := true
if d.Post.CommentStatus != "open" ||
(d.Post.PostPassword != "" && d.password != d.Post.PostPassword) {
(d.Post.PostPassword != "" && d.GetPassword() != d.Post.PostPassword) {
ableComment = false
}
d.ginH["showComment"] = ableComment

73
internal/theme/wp/fn.go Normal file
View File

@ -0,0 +1,73 @@
package wp
import (
"errors"
)
var fnMap map[string]map[string]any
var fnHook map[string]map[string]any
func GetFn[T any](fnType string, name string) []T {
v, ok := fnMap[fnType]
if !ok {
return nil
}
vv, ok := v[name]
if !ok {
return nil
}
return vv.([]T)
}
func GetFnHook[T any](fnType string, name string) []T {
v, ok := fnHook[fnType]
if !ok {
return nil
}
vv, ok := v[name]
if !ok {
return nil
}
return vv.([]T)
}
func PushFn[T any](fnType string, name string, fns ...T) error {
v, ok := fnMap[fnType]
if !ok {
v = make(map[string]any)
fnMap[fnType] = v
v[name] = fns
return nil
}
vv, ok := v[name]
if !ok || vv == nil {
v[name] = fns
return nil
}
s, ok := vv.([]T)
if ok {
s = append(s, fns...)
v[name] = s
}
return errors.New("error fn type")
}
func PushFnHook[T any](fnType string, name string, fns ...T) error {
v, ok := fnHook[fnType]
if !ok {
v = make(map[string]any)
fnHook[fnType] = v
v[name] = fns
return nil
}
vv, ok := v[name]
if !ok || vv == nil {
v[name] = fns
return nil
}
s, ok := vv.([]T)
if ok {
s = append(s, fns...)
v[name] = s
}
return errors.New("error fn type")
}

View File

@ -32,7 +32,7 @@ func PasswordProject(next PostsPlugin, h *Handle, post *models.Posts) {
r := post
if post.PostPassword != "" {
wpposts.PasswordProjectTitle(r)
if h.password != post.PostPassword {
if h.GetPassword() != post.PostPassword {
wpposts.PasswdProjectContent(r)
return
}

View File

@ -9,6 +9,16 @@ import (
type HandlePipeFn[T any] func(HandleFn[T], T)
type Pipe struct {
Name string
Order int
Fn HandlePipeFn[*Handle]
}
func NewPipe(name string, order int, fn HandlePipeFn[*Handle]) Pipe {
return Pipe{Name: name, Order: order, Fn: fn}
}
// HandlePipe 方便把功能写在其它包里
func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) {
@ -18,6 +28,28 @@ func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
}, initial)
}
func (h *Handle) PushPipe(scene string, pipes ...Pipe) error {
return PushFn("pipe", scene, pipes...)
}
func (h *Handle) PushPipeHook(scene string, pipes ...func(Pipe) (Pipe, bool)) error {
return PushFnHook("pipeHook", scene, pipes...)
}
func (h *Handle) DeletePipe(scene, pipeName string) error {
return PushFnHook("pipeHook", scene, func(pipe Pipe) (Pipe, bool) {
return pipe, pipeName != pipe.Name
})
}
func (h *Handle) ReplacePipe(scene, pipeName string, pipe Pipe) error {
return PushFnHook("pipeHook", scene, func(p Pipe) (Pipe, bool) {
if pipeName == p.Name {
p = pipe
}
return p, true
})
}
func (h *Handle) PushHandler(pipScene string, scene string, fns ...HandleCall) {
if _, ok := h.handlers[pipScene]; !ok {
h.handlers[pipScene] = make(map[string][]HandleCall)
@ -69,9 +101,48 @@ func PipeKey(h *Handle, pipScene string) string {
return h.ComponentFilterFnHook("pipeKey", key, pipScene)
}
func Run(h *Handle, conf func(*Handle)) {
InitHandle(conf, h)
reload.GetAnyValBys(str.Join("pipeInit-", h.scene), h, func(h *Handle) func(*Handle) {
p := GetFn[Pipe]("pipe", constraints.AllScene)
p = append(p, GetFn[Pipe]("pipe", h.scene)...)
pipes := slice.FilterAndMap(p, func(pipe Pipe) (Pipe, bool) {
var ok bool
hooks := GetFnHook[func(Pipe) (Pipe, bool)]("pipeHook", constraints.AllScene)
hooks = append(hooks, GetFnHook[func(Pipe) (Pipe, bool)]("pipeHook", h.scene)...)
for _, fn := range hooks {
pipe, ok = fn(pipe)
if !ok {
return pipe, false
}
}
return pipe, pipe.Fn != nil
})
slice.Sort(pipes, func(i, j Pipe) bool {
return i.Order > j.Order
})
arr := slice.Map(pipes, func(t Pipe) HandlePipeFn[*Handle] {
return t.Fn
})
return HandlePipe(NothingToDo, arr...)
})(h)
}
func MiddlewareKey(h *Handle, pipScene string) string {
return h.ComponentFilterFnHook("middleware", "middleware", pipScene)
}
func PipeMiddlewareHandle(h *Handle, middlewares map[string][]HandleCall) (handlers []HandleCall) {
handlers = append(handlers, middlewares[h.scene]...)
handlers = append(handlers, middlewares[constraints.AllScene]...)
handlers = h.PipeHandleHook("PipeMiddlewareHandle", handlers)
return
}
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall) (handlers []HandleCall) {
handlers = append(handlers, dataHandlers[h.scene]...)
handlers = append(handlers, dataHandlers[constraints.AllScene]...)
handlers = h.PipeHandleHook("PipeDataHandle", handlers)
return
}
@ -80,6 +151,7 @@ func PipeRender(h *Handle, renders map[string][]HandleCall) (handlers []HandleCa
handlers = append(handlers, renders[h.scene]...)
handlers = append(handlers, renders[constraints.AllStats]...)
handlers = append(handlers, renders[constraints.AllScene]...)
handlers = h.PipeHandleHook("PipeRender", handlers)
return
}
@ -104,3 +176,24 @@ func (h *Handle) ReplaceHandle(pipeScene, name string, fn HandleFn[*Handle]) {
func (h *Handle) HookHandle(pipeScene string, hook func(HandleCall) (HandleCall, bool)) {
h.handleHook[pipeScene] = append(h.handleHook[pipeScene], hook)
}
func (h *Handle) PushPipeHandleHook(name string, fn ...func([]HandleCall) []HandleCall) error {
return PushFnHook("pipeHandleHook", name, fn...)
}
func (h *Handle) PipeHandleHook(name string, calls []HandleCall) []HandleCall {
fn := GetFnHook[func(*Handle, []HandleCall) []HandleCall]("pipeHandleHook", name)
return slice.Reduce(fn, func(t func(*Handle, []HandleCall) []HandleCall, r []HandleCall) []HandleCall {
return t(h, r)
}, calls)
}
func InitPipe(h *Handle) {
h.PushPipe(constraints.Home, NewPipe(constraints.PipeMiddleware, 300,
PipeHandle(constraints.PipeMiddleware, MiddlewareKey, PipeMiddlewareHandle)))
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeData, 200,
PipeHandle(constraints.PipeData, PipeKey, PipeDataHandle)))
h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeRender, 100,
PipeHandle(constraints.PipeRender, PipeKey, PipeRender)))
}

View File

@ -1,6 +1,7 @@
package wp
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
@ -81,7 +82,7 @@ type HandleCall struct {
Name string
}
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
func InitHandle(fn func(*Handle), h *Handle) {
var inited = false
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
h.components = make(map[string]map[string][]Components[string])
@ -90,6 +91,8 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
h.handlers = make(map[string]map[string][]HandleCall)
h.handleHook = make(map[string][]func(HandleCall) (HandleCall, bool))
h.ginH = gin.H{}
fnMap = map[string]map[string]any{}
fnHook = map[string]map[string]any{}
fn(h)
inited = true
return *h
@ -131,10 +134,6 @@ func (h *Handle) SetErr(err error) {
h.err = err
}
func (h *Handle) Password() string {
return h.password
}
func (h *Handle) SetTempl(templ string) {
h.templ = templ
}
@ -163,11 +162,15 @@ func NewHandle(c *gin.Context, scene string, theme string) *Handle {
}
}
func (h *Handle) GetPassword() {
func (h *Handle) GetPassword() string {
if h.password != "" {
return h.password
}
pw := h.Session.Get("post_password")
if pw != nil {
h.password = pw.(string)
}
return h.password
}
func PreTemplate(h *Handle) {
@ -208,5 +211,5 @@ func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
}
func NothingToDo(*Handle) {
fmt.Println("hi guys,how did you came to here? Is something wrong happened ?")
}