优化 hook pipe
This commit is contained in:
parent
3ba2c02db5
commit
4323c508ba
|
@ -17,6 +17,7 @@ const (
|
||||||
AllScene = "AllScene"
|
AllScene = "AllScene"
|
||||||
|
|
||||||
PipeData = "PipeData"
|
PipeData = "PipeData"
|
||||||
|
PipeMiddleware = "PipeMiddleware"
|
||||||
PipeRender = "PipeRender"
|
PipeRender = "PipeRender"
|
||||||
|
|
||||||
Defaults = "default"
|
Defaults = "default"
|
||||||
|
|
|
@ -4,12 +4,14 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"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/enlightjs"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin"
|
"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"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var plugins = wp.HandlePlugins{
|
var plugins = wp.HandlePlugins{
|
||||||
"enlightjs": enlightjs.EnlighterJS,
|
"enlightjs": enlightjs.EnlighterJS,
|
||||||
"hiddenLogin": hiddenlogin.HiddenLogin,
|
"hiddenLogin": hiddenlogin.HiddenLogin,
|
||||||
|
"test": tests.Tt,
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterPlugins(m wp.HandlePlugins) {
|
func RegisterPlugins(m wp.HandlePlugins) {
|
||||||
|
|
22
internal/plugins/wphandle/tests/tt.go
Normal file
22
internal/plugins/wphandle/tests/tt.go
Normal 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")
|
||||||
|
}))
|
||||||
|
}
|
|
@ -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) {
|
func Hook(h *wp.Handle) {
|
||||||
pipe(h)
|
wp.Run(h, configs)
|
||||||
}
|
|
||||||
|
|
||||||
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
|
||||||
wp.InitThemeArgAndConfig(configs, h)
|
|
||||||
h.GetPassword()
|
|
||||||
next(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) {
|
func configs(h *wp.Handle) {
|
||||||
|
wphandle.UsePlugins(h)
|
||||||
conf := config.GetConfig()
|
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"`)
|
||||||
})
|
})
|
||||||
|
wp.InitPipe(h)
|
||||||
|
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
|
||||||
|
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
|
||||||
|
|
||||||
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
|
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
|
||||||
wphandle.UsePlugins(h)
|
|
||||||
h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
|
h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
|
||||||
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
|
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
|
||||||
h.CommonComponents()
|
h.CommonComponents()
|
||||||
|
|
|
@ -42,18 +42,16 @@ var paginate = func() plugins.PageEle {
|
||||||
return p
|
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) {
|
func Hook(h *wp.Handle) {
|
||||||
pipe(h)
|
wp.Run(h, configs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) {
|
func configs(h *wp.Handle) {
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
wphandle.UsePlugins(h)
|
wphandle.UsePlugins(h)
|
||||||
|
wp.InitPipe(h)
|
||||||
|
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
|
||||||
|
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
|
||||||
h.PushComponentFilterFn("bodyClass", calClass)
|
h.PushComponentFilterFn("bodyClass", calClass)
|
||||||
h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
|
h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
|
||||||
components.WidgetArea(h)
|
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(index, 100, "index"))
|
||||||
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90, "wp.PreCodeAndStats"))
|
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="/">
|
var searchForm = `<form role="search" method="get" class="search-form" action="/">
|
||||||
<label for="search-form-1">
|
<label for="search-form-1">
|
||||||
|
@ -186,7 +179,7 @@ func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func calClass(h *wp.Handle, s string, a ...any) string {
|
func calClass(h *wp.Handle, s string, _ ...any) string {
|
||||||
class := strings.Split(s, " ")
|
class := strings.Split(s, " ")
|
||||||
themeMods := h.CommonThemeMods()
|
themeMods := h.CommonThemeMods()
|
||||||
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
|
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
|
||||||
|
|
|
@ -95,7 +95,7 @@ func (h *Handle) PostClass(posts models.Posts) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
if posts.PostPassword != "" {
|
if posts.PostPassword != "" {
|
||||||
if h.password != posts.PostPassword {
|
if h.GetPassword() != posts.PostPassword {
|
||||||
class = append(class, "post-password-required")
|
class = append(class, "post-password-required")
|
||||||
} else {
|
} else {
|
||||||
class = append(class, "post-password-projected")
|
class = append(class, "post-password-projected")
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"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/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,21 +34,14 @@ func archiveArgs() map[string]string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func archivesConfig() map[any]any {
|
var archivesConfig = map[any]any{
|
||||||
return map[any]any{
|
|
||||||
"count": int64(0),
|
"count": int64(0),
|
||||||
"dropdown": int64(0),
|
"dropdown": int64(0),
|
||||||
"title": "归档",
|
"title": "归档",
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func Archive(h *wp.Handle, id string) string {
|
func Archive(h *wp.Handle, id string) string {
|
||||||
conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any {
|
conf := configs(archivesConfig, "widget_archives", int64(2))
|
||||||
archivesConfig := archivesConfig()
|
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
|
|
||||||
return maps.FilterZeroMerge(archivesConfig, conf)
|
|
||||||
})
|
|
||||||
|
|
||||||
args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string {
|
||||||
archiveArgs := archiveArgs()
|
archiveArgs := archiveArgs()
|
||||||
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs())
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs())
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"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/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -24,15 +23,12 @@ var categoryTemplate = `{$before_widget}
|
||||||
{$navCloser}
|
{$navCloser}
|
||||||
{$after_widget}
|
{$after_widget}
|
||||||
`
|
`
|
||||||
|
var categoryConfig = map[any]any{
|
||||||
func categoryConfig() map[any]any {
|
|
||||||
return map[any]any{
|
|
||||||
"count": int64(0),
|
"count": int64(0),
|
||||||
"dropdown": int64(0),
|
"dropdown": int64(0),
|
||||||
"hierarchical": int64(0),
|
"hierarchical": int64(0),
|
||||||
"title": "分类",
|
"title": "分类",
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func categoryArgs() map[string]string {
|
func categoryArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
|
@ -50,12 +46,7 @@ func categoryArgs() map[string]string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Category(h *wp.Handle, id string) string {
|
func Category(h *wp.Handle, id string) string {
|
||||||
|
conf := configs(categoryConfig, "widget_categories", int64(2))
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string {
|
||||||
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, 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())
|
return h.ComponentFilterFnHook("wp_dropdown_cats", s.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
func IsCategory(h *wp.Handle) {
|
||||||
if h.Scene() != constraints.Home {
|
|
||||||
next(h)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
name, ok := parseDropdownCate(h)
|
name, ok := parseDropdownCate(h)
|
||||||
if !ok {
|
if ok {
|
||||||
next(h)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name))
|
h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name))
|
||||||
|
h.Abort()
|
||||||
|
h.StopPipe()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
|
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
|
||||||
|
|
|
@ -1,9 +1,22 @@
|
||||||
package widget
|
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 {
|
func Fn(id string, fn func(*wp.Handle, string) string) func(h *wp.Handle) string {
|
||||||
return func(h *wp.Handle) string {
|
return func(h *wp.Handle) string {
|
||||||
return fn(h, id)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -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...)
|
|
||||||
}
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"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/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,12 +24,10 @@ func recentCommentsArgs() map[string]string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recentCommentConf() map[any]any {
|
var recentCommentConf = map[any]any{
|
||||||
return map[any]any{
|
|
||||||
"number": int64(5),
|
"number": int64(5),
|
||||||
"title": "近期评论",
|
"title": "近期评论",
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var recentCommentsTemplate = `{$before_widget}
|
var recentCommentsTemplate = `{$before_widget}
|
||||||
{$nav}
|
{$nav}
|
||||||
|
@ -43,12 +40,8 @@ var recentCommentsTemplate = `{$before_widget}
|
||||||
`
|
`
|
||||||
|
|
||||||
func RecentComments(h *wp.Handle, id string) string {
|
func RecentComments(h *wp.Handle, id string) string {
|
||||||
conf := reload.GetAnyValBys("widget-recent-comment-conf", h, func(h *wp.Handle) map[any]any {
|
conf := configs(recentCommentConf, "widget_recent-comments", int64(2))
|
||||||
commentConf := recentCommentConf()
|
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", commentConf, int64(2))
|
|
||||||
conf = maps.FilterZeroMerge(commentConf, conf)
|
|
||||||
return conf
|
|
||||||
})
|
|
||||||
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
|
||||||
commentsArgs := recentCommentsArgs()
|
commentsArgs := recentCommentsArgs()
|
||||||
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
|
|
|
@ -63,7 +63,7 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
|
||||||
func (d *DetailHandle) PasswordProject() {
|
func (d *DetailHandle) PasswordProject() {
|
||||||
if d.Post.PostPassword != "" {
|
if d.Post.PostPassword != "" {
|
||||||
wpposts.PasswordProjectTitle(&d.Post)
|
wpposts.PasswordProjectTitle(&d.Post)
|
||||||
if d.password != d.Post.PostPassword {
|
if d.GetPassword() != d.Post.PostPassword {
|
||||||
wpposts.PasswdProjectContent(&d.Post)
|
wpposts.PasswdProjectContent(&d.Post)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ func (d *DetailHandle) RenderComment() {
|
||||||
}
|
}
|
||||||
ableComment := true
|
ableComment := true
|
||||||
if d.Post.CommentStatus != "open" ||
|
if d.Post.CommentStatus != "open" ||
|
||||||
(d.Post.PostPassword != "" && d.password != d.Post.PostPassword) {
|
(d.Post.PostPassword != "" && d.GetPassword() != d.Post.PostPassword) {
|
||||||
ableComment = false
|
ableComment = false
|
||||||
}
|
}
|
||||||
d.ginH["showComment"] = ableComment
|
d.ginH["showComment"] = ableComment
|
||||||
|
|
73
internal/theme/wp/fn.go
Normal file
73
internal/theme/wp/fn.go
Normal 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")
|
||||||
|
}
|
|
@ -32,7 +32,7 @@ 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.GetPassword() != post.PostPassword {
|
||||||
wpposts.PasswdProjectContent(r)
|
wpposts.PasswdProjectContent(r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,16 @@ import (
|
||||||
|
|
||||||
type HandlePipeFn[T any] func(HandleFn[T], T)
|
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 方便把功能写在其它包里
|
// HandlePipe 方便把功能写在其它包里
|
||||||
func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
|
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) {
|
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)
|
}, 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) {
|
func (h *Handle) PushHandler(pipScene string, scene string, fns ...HandleCall) {
|
||||||
if _, ok := h.handlers[pipScene]; !ok {
|
if _, ok := h.handlers[pipScene]; !ok {
|
||||||
h.handlers[pipScene] = make(map[string][]HandleCall)
|
h.handlers[pipScene] = make(map[string][]HandleCall)
|
||||||
|
@ -69,9 +101,48 @@ func PipeKey(h *Handle, pipScene string) string {
|
||||||
return h.ComponentFilterFnHook("pipeKey", key, pipScene)
|
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) {
|
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall) (handlers []HandleCall) {
|
||||||
handlers = append(handlers, dataHandlers[h.scene]...)
|
handlers = append(handlers, dataHandlers[h.scene]...)
|
||||||
handlers = append(handlers, dataHandlers[constraints.AllScene]...)
|
handlers = append(handlers, dataHandlers[constraints.AllScene]...)
|
||||||
|
handlers = h.PipeHandleHook("PipeDataHandle", handlers)
|
||||||
return
|
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[h.scene]...)
|
||||||
handlers = append(handlers, renders[constraints.AllStats]...)
|
handlers = append(handlers, renders[constraints.AllStats]...)
|
||||||
handlers = append(handlers, renders[constraints.AllScene]...)
|
handlers = append(handlers, renders[constraints.AllScene]...)
|
||||||
|
handlers = h.PipeHandleHook("PipeRender", handlers)
|
||||||
return
|
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)) {
|
func (h *Handle) HookHandle(pipeScene string, hook func(HandleCall) (HandleCall, bool)) {
|
||||||
h.handleHook[pipeScene] = append(h.handleHook[pipeScene], hook)
|
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)))
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package wp
|
package wp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper/maps"
|
"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"
|
||||||
|
@ -81,7 +82,7 @@ type HandleCall struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
func InitHandle(fn func(*Handle), h *Handle) {
|
||||||
var inited = false
|
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]map[string][]Components[string])
|
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.handlers = make(map[string]map[string][]HandleCall)
|
||||||
h.handleHook = make(map[string][]func(HandleCall) (HandleCall, bool))
|
h.handleHook = make(map[string][]func(HandleCall) (HandleCall, bool))
|
||||||
h.ginH = gin.H{}
|
h.ginH = gin.H{}
|
||||||
|
fnMap = map[string]map[string]any{}
|
||||||
|
fnHook = map[string]map[string]any{}
|
||||||
fn(h)
|
fn(h)
|
||||||
inited = true
|
inited = true
|
||||||
return *h
|
return *h
|
||||||
|
@ -131,10 +134,6 @@ func (h *Handle) SetErr(err error) {
|
||||||
h.err = err
|
h.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) Password() string {
|
|
||||||
return h.password
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) SetTempl(templ string) {
|
func (h *Handle) SetTempl(templ string) {
|
||||||
h.templ = templ
|
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")
|
pw := h.Session.Get("post_password")
|
||||||
if pw != nil {
|
if pw != nil {
|
||||||
h.password = pw.(string)
|
h.password = pw.(string)
|
||||||
}
|
}
|
||||||
|
return h.password
|
||||||
}
|
}
|
||||||
|
|
||||||
func PreTemplate(h *Handle) {
|
func PreTemplate(h *Handle) {
|
||||||
|
@ -208,5 +211,5 @@ func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NothingToDo(*Handle) {
|
func NothingToDo(*Handle) {
|
||||||
|
fmt.Println("hi guys,how did you came to here? Is something wrong happened ?")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user