From b69b01f27b00d9b849d909521138099687bde2a6 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 5 May 2023 20:45:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/cmd/route/route.go | 2 +- app/theme/twentyfifteen/twentyfifteen.go | 6 ++-- app/theme/twentyseventeen/twentyseventeen.go | 24 +++---------- app/theme/wp/detail.go | 8 +++-- app/theme/wp/index.go | 21 ++++++----- app/theme/wp/listpostplugins.go | 4 --- app/theme/wp/pipe.go | 5 ++- app/theme/wp/wp.go | 38 +++++++++++++++++--- 8 files changed, 65 insertions(+), 43 deletions(-) diff --git a/app/cmd/route/route.go b/app/cmd/route/route.go index ef51684..bf29651 100644 --- a/app/cmd/route/route.go +++ b/app/cmd/route/route.go @@ -77,7 +77,7 @@ func SetupRouter() *gin.Engine { r.GET("/p/:id/feed", actions.PostFeed) r.GET("/feed", actions.Feed) r.GET("/comments/feed", actions.CommentsFeed) - //r.NoRoute(actions.ThemeHook(constraints.NoRoute)) + r.NoRoute(actions.ThemeHook(constraints.NoRoute)) commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime) r.POST("/comment", commentMiddleWare, actions.PostComment) if c.Pprof != "" { diff --git a/app/theme/twentyfifteen/twentyfifteen.go b/app/theme/twentyfifteen/twentyfifteen.go index 03717af..4199a39 100644 --- a/app/theme/twentyfifteen/twentyfifteen.go +++ b/app/theme/twentyfifteen/twentyfifteen.go @@ -63,11 +63,11 @@ func configs(h *wp.Handle) { components.WidgetArea(h) wp.ReplyCommentJs(h) h.SetData("customHeader", customHeader(h)) - h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender")) + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(postThumb, 60, "postThumb")) - h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Details, 100, "wp.Details")) - h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.Indexs, 100, "wp.Indexs")) + h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100, "wp.Detail")) + wp.PushIndexHandler(constraints.PipeData, h, wp.NewHandleFn(wp.Index, 100, "wp.Index")) h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 80, "wp.PreCodeAndStats")) h.PushRender(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70, "wp.PreTemplate")) } diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 6e15f8c..f7b7276 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -71,10 +71,10 @@ func configs(h *wp.Handle) { wp.ReplyCommentJs(h) h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...)) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) - h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender")) + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10, "wp.DetailRender")) - h.PushDataHandler(constraints.Detail, wp.NewHandleFn(detail, 100, "detail")) - h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(index, 100, "index")) + h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100, "wp.Detail"), wp.NewHandleFn(postThumb, 90, "{theme}.postThumb")) + wp.PushIndexHandler(constraints.PipeData, h, wp.NewHandleFn(wp.Index, 100, "wp.Index")) h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90, "wp.PreCodeAndStats")) } @@ -97,24 +97,8 @@ func errorsHandle(h *wp.Handle) { } } -func index(h *wp.Handle) { - if h.Scene() == constraints.Detail { - return - } - i := h.Index - err := i.BuildIndexData(wp.NewIndexParams(i.C)) - if err != nil { - i.SetErr(err) - } - h.SetData("scene", h.Scene()) -} - -func detail(h *wp.Handle) { +func postThumb(h *wp.Handle) { d := h.Detail - err := d.BuildDetailData() - if err != nil { - d.SetErr(err) - } if d.Post.Thumbnail.Path != "" { img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail") img.Sizes = "100vw" diff --git a/app/theme/wp/detail.go b/app/theme/wp/detail.go index 4112ccc..35356f4 100644 --- a/app/theme/wp/detail.go +++ b/app/theme/wp/detail.go @@ -109,8 +109,12 @@ func DetailRender(h *Handle) { d.ginH["post"] = d.Post } -func Details(h *Handle) { - _ = h.Detail.BuildDetailData() +func Detail(h *Handle) { + err := h.Detail.BuildDetailData() + if err != nil { + h.Detail.SetErr(err) + } + h.SetData("scene", h.Scene()) } func ReplyCommentJs(h *Handle) { diff --git a/app/theme/wp/index.go b/app/theme/wp/index.go index e699fc5..70dbe50 100644 --- a/app/theme/wp/index.go +++ b/app/theme/wp/index.go @@ -43,6 +43,13 @@ func NewIndexHandle(handle *Handle) *IndexHandle { return &IndexHandle{Handle: handle} } +func PushIndexHandler(pipeScene string, h *Handle, call HandleCall) { + h.PushHandlers(pipeScene, call, constraints.Home, + constraints.Category, constraints.Search, constraints.Tag, + constraints.Archive, constraints.Author, + ) +} + func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { i.Param = parm switch i.scene { @@ -138,21 +145,19 @@ func (i *IndexHandle) ExecPostsPlugin() { } func IndexRender(h *Handle) { - if h.scene == constraints.Detail || h.Stats != constraints.Ok { - return - } i := h.Index i.ExecPostsPlugin() i.Pagination() i.ginH["posts"] = i.Posts } -func Indexs(h *Handle) { - if h.Scene() == constraints.Detail { - return - } +func Index(h *Handle) { i := h.Index - _ = i.BuildIndexData(NewIndexParams(i.C)) + err := i.BuildIndexData(NewIndexParams(i.C)) + if err != nil { + i.SetErr(err) + } + h.SetData("scene", h.Scene()) } func (i *IndexHandle) MarkSticky(posts *[]models.Posts) { diff --git a/app/theme/wp/listpostplugins.go b/app/theme/wp/listpostplugins.go index ad68d47..a4a5bef 100644 --- a/app/theme/wp/listpostplugins.go +++ b/app/theme/wp/listpostplugins.go @@ -1,15 +1,12 @@ package wp import ( - "errors" "github.com/fthvgb1/wp-go/app/pkg/config" - "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/plugins" "github.com/fthvgb1/wp-go/app/plugins/wpposts" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" - str "github.com/fthvgb1/wp-go/helper/strings" ) type PostsPlugin func(*Handle, *models.Posts) @@ -75,7 +72,6 @@ func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, * if ok { return v, true } - logs.IfError(errors.New(str.Join("插件", t, "不存在")), "") return nil, false }) } diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index f9d8ce3..cb9f11f 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -3,6 +3,7 @@ package wp import ( "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" + "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" ) @@ -102,7 +103,9 @@ func PipeKey(h *Handle, pipScene string) string { } func Run(h *Handle, conf func(*Handle)) { - InitHandle(conf, h) + if !helper.GetContextVal(h.C, "inited", false) { + 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)...) diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index b313dd5..b4e2ae2 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -1,7 +1,6 @@ package wp import ( - "fmt" "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/logs" @@ -39,6 +38,14 @@ type Handle struct { template *template.Template } +func (h *Handle) GinH() gin.H { + return h.ginH +} + +func (h *Handle) SetScene(scene string) { + h.scene = scene +} + func (h *Handle) Components() map[string]map[string][]Components[string] { return h.components } @@ -94,6 +101,7 @@ func InitHandle(fn func(*Handle), h *Handle) { fnMap = map[string]map[string]any{} fnHook = map[string]map[string]any{} fn(h) + h.C.Set("inited", true) inited = true return *h }) @@ -113,6 +121,7 @@ func InitHandle(fn func(*Handle), h *Handle) { h.componentHook = hh.componentHook h.componentsArgs = hh.componentsArgs h.componentFilterFn = hh.componentFilterFn + h.C.Set("inited", true) } func (h *Handle) Abort() { @@ -195,13 +204,35 @@ func PreCodeAndStats(h *Handle) { } } +var htmlContentType = []string{"text/html; charset=utf-8"} + +func (h *Handle) RenderHtml(t *template.Template, statsCode int, name string) { + header := h.C.Writer.Header() + if val := header["Content-Type"]; len(val) == 0 { + header["Content-Type"] = htmlContentType + } + h.C.Status(statsCode) + err := t.ExecuteTemplate(h.C.Writer, name, h.ginH) + h.Abort() + h.StopPipe() + if err != nil { + panic(err) + } +} + +func (h *Handle) PushHandlers(pipeScene string, call HandleCall, statsOrScene ...string) { + for _, s := range statsOrScene { + h.PushHandler(pipeScene, s, call) + } +} + func (h *Handle) CommonComponents() { h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss) h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10, "wp.CalComponents")) - h.PushRender(constraints.AllStats, NewHandleFn(RenderTemplate, 0, "wp.RenderTemplate")) + h.PushRender(constraints.AllStats, NewHandleFn(PreRenderTemplate, 0, "wp.PreRenderTemplate")) } -func RenderTemplate(h *Handle) { +func PreRenderTemplate(h *Handle) { h.C.HTML(h.Code, h.templ, h.ginH) h.StopPipe() } @@ -211,5 +242,4 @@ 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 ?") }