优化代码

This commit is contained in:
xing 2023-02-14 23:28:45 +08:00
parent f96f364a69
commit 72a37d3bc8
5 changed files with 59 additions and 38 deletions

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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
}

View File

@ -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) {

View File

@ -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{}