From 12a1fea5ed91822d12dae7357ebd0a9594871501 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 12 Mar 2023 14:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/wp/archive.go | 2 +- internal/theme/wp/recentposts.go | 2 +- internal/theme/wp/search.go | 6 ++++- internal/theme/wp/widgetareadata.go | 2 +- internal/theme/wp/wp.go | 39 +++++++++++++++++++++-------- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/internal/theme/wp/archive.go b/internal/theme/wp/archive.go index 8af88b9..04ac07a 100644 --- a/internal/theme/wp/archive.go +++ b/internal/theme/wp/archive.go @@ -107,7 +107,7 @@ func archiveUl(h *Handle, conf map[any]any, args map[string]string, archives []m if showCount == 1 { count = fmt.Sprintf("(%v)", archive.Posts) } - s.Sprintf(`
  • %[1]s年%[2]s月%[3]s
  • `, archive.Year, archive.Month, count) + s.Sprintf(`
  • %[1]s年%[2]s月%[3]s
  • `, archive.Year, archive.Month, count) } return s.String() } diff --git a/internal/theme/wp/recentposts.go b/internal/theme/wp/recentposts.go index 4a1cacc..517192b 100644 --- a/internal/theme/wp/recentposts.go +++ b/internal/theme/wp/recentposts.go @@ -57,7 +57,7 @@ func RecentPosts(h *Handle) string { posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string { t = ProjectTitle(t) date := "" - if conf["show_date"].(bool) { + if v, ok := conf["show_date"].(bool); ok && v { date = fmt.Sprintf(`%s`, t.PostDateGmt.Format("2006年01月02日")) } ariaCurrent := "" diff --git a/internal/theme/wp/search.go b/internal/theme/wp/search.go index 74014ba..bf9593e 100644 --- a/internal/theme/wp/search.go +++ b/internal/theme/wp/search.go @@ -5,6 +5,7 @@ import ( "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/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints/components" "github.com/fthvgb1/wp-go/internal/wpconfig" "strings" @@ -51,7 +52,10 @@ func SearchForm(h *Handle) string { if args["{$title}"] != "" { args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"]) } - args["{$value}"] = html.SpecialChars(h.Index.Param.Search) + args["{$value}"] = "" + if h.Scene() == constraints.Search { + args["{$value}"] = html.SpecialChars(h.Index.Param.Search) + } form := html5SearchForm if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "search-form") { form = xmlSearchForm diff --git a/internal/theme/wp/widgetareadata.go b/internal/theme/wp/widgetareadata.go index ed8c27d..e2455e9 100644 --- a/internal/theme/wp/widgetareadata.go +++ b/internal/theme/wp/widgetareadata.go @@ -25,7 +25,7 @@ func (h *Handle) WidgetArea() { return nil, false }) h.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) { - h.PushGroupCacheComponentFn(constraints.SidebarsWidgets, constraints.SidebarsWidgets, 10, sidebar...) + h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...) }, 30)) h.ginH["categories"] = cache.CategoriesTags(h.C, constraints.Category) } diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index 5ae8942..d619e5a 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -41,7 +41,8 @@ type HandlePlugins map[string]HandleFn[*Handle] // Components Order 为执行顺序,降序执行 type Components struct { - str string + Str string + Fn func(*Handle) string Order int } @@ -54,8 +55,9 @@ type HandleCall struct { Order int } -func (h *Handle) ComponentFilterFn(name string) []func(*Handle, string) string { - return h.componentFilterFn[name] +func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string) string, bool) { + fn, ok := h.componentFilterFn[name] + return fn, ok } func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string) string) { @@ -184,7 +186,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle { } func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components { - return Components{str: h.CacheStr(name, fn), Order: order} + return Components{Str: h.CacheStr(name, fn), Order: order} } func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { @@ -213,7 +215,7 @@ func (h *Handle) PushHeadScript(fn ...Components) { h.PushComponents(constraints.HeadScript, fn...) } func (h *Handle) PushGroupHeadScript(order int, str ...string) { - h.PushGroupComponents(constraints.HeadScript, order, str...) + h.PushGroupComponentStrs(constraints.HeadScript, order, str...) } func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) { h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...) @@ -224,7 +226,7 @@ func (h *Handle) PushFooterScript(fn ...Components) { } func (h *Handle) PushGroupFooterScript(order int, fns ...string) { - h.PushGroupComponents(constraints.FooterScript, order, fns...) + h.PushGroupComponentStrs(constraints.FooterScript, order, fns...) } func (h *Handle) componentKey(name string) string { @@ -236,7 +238,7 @@ func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(* } func (h *Handle) PushGroupCacheComponentFn(name, key string, order int, fns ...func(*Handle) string) { v := reload.GetStrBy(key, "\n", h, fns...) - h.PushGroupComponents(name, order, v) + h.PushGroupComponentStrs(name, order, v) } func (h *Handle) GetPassword() { @@ -314,10 +316,24 @@ func (h *Handle) PushComponents(name string, components ...Components) { h.components[k] = append(h.components[k], components...) } -func (h *Handle) PushGroupComponents(name string, order int, fns ...string) { +func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) { var calls []Components for _, fn := range fns { - calls = append(calls, Components{fn, order}) + calls = append(calls, Components{ + Str: fn, + Order: order, + }) + } + k := h.componentKey(name) + h.components[k] = append(h.components[k], calls...) +} +func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) { + var calls []Components + for _, fn := range fns { + calls = append(calls, Components{ + Fn: fn, + Order: order, + }) } k := h.componentKey(name) h.components[k] = append(h.components[k], calls...) @@ -329,7 +345,10 @@ func (h *Handle) CalMultipleComponents() { return i.Order > j.Order }) v := strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) { - s := t.str + s := t.Str + if s == "" && t.Fn != nil { + s = t.Fn(h) + } return s, s != "" }), "\n") kk := strings.Split(k, "_")