From 4e84bf21a07721361afd36789bda1dd6940514c3 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 27 Jan 2023 00:52:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/slice/slice.go | 26 ++++ helper/slice/slice_test.go | 124 ++++++++++++++++++ internal/actions/index.go | 3 +- internal/plugins/plugins.go | 2 + .../theme/twentyseventeen/twentyseventeen.go | 19 ++- 5 files changed, 169 insertions(+), 5 deletions(-) diff --git a/helper/slice/slice.go b/helper/slice/slice.go index d45b4ce..8c7caff 100644 --- a/helper/slice/slice.go +++ b/helper/slice/slice.go @@ -20,6 +20,32 @@ func FilterAndMap[N any, T any](arr []T, fn func(T) (N, bool)) (r []N) { return } +func Walk[T any](arr []T, fn func(*T)) { + for i := 0; i < len(arr); i++ { + fn(&arr[i]) + } +} + +func First[T any](arr []T, fn func(T) bool) (int, T) { + for i, t := range arr { + if fn(t) { + return i, t + } + } + var r T + return -1, r +} + +func Last[T any](arr []T, fn func(T) bool) (int, T) { + for i := len(arr) - 1; i > 0; i-- { + if fn(arr[i]) { + return i, arr[i] + } + } + var r T + return -1, r +} + func Filter[T any](arr []T, fn func(T) bool) []T { var r []T for _, t := range arr { diff --git a/helper/slice/slice_test.go b/helper/slice/slice_test.go index 126e0f4..9cc3a86 100644 --- a/helper/slice/slice_test.go +++ b/helper/slice/slice_test.go @@ -1,6 +1,7 @@ package slice import ( + "fmt" "github.com/fthvgb1/wp-go/helper/number" "reflect" "testing" @@ -527,3 +528,126 @@ func TestToAnySlice(t *testing.T) { }) } } + +func TestFirst(t *testing.T) { + type args[T int] struct { + arr []T + fn func(T) bool + } + type testCase[T int] struct { + name string + args args[T] + want int + want1 T + } + tests := []testCase[int]{ + { + name: "t1", + args: args[int]{ + arr: number.Range(1, 10, 1), + fn: func(t int) bool { + return t == 5 + }, + }, + want: 4, + want1: 5, + }, { + name: "t2", + args: args[int]{ + arr: number.Range(1, 10, 1), + fn: func(t int) bool { + return t == 11 + }, + }, + want: -1, + want1: 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := First(tt.args.arr, tt.args.fn) + if got != tt.want { + t.Errorf("First() got = %v, want %v", got, tt.want) + } + if !reflect.DeepEqual(got1, tt.want1) { + t.Errorf("First() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} + +func TestLast(t *testing.T) { + type args[T int] struct { + arr []T + fn func(T) bool + } + type testCase[T int] struct { + name string + args args[T] + want int + want1 T + } + tests := []testCase[int]{ + { + name: "t1", + args: args[int]{ + arr: []int{1, 55, 5, 5, 5, 5, 22}, + fn: func(t int) bool { + return t == 5 + }, + }, + want: 5, + want1: 5, + }, { + name: "t2", + args: args[int]{ + arr: number.Range(1, 10, 1), + fn: func(t int) bool { + return t == 11 + }, + }, + want: -1, + want1: 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := Last(tt.args.arr, tt.args.fn) + if got != tt.want { + t.Errorf("Last() got = %v, want %v", got, tt.want) + } + if !reflect.DeepEqual(got1, tt.want1) { + t.Errorf("Last() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} + +func TestWalk(t *testing.T) { + type args[T int] struct { + arr []T + fn func(*T) + } + type testCase[T int] struct { + name string + args args[T] + } + tests := []testCase[int]{ + { + name: "t1", + args: args[int]{ + arr: number.Range(1, 10, 1), + fn: func(i *int) { + *i = *i * 2 + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fmt.Println(tt.args.arr) + Walk(tt.args.arr, tt.args.fn) + fmt.Println(tt.args.arr) + }) + } +} diff --git a/internal/actions/index.go b/internal/actions/index.go index 87aa0b1..a05ea4d 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -140,6 +140,7 @@ func (h *indexHandle) parseParams() (err error) { if category == "" { category = h.c.Param("tag") if category != "" { + h.scene = plugins.Tag allNames := cache.AllTagsNames(h.c) if _, ok := allNames[category]; !ok { return errors.New(str.Join("not exists tag ", category)) @@ -148,6 +149,7 @@ func (h *indexHandle) parseParams() (err error) { h.header = fmt.Sprintf("标签: %s", category) } } else { + h.scene = plugins.Category allNames := cache.AllCategoryNames(h.c) if _, ok := allNames[category]; !ok { return errors.New(str.Join("not exists category ", category)) @@ -189,7 +191,6 @@ func (h *indexHandle) parseParams() (err error) { "left join", "wp_terms d", "c.term_id=d.term_id", }) h.setTitleLR(category, wpconfig.Options.Value("blogname")) - h.scene = plugins.Category } s := h.c.Query("s") if s != "" && strings.Replace(s, " ", "", -1) != "" { diff --git a/internal/plugins/plugins.go b/internal/plugins/plugins.go index 664533c..13568c7 100644 --- a/internal/plugins/plugins.go +++ b/internal/plugins/plugins.go @@ -8,6 +8,7 @@ const ( Home = iota + 1 Archive Category + Tag Search Detail @@ -21,6 +22,7 @@ var IndexSceneMap = map[int]struct{}{ Home: {}, Archive: {}, Category: {}, + Tag: {}, Search: {}, } diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 44f3344..942c893 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -64,9 +64,19 @@ func Hook(status int, c *gin.Context, h gin.H, scene, stats int) { d = 0 } } else if scene == plugins.Category { - cate := slice.Filter(cache.Categories(c), func(my models.TermsMy) bool { - return my.Name == c.Param("category") - })[0] + cat := c.Param("category") + _, cate := slice.First(cache.Categories(c), func(my models.TermsMy) bool { + return my.Name == cat + }) + d = int(cate.Terms.TermId) + if cate.Slug[0] != '%' { + s = cate.Slug + } + } else if scene == plugins.Tag { + cat := c.Param("tag") + _, cate := slice.First(cache.Tags(c), func(my models.TermsMy) bool { + return my.Name == cat + }) d = int(cate.Terms.TermId) if cate.Slug[0] != '%' { s = cate.Slug @@ -175,7 +185,7 @@ func bodyClass(scene, d int, a ...any) string { } else { s = "search-no-results" } - } else if scene == plugins.Category { + } else if scene == plugins.Category || scene == plugins.Tag { s = fmt.Sprintf("category-%d %v", d, a[0]) } else if scene == plugins.Detail { s = fmt.Sprintf("postid-%d", d) @@ -184,6 +194,7 @@ func bodyClass(scene, d int, a ...any) string { plugins.Home: "home blog ", plugins.Archive: "archive date page-two-column", plugins.Category: str.Join("archive category page-two-column ", s), + plugins.Tag: str.Join("archive category page-two-column ", s), plugins.Search: str.Join("search ", s), plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s), }[scene]