wp-go/internal/theme/twentyseventeen/twentyseventeen.go
2023-02-25 00:56:52 +08:00

167 lines
5.5 KiB
Go

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 = `<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 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, `<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)
}
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")
}
}
}