From 0d62ebd5a5e553184a39fe9bda22321ca35753c8 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 2 Mar 2023 20:36:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=8C=E5=96=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/slice/sort.go | 15 +---- helper/slice/sort_test.go | 67 ++++--------------- internal/plugins/comment.go | 2 +- .../theme/twentyseventeen/twentyseventeen.go | 27 +++++--- internal/theme/wp/detail.go | 1 - internal/theme/wp/wp.go | 43 +++++++++--- stream/stream.go | 2 +- 7 files changed, 68 insertions(+), 89 deletions(-) diff --git a/helper/slice/sort.go b/helper/slice/sort.go index 63ea03f..2fa0c41 100644 --- a/helper/slice/sort.go +++ b/helper/slice/sort.go @@ -27,7 +27,8 @@ func (r anyArr[T]) Less(i, j int) bool { return r.fn(r.data[i], r.data[j]) } -func SortSelf[T any](arr []T, fn func(i, j T) bool) { +// Sort fn 中i>j 为降序,反之为升序 +func Sort[T any](arr []T, fn func(i, j T) bool) { slice := anyArr[T]{ data: arr, fn: fn, @@ -36,18 +37,6 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) { return } -// Sort fn 中i>j 为降序,反之为升序 -func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) { - r = make([]T, len(arr)) - copy(r, arr) - slice := anyArr[T]{ - data: r, - fn: fn, - } - sort.Sort(slice) - return -} - func Sorts[T constraints.Ordered](a []T, order int) { slice := anyArr[T]{ data: a, diff --git a/helper/slice/sort_test.go b/helper/slice/sort_test.go index 7e0f46c..18a58f6 100644 --- a/helper/slice/sort_test.go +++ b/helper/slice/sort_test.go @@ -7,59 +7,6 @@ import ( "testing" ) -func TestSortSelf(t *testing.T) { - type xy struct { - x int - y int - } - - type args struct { - arr []xy - fn func(i, j xy) bool - } - tests := []struct { - name string - args args - wantR []xy - }{ - { - name: "t1", - args: args{ - arr: []xy{ - {1, 2}, - {3, 4}, - {1, 3}, - {2, 1}, - {1, 6}, - }, - fn: func(i, j xy) bool { - if i.x < j.x { - return true - } - if i.x == j.x && i.y > i.y { - return true - } - return false - }, - }, - wantR: []xy{ - {1, 2}, - {1, 3}, - {1, 6}, - {2, 1}, - {3, 4}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if SortSelf(tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) { - t.Errorf("SimpleSort() = %v, want %v", tt.args.arr, tt.wantR) - } - }) - } -} - func TestSort(t *testing.T) { type xy struct { x int @@ -103,11 +50,21 @@ func TestSort(t *testing.T) { {3, 4}, }, }, + { + name: "t1", + args: args[xy]{ + arr: []xy{{1, 2}, {1, 3}, {1, 6}}, + fn: func(i, j xy) bool { + return i.x > j.x + }, + }, + wantR: []xy{{1, 2}, {1, 3}, {1, 6}}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotR := Sort[xy](tt.args.arr, tt.args.fn); !reflect.DeepEqual(gotR, tt.wantR) { - t.Errorf("SimpleSortR() = %v, want %v", gotR, tt.wantR) + if Sort[xy](tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) { + t.Errorf("SimpleSortR() = %v, want %v", tt.args.arr, tt.wantR) } }) } diff --git a/internal/plugins/comment.go b/internal/plugins/comment.go index 72c689e..a315575 100644 --- a/internal/plugins/comment.go +++ b/internal/plugins/comment.go @@ -55,7 +55,7 @@ func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html st if d.depth > d.maxDepth { comments = d.findComments(comments) } - slice.SortSelf(comments, d.i.Sort) + slice.Sort(comments, d.i.Sort) for i, comment := range comments { eo := "even" if (i+1)%2 == 0 { diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index b99142e..148d5bd 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -9,6 +9,7 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/constraints" + "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/theme/wp" @@ -47,10 +48,12 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { h.WidgetAreaData() h.GetPassword() h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 15)) - h.PushHeadScript( - wp.NewComponents(colorScheme, 10), - wp.NewComponents(customHeader, 10), - ) + errHandle := wp.NewHandleFn(errorsHandle, 100) + h.PushHandleFn(constraints.Error404, errHandle) + h.PushHandleFn(constraints.ParamError, errHandle) + h.PushHandleFn(constraints.InternalErr, errHandle) + h.PushGroupHeadScript(10, colorScheme, customHeader) + if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") { h.PushHeadScript(wp.NewComponents(func(h *wp.Handle) string { return ` ` @@ -76,12 +79,18 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] { }) }() +func errorsHandle(h *wp.Handle) { + h.SetTempl("twentyseventeen/posts/error.gohtml") + switch h.Stats { + case constraints.Error404, constraints.InternalErr: + logs.ErrPrintln(h.Err(), "报错:") + } +} + func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) { err := i.BuildIndexData(wp.NewIndexParams(i.C)) if err != nil { - i.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) - i.Render() - return + i.SetErr(err) } i.SetPageEle(paginate) i.SetPostsPlugins(listPostsPlugins) @@ -91,9 +100,7 @@ func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) { func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) { err := d.BuildDetailData() if err != nil { - d.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) - d.Render() - return + d.SetErr(err) } if d.Post.Thumbnail.Path != "" { img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail") diff --git a/internal/theme/wp/detail.go b/internal/theme/wp/detail.go index 81a3fa1..4d44c55 100644 --- a/internal/theme/wp/detail.go +++ b/internal/theme/wp/detail.go @@ -41,7 +41,6 @@ func (d *DetailHandle) CheckAndGetPost() (err error) { if id > maxId || id <= 0 { d.Stats = constraints.ParamError err = errors.New("无效的文档id") - d.class = append(d.class, "error404") } if err != nil { return diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index 1623614..5177e64 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -108,8 +108,16 @@ func NewComponents(fn func(*Handle) string, order int) Components { return Components{Fn: fn, Order: order} } -func (h *Handle) PushHandleFn(stats int, fns ...HandleCall) { - h.handleFns[stats] = append(h.handleFns[stats], fns...) +func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { + h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], fns...) +} + +func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Handle]) { + var calls []HandleCall + for _, fn := range fns { + calls = append(calls, HandleCall{fn, order}) + } + h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...) } func (h *Handle) AddComponent(name string, fn func(*Handle) string) { @@ -122,10 +130,17 @@ func (h *Handle) AddComponent(name string, fn func(*Handle) string) { } func (h *Handle) PushHeadScript(fn ...Components) { - h.components[constraints.HeadScript] = append(h.components[constraints.HeadScript], fn...) + h.PushComponents(constraints.HeadScript, fn...) +} +func (h *Handle) PushGroupHeadScript(order int, fns ...func(*Handle) string) { + h.PushGroupComponents(constraints.HeadScript, order, fns...) } func (h *Handle) PushFooterScript(fn ...Components) { - h.components[constraints.FooterScript] = append(h.components[constraints.FooterScript], fn...) + h.PushComponents(constraints.FooterScript, fn...) +} + +func (h *Handle) PushGroupFooterScript(order int, fns ...func(*Handle) string) { + h.PushGroupComponents(constraints.FooterScript, order, fns...) } func (h *Handle) GetPassword() { @@ -141,11 +156,15 @@ func (h *Handle) ExecHandleFns() { if ok { fns = append(fns, calls...) } - call, ok := h.handleFns[constraints.AllStats] + calls, ok = h.handleFns[h.scene] if ok { - fns = append(fns, call...) + fns = append(fns, calls...) } - slice.SortSelf(fns, func(i, j HandleCall) bool { + calls, ok = h.handleFns[constraints.AllStats] + if ok { + fns = append(fns, calls...) + } + slice.Sort(fns, func(i, j HandleCall) bool { return i.Order > j.Order }) for _, fn := range fns { @@ -200,11 +219,19 @@ func (h *Handle) PushComponents(name string, components ...Components) { h.components[name] = append(h.components[name], components...) } +func (h *Handle) PushGroupComponents(name string, order int, fns ...func(*Handle) string) { + var calls []Components + for _, fn := range fns { + calls = append(calls, Components{fn, order}) + } + h.components[name] = append(h.components[name], calls...) +} + func (h *Handle) CalMultipleComponents() { for k, ss := range h.components { v, ok := reload.GetStr(k) if !ok { - slice.SortSelf(ss, func(i, j Components) bool { + slice.Sort(ss, func(i, j Components) bool { return i.Order > j.Order }) v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) { diff --git a/stream/stream.go b/stream/stream.go index 0fb30c7..9de04a1 100644 --- a/stream/stream.go +++ b/stream/stream.go @@ -104,7 +104,7 @@ func (r Stream[T]) Reduce(fn func(v, r T) T, init T) T { } func (r Stream[T]) Sort(fn func(i, j T) bool) Stream[T] { - slice.SortSelf(r.arr, fn) + slice.Sort(r.arr, fn) return r }