diff --git a/helper/html/html.go b/helper/html/html.go index 2131799..21c31c9 100644 --- a/helper/html/html.go +++ b/helper/html/html.go @@ -31,7 +31,11 @@ const ( EntSpace = 8 ) -func htmlSpecialChars(text string, flags int) string { +func SpecialChars(text string, flag ...int) string { + flags := EntQuotes + if len(flag) > 0 { + flags = flag[0] + } r, ok := unEntitlesMap[flags] e := entitlesMap[flags] if !ok { @@ -48,7 +52,11 @@ func htmlSpecialChars(text string, flags int) string { } return text } -func htmlSpecialCharsDecode(text string, flags int) string { +func SpecialCharsDecode(text string, flag ...int) string { + flags := EntQuotes + if len(flag) > 0 { + flags = flag[0] + } r, ok := entitlesMap[flags] u := unEntitlesMap[flags] if !ok { diff --git a/helper/html/html_test.go b/helper/html/html_test.go index 4dc1ddc..4008395 100644 --- a/helper/html/html_test.go +++ b/helper/html/html_test.go @@ -32,8 +32,8 @@ func Test_htmlSpecialChars(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := htmlSpecialChars(tt.args.text, tt.args.flags); got != tt.want { - t.Errorf("htmlSpecialChars() = %v, want %v", got, tt.want) + if got := SpecialChars(tt.args.text, tt.args.flags); got != tt.want { + t.Errorf("SpecialChars() = %v, want %v", got, tt.want) } }) } @@ -88,8 +88,8 @@ func Test_htmlSpecialCharsDecode(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := htmlSpecialCharsDecode(tt.args.text, tt.args.flags); got != tt.want { - t.Errorf("htmlSpecialCharsDecode() = %v, want %v", got, tt.want) + if got := SpecialCharsDecode(tt.args.text, tt.args.flags); got != tt.want { + t.Errorf("SpecialCharsDecode() = %v, want %v", got, tt.want) } }) } diff --git a/helper/slice/set.go b/helper/slice/set.go index c3b6972..29536fe 100644 --- a/helper/slice/set.go +++ b/helper/slice/set.go @@ -1,6 +1,6 @@ package slice -func IsContained[T comparable](a T, arr []T) bool { +func IsContained[T comparable](arr []T, a T) bool { for _, v := range arr { if a == v { return true @@ -23,7 +23,7 @@ func Diff[T comparable](a []T, b ...[]T) (r []T) { for _, t := range a { f := false for _, ts := range b { - if IsContained(t, ts) { + if IsContained(ts, t) { f = true break } @@ -74,7 +74,7 @@ func Intersect[T comparable](a []T, b ...[]T) (r []T) { for _, t := range a { f := false for _, ts := range b { - if !IsContained(t, ts) { + if !IsContained(ts, t) { f = true break } diff --git a/internal/actions/themehook.go b/internal/actions/themehook.go index fcf2bb6..89e9c98 100644 --- a/internal/actions/themehook.go +++ b/internal/actions/themehook.go @@ -8,7 +8,7 @@ import ( func ThemeHook(scene int) func(*gin.Context) { return func(ctx *gin.Context) { - t := theme.GetTemplateName() + t := theme.GetCurrentTemplateName() h := wp.NewHandle(ctx, scene, t) h.Index = wp.NewIndexHandle(h) h.Detail = wp.NewDetailHandle(h) diff --git a/internal/cmd/route/route.go b/internal/cmd/route/route.go index b94f08b..5e0833c 100644 --- a/internal/cmd/route/route.go +++ b/internal/cmd/route/route.go @@ -29,7 +29,7 @@ func SetupRouter() *gin.Engine { } } - r.HTMLRender = theme.GetTemplate() + r.HTMLRender = theme.Template() wpconfig.SetTemplateFs(theme.TemplateFs) siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) r.Use( diff --git a/internal/theme/fs.go b/internal/theme/fs.go index bae4ddb..2563a58 100644 --- a/internal/theme/fs.go +++ b/internal/theme/fs.go @@ -14,7 +14,7 @@ var TemplateFs embed.FS var templates map[string]*template.Template //方便外部获取模板render后的字符串,不然在gin中获取不了 -func GetTemplate() *multipTemplate.MultipleFsTemplate { +func Template() *multipTemplate.MultipleFsTemplate { t := multipTemplate.NewFsTemplate(TemplateFs) templates = t.Template t.FuncMap = FuncMap() @@ -24,6 +24,11 @@ func GetTemplate() *multipTemplate.MultipleFsTemplate { return t } +func GetTemplate(name string) (*template.Template, bool) { + t, ok := templates[name] + return t, ok +} + // 所有主题模板通用设置 func commonTemplate(t *multipTemplate.MultipleFsTemplate) { m, err := fs.Glob(t.Fs, "*/posts/*.gohtml") diff --git a/internal/theme/theme.go b/internal/theme/theme.go index bb57dc7..d4dce68 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -14,7 +14,7 @@ func InitTheme() { twentyseventeen.Init(TemplateFs) } -func GetTemplateName() string { +func GetCurrentTemplateName() string { tmlp := config.GetConfig().Theme if tmlp == "" { tmlp = wpconfig.GetOption("template") diff --git a/internal/theme/wp/components/constraints.go b/internal/theme/wp/components/constraints.go new file mode 100644 index 0000000..801e7c4 --- /dev/null +++ b/internal/theme/wp/components/constraints.go @@ -0,0 +1,5 @@ +package components + +const ( + SearchFormArgs = "SearchFormArgs" +) diff --git a/internal/theme/wp/index.go b/internal/theme/wp/index.go index 62cd71e..20b5f00 100644 --- a/internal/theme/wp/index.go +++ b/internal/theme/wp/index.go @@ -48,6 +48,10 @@ func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { i.Param = parm switch i.scene { case constraints.Home, constraints.Search: + s := i.C.Query("s") + if s != "" && strings.Replace(s, " ", "", -1) != "" { + i.scene = constraints.Search + } i.Param.ParseSearch() case constraints.Category: err = i.Param.ParseCategory() diff --git a/internal/theme/wp/indexparams.go b/internal/theme/wp/indexparams.go index 39a4a23..c6f11fe 100644 --- a/internal/theme/wp/indexparams.go +++ b/internal/theme/wp/indexparams.go @@ -105,7 +105,7 @@ func NewIndexParams(ctx *gin.Context) *IndexParams { func (i *IndexParams) parseSearch() { s := i.Ctx.Query("s") - if s != "" && strings.Replace(s, " ", "", -1) != "" { + if s != "" { q := str.Join("%", s, "%") i.Where = append(i.Where, []string{ "and", "post_title", "like", q, "", diff --git a/internal/theme/wp/search.go b/internal/theme/wp/search.go new file mode 100644 index 0000000..10c266d --- /dev/null +++ b/internal/theme/wp/search.go @@ -0,0 +1,60 @@ +package wp + +import ( + "github.com/fthvgb1/wp-go/helper/html" + "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/theme/wp/components" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "strings" +) + +var searchTemplate = `{$before_widget} +{$title} +{$form} +{$after_widget}` + +var searchArgs = map[string]string{ + "{$before_widget}": `