Compare commits

..

No commits in common. "8a5fc0224722ffa354b48ea19c8c7c9e26a6d91b" and "865b37cf89d349a18ace9ee0f7f439d4f6c25676" have entirely different histories.

7 changed files with 33 additions and 45 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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