From 72a37d3bc896bc00befa54affd8617deae437a7f Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 14 Feb 2023 23:28:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/common/detail.go | 26 ++++++++++++++ internal/theme/common/index.go | 26 ++++++++++++++ internal/theme/common/listpostplugins.go | 4 +-- internal/theme/twentyfifteen/twentyfifteen.go | 34 ++----------------- plugin/pagination/pagination.go | 7 ++-- 5 files changed, 59 insertions(+), 38 deletions(-) diff --git a/internal/theme/common/detail.go b/internal/theme/common/detail.go index 777f713..73f3710 100644 --- a/internal/theme/common/detail.go +++ b/internal/theme/common/detail.go @@ -4,10 +4,12 @@ import ( "fmt" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" + "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/wpconfig" + "net/http" ) type DetailHandle struct { @@ -89,3 +91,27 @@ func (d *DetailHandle) ContextPost() { d.GinH["next"] = next d.GinH["prev"] = prev } + +func (d *DetailHandle) Render() { + d.PasswordProject() + if d.CommentRender == nil { + d.CommentRender = plugins.CommentRender() + } + d.RenderComment() + d.CalBodyClass() + if d.Templ == "" { + d.Templ = fmt.Sprintf("%s/posts/detail.gohtml", d.Theme) + } + d.C.HTML(d.Code, d.Templ, d.GinH) +} + +func (d *DetailHandle) Details() { + err := d.BuildDetailData() + if err != nil { + d.Stats = constraints.Error404 + d.Code = http.StatusNotFound + d.C.HTML(d.Code, d.Templ, d.GinH) + return + } + d.Render() +} diff --git a/internal/theme/common/index.go b/internal/theme/common/index.go index c2f7992..e10553b 100644 --- a/internal/theme/common/index.go +++ b/internal/theme/common/index.go @@ -9,8 +9,10 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/plugin/pagination" + "net/http" ) func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { @@ -103,3 +105,27 @@ func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) { i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts](plugin, i.Handle, Defaults(calls...))) } + +func (i *IndexHandle) Render() { + i.ExecPostsPlugin() + if i.PageEle == nil { + i.PageEle = plugins.TwentyFifteenPagination() + } + i.Pagination() + i.CalBodyClass() + if i.Templ == "" { + i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) + } + i.C.HTML(i.Code, i.Templ, i.GinH) +} + +func (i *IndexHandle) Indexs() { + err := i.BuildIndexData(NewIndexParams(i.C)) + if err != nil { + i.Stats = constraints.Error404 + i.Code = http.StatusNotFound + i.C.HTML(i.Code, i.Templ, i.GinH) + return + } + i.Render() +} diff --git a/internal/theme/common/listpostplugins.go b/internal/theme/common/listpostplugins.go index d2f7de0..78d5d50 100644 --- a/internal/theme/common/listpostplugins.go +++ b/internal/theme/common/listpostplugins.go @@ -37,12 +37,12 @@ func PasswordProject(next Fn[models.Posts], h *Handle, post models.Posts) (r mod return } -// Digest 生成摘要 +// Digest 生成摘要 注意放到最后,不继续往下执行 func Digest(next Fn[models.Posts], h *Handle, post models.Posts) models.Posts { if post.PostExcerpt != "" { plugins.PostExcerpt(&post) } else { plugins.Digest(h.C, &post, config.GetConfig().DigestWordCount) } - return next(post) + return post } diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 6f2202d..609302d 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -1,16 +1,13 @@ package twentyfifteen import ( - "fmt" "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/cache" "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/common" "github.com/gin-gonic/gin" - "net/http" ) const ThemeName = "twentyfifteen" @@ -42,38 +39,11 @@ func Hook(h *common.Handle) { } func (i *indexHandle) Index() { - i.Templ = "twentyfifteen/posts/index.gohtml" - img := getHeaderImage(i.C) - fmt.Println(img) - err := i.BuildIndexData(common.NewIndexParams(i.C)) - if err != nil { - i.Stats = constraints.Error404 - i.Code = http.StatusNotFound - i.C.HTML(i.Code, i.Templ, i.GinH) - return - } - i.ExecPostsPlugin() - i.PageEle = plugins.TwentyFifteenPagination() - i.Pagination() - i.CalBodyClass() - i.C.HTML(i.Code, i.Templ, i.GinH) + i.Indexs() } func (d *detailHandle) Detail() { - d.Templ = "twentyfifteen/posts/detail.gohtml" - - err := d.BuildDetailData() - if err != nil { - d.Stats = constraints.Error404 - d.Code = http.StatusNotFound - d.C.HTML(d.Code, d.Templ, d.GinH) - return - } - d.PasswordProject() - d.CommentRender = plugins.CommentRender() - d.RenderComment() - d.CalBodyClass() - d.C.HTML(d.Code, d.Templ, d.GinH) + d.Details() } func getHeaderImage(c *gin.Context) (r models.PostThumbnail) { diff --git a/plugin/pagination/pagination.go b/plugin/pagination/pagination.go index 1783c4e..4601fed 100644 --- a/plugin/pagination/pagination.go +++ b/plugin/pagination/pagination.go @@ -1,7 +1,7 @@ package pagination import ( - "math" + "github.com/fthvgb1/wp-go/helper/number" "strings" ) @@ -26,8 +26,7 @@ type ParsePagination struct { } func NewParsePagination(totalRaw int, pageSize int, currentPage, step int, query string, path string) ParsePagination { - allPage := int(math.Ceil(float64(totalRaw) / float64(pageSize))) - return ParsePagination{TotalPage: allPage, TotalRaw: totalRaw, PageSize: pageSize, CurrentPage: currentPage, Query: query, Path: path, Step: step} + return ParsePagination{TotalPage: number.CalTotalPage(totalRaw, pageSize), TotalRaw: totalRaw, PageSize: pageSize, CurrentPage: currentPage, Query: query, Path: path, Step: step} } func Paginate(e Elements, p ParsePagination) string { @@ -36,7 +35,7 @@ func Paginate(e Elements, p ParsePagination) string { } func (p ParsePagination) ToHtml() (html string) { - if p.TotalRaw < 2 { + if p.TotalPage < 2 { return } s := strings.Builder{}