From 55a9e90ec9aba218f40097af0517d1ff1fd0f755 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 9 Feb 2023 17:12:33 +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 --- helper/slice/slices.go | 8 +++ helper/slice/slices_test.go | 54 +++++++++++++++++++ internal/theme/common/common.go | 7 ++- internal/theme/hook.go | 18 +------ internal/theme/theme.go | 2 + .../theme/twentyseventeen/twentyseventeen.go | 11 ++-- 6 files changed, 77 insertions(+), 23 deletions(-) diff --git a/helper/slice/slices.go b/helper/slice/slices.go index ab07083..e0056bf 100644 --- a/helper/slice/slices.go +++ b/helper/slice/slices.go @@ -63,3 +63,11 @@ func Copy[T any](a []T) []T { copy(dst, a) return dst } + +func Unshift[T any](a *[]T, e ...T) { + *a = append(e, *a...) +} + +func Push[T any](a *[]T, e ...T) { + *a = append(*a, e...) +} diff --git a/helper/slice/slices_test.go b/helper/slice/slices_test.go index 0d2cfaa..d582da1 100644 --- a/helper/slice/slices_test.go +++ b/helper/slice/slices_test.go @@ -165,3 +165,57 @@ func TestCopy(t *testing.T) { }) } } + +func TestUnshift(t *testing.T) { + type args[T int] struct { + a *[]T + e []T + } + type testCase[T int] struct { + name string + args args[T] + } + a := number.Range(1, 10, 1) + tests := []testCase[int]{ + { + name: "t1", + args: args[int]{ + a: &a, + e: number.Range(11, 15, 1), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + Unshift(tt.args.a, tt.args.e...) + fmt.Println(tt.args.a) + }) + } +} + +func TestPush(t *testing.T) { + type args[T int] struct { + a *[]T + e []T + } + type testCase[T int] struct { + name string + args args[T] + } + a := number.Range(1, 10, 1) + tests := []testCase[int]{ + { + name: "t1", + args: args[int]{ + a: &a, + e: number.Range(11, 15, 1), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + Push(tt.args.a, tt.args.e...) + fmt.Println(tt.args.a) + }) + } +} diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index eb321e4..62feaec 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -78,13 +78,18 @@ func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts { return next(post) } -func Digests(ctx context.Context) Fn[models.Posts] { +func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] { return func(post models.Posts) models.Posts { if post.PostExcerpt != "" { plugins.PostExcerpt(&post) } else { plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount) } + if len(calls) > 0 { + for _, call := range calls { + call(&post) + } + } return post } } diff --git a/internal/theme/hook.go b/internal/theme/hook.go index bb010ae..5795881 100644 --- a/internal/theme/hook.go +++ b/internal/theme/hook.go @@ -2,6 +2,7 @@ package theme import ( "github.com/fthvgb1/wp-go/internal/theme/common" + "github.com/fthvgb1/wp-go/internal/theme/twentyfifteen" ) var themeMap = map[string]func(handle common.Handle){} @@ -19,20 +20,5 @@ func Hook(themeName string, handle common.Handle) { fn(handle) return } - /*if _, ok := plugins.IndexSceneMap[scene]; ok { - p, ok := h["pagination"] - if ok { - pp, ok := p.(pagination.ParsePagination) - if ok { - h["pagination"] = pagination.Paginate(plugins.TwentyFifteenPagination(), pp) - } - } - c.HTML(code, "twentyfifteen/posts/index.gohtml", h) - return - } else if scene == plugins.Detail { - h["comments"] = plugins.FormatComments(c, plugins.CommentRender(), h["comments"].([]models.Comments), h["maxDep"].(int)) - c.HTML(code, "twentyfifteen/posts/detail.gohtml", h) - return - } - logs.ErrPrintln(errors.New("what happening"), " how reached here", themeName, code, h, scene, status)*/ + themeMap[twentyfifteen.ThemeName](handle) } diff --git a/internal/theme/theme.go b/internal/theme/theme.go index f38d3fb..ea6236b 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -2,11 +2,13 @@ package theme import ( "github.com/fthvgb1/wp-go/internal/pkg/config" + "github.com/fthvgb1/wp-go/internal/theme/twentyfifteen" "github.com/fthvgb1/wp-go/internal/theme/twentyseventeen" "github.com/fthvgb1/wp-go/internal/wpconfig" ) func InitThemeAndTemplateFuncMap() { + addThemeHookFunc(twentyfifteen.ThemeName, twentyfifteen.Hook) addThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook) } diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 4186b93..c0d1791 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -31,9 +31,9 @@ type handle struct { templ string } -func Hook(h2 common.Handle) { +func Hook(cHandle common.Handle) { h := handle{ - Handle: h2, + Handle: cHandle, templ: "twentyseventeen/posts/index.gohtml", } h.GinH["HeaderImage"] = h.getHeaderImage(h.C) @@ -57,7 +57,7 @@ func (h handle) Index() { h.GinH["posts"] = slice.Map( h.GinH["posts"].([]models.Posts), - common.PluginFn[models.Posts](plugin, h.Handle, common.Digests(h.C))) + common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C))) p, ok := h.GinH["pagination"] if ok { @@ -111,9 +111,8 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls func postThumbnail(next common.Fn[models.Posts], h common.Handle, t models.Posts) models.Posts { if t.Thumbnail.Path != "" { - if slice.IsContained(h.Scene, []int{plugins.Home, plugins.Archive, plugins.Search}) { - t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" - } else { + t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" + if h.Scene == plugins.Detail { t.Thumbnail.Sizes = "100vw" } }