package twentyseventeen import ( "fmt" "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/cmd/reload" "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/internal/theme/common" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/gin-gonic/gin" "net/http" "strings" ) const ThemeName = "twentyseventeen" var paginate = func() plugins.PageEle { p := plugins.TwentyFifteenPagination() p.PrevEle = `` p.NextEle = strings.Replace(p.NextEle, "下一页", `下一页 `, 1) return p }() var pipe = common.HandlePipe(common.Render, dispatch) 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.PushHeadScript(constraints.HeadScript, colorScheme) h.GinH["HeaderImage"] = getHeaderImage(h) switch h.Scene { case constraints.Detail: detail(next, h.Detail) default: index(next, h.Index) } } var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Handle] { return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{ "twentyseventeen_postThumbnail": postThumbnail, }) }() func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) { err := i.BuildIndexData(common.NewIndexParams(i.C)) if err != nil { i.Stats = constraints.Error404 i.Code = http.StatusNotFound i.CalBodyClass() i.C.HTML(i.Code, i.Templ, i.GinH) return } i.PostsPlugins = listPostsPlugins i.PageEle = paginate next(i.Handle) } func detail(next common.HandleFn[*common.Handle], d *common.DetailHandle) { err := d.BuildDetailData() if err != nil { d.Code = http.StatusNotFound d.Stats = constraints.Error404 d.GinH["bodyClass"] = d.BodyClass() 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 } d.CommentRender = commentFormat next(d.Handle) } var commentFormat = comment{} type comment struct { plugins.CommonCommentFormat } func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string { templ := plugins.CommonLi() templ = strings.ReplaceAll(templ, `回复`, `回复`) return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) } func postThumbnail(next common.Fn[models.Posts], h *common.Handle, t models.Posts) models.Posts { if t.Thumbnail.Path != "" { t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" if h.Scene == constraints.Detail { t.Thumbnail.Sizes = "100vw" } } return next(t) } var header = reload.Vars(models.PostThumbnail{}) func getHeaderImage(h *common.Handle) (r models.PostThumbnail) { img := header.Load() if img.Path != "" { return r } image, rand := h.GetCustomHeader() if image.Path != "" { r = image r.Sizes = "100vw" if !rand { header.Store(r) } return } r.Path = helper.CutUrlHost(h.ThemeMods.ThemeSupport.CustomHeader.DefaultImage) r.Width = 2000 r.Height = 1200 r.Sizes = "100vw" header.Store(r) return } func calClass(h *common.Handle) { u := wpconfig.GetThemeModsVal(h.Theme, "header_image", h.ThemeMods.ThemeSupport.CustomHeader.DefaultImage) if u != "" && u != "remove-header" { h.Class = append(h.Class, "has-header-image") } if len(h.ThemeMods.SidebarsWidgets.Data.Sidebar1) > 0 { h.Class = append(h.Class, "has-sidebar") } if h.ThemeMods.HeaderTextcolor == "blank" { h.Class = append(h.Class, "title-tagline-hidden") } h.Class = append(h.Class, "hfeed") h.Class = append(h.Class, str.Join("colors-", wpconfig.GetThemeModsVal(h.Theme, "colorscheme", "light"))) if h.Scene == constraints.Archive { if "one-column" == wpconfig.GetThemeModsVal(h.Theme, "page_layout", "") { h.Class = append(h.Class, "page-one-column") } else { h.Class = append(h.Class, "page-two-column") } } }