wp-go/internal/theme/twentyseventeen/twentyseventeen.go
2023-02-23 20:22:42 +08:00

148 lines
5.1 KiB
Go

package twentyseventeen
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"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/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 = `<a class="prev page-numbers" href="%s"><svg class="icon icon-arrow-left" aria-hidden="true" role="img"> <use href="#icon-arrow-left" xlink:href="#icon-arrow-left"></use> </svg>
<span class="screen-reader-text">上一页</span></a>`
p.NextEle = strings.Replace(p.NextEle, "下一页", `<span class="screen-reader-text">下一页</span>
<svg class="icon icon-arrow-right" aria-hidden="true" role="img"> <use href="#icon-arrow-right" xlink:href="#icon-arrow-right"></use>
</svg>`, 1)
return p
}()
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) {
h.WidgetAreaData()
h.GetPassword()
h.HandleFns = append(h.HandleFns, calClass)
h.GinH["HeaderImage"] = getHeaderImage(h.C)
if h.Scene == constraints.Detail {
detailPipe(common.NewDetailHandle(h))
return
}
indexPipe(common.NewIndexHandle(h))
}
var pluginFns = 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.IndexHandle], 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 = pluginFns
i.PageEle = paginate
next(i)
}
func detail(next common.HandleFn[*common.DetailHandle], 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
}
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)
}
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, `<a rel="nofollow" class="comment-reply-link"
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"
data-replyto="回复给{{CommentAuthor}}"
aria-label="回复给{{CommentAuthor}}">回复</a>`, `<a rel="nofollow" class="comment-reply-link"
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"
data-replyto="回复给{{CommentAuthor}}"
aria-label="回复给{{CommentAuthor}}"><svg class="icon icon-mail-reply" aria-hidden="true" role="img"> <use href="#icon-mail-reply" xlink:href="#icon-mail-reply"></use> </svg>回复</a>`)
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)
}
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
r.Width = 2000
r.Height = 1200
hs, err := cache.GetHeaderImages(c, ThemeName)
if err != nil {
logs.ErrPrintln(err, "获取页眉背景图失败")
} else if len(hs) > 0 && err == nil {
_, r = slice.Rand(hs)
}
r.Sizes = "100vw"
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 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")
}
}
}