wp-go/internal/theme/twentyseventeen/twentyseventeen.go

167 lines
5.5 KiB
Go
Raw Normal View History

2023-01-17 15:18:31 +00:00
package twentyseventeen
import (
2023-01-25 18:26:36 +00:00
"fmt"
"github.com/fthvgb1/wp-go/helper"
2023-02-09 15:15:10 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
2023-02-24 16:56:52 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
2023-02-10 13:23:30 +00:00
"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"
2023-02-14 11:47:47 +00:00
"github.com/fthvgb1/wp-go/internal/wpconfig"
2023-01-17 15:18:31 +00:00
"github.com/gin-gonic/gin"
"net/http"
"strings"
2023-01-17 15:18:31 +00:00
)
const ThemeName = "twentyseventeen"
2023-01-18 15:11:26 +00:00
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
}()
2023-02-24 11:34:19 +00:00
var pipe = common.HandlePipe(common.Render, dispatch)
2023-02-23 12:22:42 +00:00
func Hook(h *common.Handle) {
2023-02-24 11:34:19 +00:00
pipe(h)
}
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData()
h.GetPassword()
h.PushHandleFn(calClass)
2023-02-24 16:56:52 +00:00
h.PushHeadScript(constraints.HeadScript, colorScheme)
h.GinH["HeaderImage"] = getHeaderImage(h)
switch h.Scene {
case constraints.Detail:
2023-02-24 11:34:19 +00:00
detail(next, h.Detail)
default:
2023-02-24 11:34:19 +00:00
index(next, h.Index)
}
}
var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Handle] {
2023-02-12 11:19:53 +00:00
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{
2023-02-09 15:15:10 +00:00
"twentyseventeen_postThumbnail": postThumbnail,
})
2023-02-09 08:08:18 +00:00
}()
2023-02-24 11:34:19 +00:00
func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
2023-02-11 15:58:40 +00:00
err := i.BuildIndexData(common.NewIndexParams(i.C))
if err != nil {
2023-02-11 15:58:40 +00:00
i.Stats = constraints.Error404
i.Code = http.StatusNotFound
2023-02-23 09:26:20 +00:00
i.CalBodyClass()
i.C.HTML(i.Code, i.Templ, i.GinH)
return
}
i.PostsPlugins = listPostsPlugins
i.PageEle = paginate
2023-02-24 11:34:19 +00:00
next(i.Handle)
}
2023-02-24 11:34:19 +00:00
func detail(next common.HandleFn[*common.Handle], d *common.DetailHandle) {
err := d.BuildDetailData()
if err != nil {
d.Code = http.StatusNotFound
2023-02-11 15:58:40 +00:00
d.Stats = constraints.Error404
2023-02-23 10:21:51 +00:00
d.GinH["bodyClass"] = d.BodyClass()
d.C.HTML(d.Code, d.Templ, d.GinH)
2023-02-09 12:49:33 +00:00
return
}
2023-02-24 11:34:19 +00:00
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
2023-02-16 16:20:38 +00:00
2023-02-24 11:34:19 +00:00
next(d.Handle)
2023-01-17 15:18:31 +00:00
}
2023-02-09 12:49:33 +00:00
var commentFormat = comment{}
type comment struct {
plugins.CommonCommentFormat
}
2023-01-30 12:48:30 +00:00
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>`)
2023-01-30 12:48:30 +00:00
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 {
2023-02-09 08:08:18 +00:00
if t.Thumbnail.Path != "" {
2023-02-09 09:12:33 +00:00
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
2023-02-10 13:23:30 +00:00
if h.Scene == constraints.Detail {
2023-02-09 08:08:18 +00:00
t.Thumbnail.Sizes = "100vw"
2023-01-25 18:26:36 +00:00
}
2023-02-09 08:08:18 +00:00
}
return next(t)
2023-01-25 18:26:36 +00:00
}
var header = reload.Vars(models.PostThumbnail{})
func getHeaderImage(h *common.Handle) (r models.PostThumbnail) {
img := header.Load()
2023-02-24 16:56:52 +00:00
if img.Path != "" {
return r
}
image, rand := h.GetCustomHeader()
if image.Path != "" {
r = image
r.Sizes = "100vw"
if !rand {
header.Store(r)
}
2023-02-24 16:56:52 +00:00
return
2023-01-17 15:18:31 +00:00
}
2023-02-24 16:56:52 +00:00
r.Path = helper.CutUrlHost(h.ThemeMods.ThemeSupport.CustomHeader.DefaultImage)
r.Width = 2000
r.Height = 1200
2023-01-31 16:58:42 +00:00
r.Sizes = "100vw"
2023-02-24 16:56:52 +00:00
header.Store(r)
2023-01-17 15:18:31 +00:00
return
}
2023-01-25 18:26:36 +00:00
2023-02-23 10:21:51 +00:00
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")
2023-02-09 12:49:33 +00:00
}
2023-02-24 16:56:52 +00:00
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")))
2023-02-23 10:21:51 +00:00
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")
}
2023-01-25 18:26:36 +00:00
}
}