Compare commits

...

2 Commits

Author SHA1 Message Date
xing
8a5fc02247 优化代码 2023-02-24 19:43:12 +08:00
xing
ccbba30ae2 再优化代码 2023-02-24 19:34:19 +08:00
7 changed files with 46 additions and 34 deletions

View File

@ -9,6 +9,9 @@ import (
func ThemeHook(scene int) func(*gin.Context) { func ThemeHook(scene int) func(*gin.Context) {
return func(ctx *gin.Context) { return func(ctx *gin.Context) {
t := theme.GetTemplateName() t := theme.GetTemplateName()
theme.Hook(t, common.NewHandle(ctx, scene, t)) h := common.NewHandle(ctx, scene, t)
h.Index = common.NewIndexHandle(h)
h.Detail = common.NewDetailHandle(h)
theme.Hook(t, h)
} }
} }

View File

@ -13,6 +13,8 @@ import (
) )
type Handle struct { type Handle struct {
Index *IndexHandle
Detail *DetailHandle
C *gin.Context C *gin.Context
Theme string Theme string
Session sessions.Session Session sessions.Session
@ -96,3 +98,12 @@ func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
} }
}, initial) }, initial)
} }
func Render(h *Handle) {
switch h.Scene {
case constraints.Detail:
h.Detail.Render()
default:
h.Index.Render()
}
}

View File

@ -46,7 +46,6 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
return return
} }
d.GinH["post"] = post
d.Post = post d.Post = post
d.GinH["user"] = cache.GetUserById(d.C, post.PostAuthor) d.GinH["user"] = cache.GetUserById(d.C, post.PostAuthor)
d.GinH["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.GetOption("blogname")) d.GinH["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.GetOption("blogname"))
@ -96,6 +95,7 @@ func (d *DetailHandle) ContextPost() {
func (d *DetailHandle) Render() { func (d *DetailHandle) Render() {
d.PasswordProject() d.PasswordProject()
d.RenderComment() d.RenderComment()
d.GinH["post"] = d.Post
d.Handle.Render() d.Handle.Render()
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/fthvgb1/wp-go/internal/theme/twentyfifteen" "github.com/fthvgb1/wp-go/internal/theme/twentyfifteen"
) )
var themeMap = map[string]func(handle *common.Handle){} var themeMap = map[string]func(*common.Handle){}
func addThemeHookFunc(name string, fn func(handle *common.Handle)) { func addThemeHookFunc(name string, fn func(handle *common.Handle)) {
if _, ok := themeMap[name]; ok { if _, ok := themeMap[name]; ok {

View File

@ -28,14 +28,13 @@ func Init(fs embed.FS) {
logs.ErrPrintln(err, "解析colorscheme失败") logs.ErrPrintln(err, "解析colorscheme失败")
} }
var detailPipe = common.HandlePipe(func(d *common.DetailHandle) { var pipe = common.HandlePipe(common.Render, dispatch)
d.Render()
}, detail)
var indexPipe = common.HandlePipe(func(i *common.IndexHandle) {
i.Render()
}, index)
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
pipe(h)
}
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.AutoCal("colorScheme", colorSchemeCss) h.AutoCal("colorScheme", colorSchemeCss)
@ -43,16 +42,16 @@ func Hook(h *common.Handle) {
h.PushHandleFn(customHeader) h.PushHandleFn(customHeader)
switch h.Scene { switch h.Scene {
case constraints.Detail: case constraints.Detail:
detailPipe(common.NewDetailHandle(h)) detail(next, h.Detail)
default: default:
indexPipe(common.NewIndexHandle(h)) index(next, h.Index)
} }
} }
func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) { func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
i.Indexs() i.Indexs()
} }
func detail(fn common.HandleFn[*common.DetailHandle], d *common.DetailHandle) { func detail(fn common.HandleFn[*common.Handle], d *common.DetailHandle) {
d.Details() d.Details()
} }

View File

@ -27,23 +27,22 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
var detailPipe = common.HandlePipe(func(d *common.DetailHandle) { var pipe = common.HandlePipe(common.Render, dispatch)
d.Render()
}, detail)
var indexPipe = common.HandlePipe(func(i *common.IndexHandle) {
i.Render()
}, index)
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
pipe(h)
}
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(calClass) h.PushHandleFn(calClass)
h.GinH["HeaderImage"] = getHeaderImage(h) h.GinH["HeaderImage"] = getHeaderImage(h)
switch h.Scene { switch h.Scene {
case constraints.Detail: case constraints.Detail:
detailPipe(common.NewDetailHandle(h)) detail(next, h.Detail)
default: default:
indexPipe(common.NewIndexHandle(h)) index(next, h.Index)
} }
} }
@ -53,7 +52,7 @@ var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Han
}) })
}() }()
func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) { func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
err := i.BuildIndexData(common.NewIndexParams(i.C)) err := i.BuildIndexData(common.NewIndexParams(i.C))
if err != nil { if err != nil {
i.Stats = constraints.Error404 i.Stats = constraints.Error404
@ -64,10 +63,10 @@ func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) {
} }
i.PostsPlugins = listPostsPlugins i.PostsPlugins = listPostsPlugins
i.PageEle = paginate i.PageEle = paginate
next(i) next(i.Handle)
} }
func detail(next common.HandleFn[*common.DetailHandle], d *common.DetailHandle) { func detail(next common.HandleFn[*common.Handle], d *common.DetailHandle) {
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.Code = http.StatusNotFound d.Code = http.StatusNotFound
@ -76,16 +75,16 @@ func detail(next common.HandleFn[*common.DetailHandle], d *common.DetailHandle)
d.C.HTML(d.Code, d.Templ, d.GinH) d.C.HTML(d.Code, d.Templ, d.GinH)
return return
} }
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail") if d.Post.Thumbnail.Path != "" {
img.Width = img.OriginAttachmentData.Width img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail")
img.Height = img.OriginAttachmentData.Height img.Sizes = "100vw"
img.Sizes = "100vw" img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset) d.Post.Thumbnail = img
d.Post.Thumbnail = img }
d.CommentRender = commentFormat
d.GinH["post"] = d.Post
next(d) d.CommentRender = commentFormat
next(d.Handle)
} }
var commentFormat = comment{} var commentFormat = comment{}

View File

@ -72,7 +72,7 @@ type ImageData struct {
func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) { func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) {
up := strings.Split(metadata.File, "/") up := strings.Split(metadata.File, "/")
if Type == "full" { if metadata.File != "" && Type == "full" {
metadata.Sizes["full"] = models.MetaDataFileSize{ metadata.Sizes["full"] = models.MetaDataFileSize{
File: filepath.Base(metadata.File), File: filepath.Base(metadata.File),
Width: metadata.Width, Width: metadata.Width,