2023-01-17 15:18:31 +00:00
|
|
|
|
package twentyseventeen
|
|
|
|
|
|
|
|
|
|
import (
|
2023-03-01 14:52:05 +00:00
|
|
|
|
"embed"
|
|
|
|
|
"encoding/json"
|
2023-01-25 18:26:36 +00:00
|
|
|
|
"fmt"
|
2023-02-23 15:42:51 +00:00
|
|
|
|
"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"
|
2023-02-23 15:42:51 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
2023-03-02 15:49:28 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
2023-02-10 13:23:30 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
2023-03-16 16:45:04 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
2023-03-02 12:36:58 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
2023-01-18 15:02:59 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
2023-03-02 15:49:28 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
|
2023-03-01 05:17:12 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
2023-03-12 12:41:10 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
|
2023-03-14 11:21:00 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
|
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"
|
2023-01-18 15:02:59 +00:00
|
|
|
|
"strings"
|
2023-01-17 15:18:31 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const ThemeName = "twentyseventeen"
|
|
|
|
|
|
2023-03-01 14:52:05 +00:00
|
|
|
|
func Init(fs embed.FS) {
|
|
|
|
|
b, err := fs.ReadFile(str.Join(ThemeName, "/themesupport.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = json.Unmarshal(b, &themesupport)
|
|
|
|
|
}
|
|
|
|
|
|
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-03-14 11:21:00 +00:00
|
|
|
|
var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready, dispatch)...)
|
2023-02-23 12:22:42 +00:00
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func Hook(h *wp.Handle) {
|
2023-02-24 11:34:19 +00:00
|
|
|
|
pipe(h)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
2023-03-12 12:41:10 +00:00
|
|
|
|
components.WidgetArea(h)
|
2023-02-11 15:51:07 +00:00
|
|
|
|
h.GetPassword()
|
2023-03-02 15:49:28 +00:00
|
|
|
|
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
2023-03-02 13:16:43 +00:00
|
|
|
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
2023-03-02 12:36:58 +00:00
|
|
|
|
errHandle := wp.NewHandleFn(errorsHandle, 100)
|
|
|
|
|
h.PushHandleFn(constraints.Error404, errHandle)
|
|
|
|
|
h.PushHandleFn(constraints.ParamError, errHandle)
|
|
|
|
|
h.PushHandleFn(constraints.InternalErr, errHandle)
|
2023-03-04 06:44:51 +00:00
|
|
|
|
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
|
|
|
|
pushScripts(h)
|
2023-02-28 15:38:23 +00:00
|
|
|
|
h.SetData("HeaderImage", getHeaderImage(h))
|
|
|
|
|
h.SetData("scene", h.Scene())
|
2023-03-16 16:45:04 +00:00
|
|
|
|
for _, s := range []string{widgets.Meta, widgets.Categories, widgets.Archive, widgets.Search, widgets.RecentComments, widgets.RecentPosts} {
|
|
|
|
|
ss := strings.ReplaceAll(s, "-", "_")
|
|
|
|
|
if s == widgets.RecentPosts {
|
|
|
|
|
ss = "recent_entries"
|
|
|
|
|
}
|
|
|
|
|
wp.SetComponentsArgsForMap(h, s, "{$before_widget}", fmt.Sprintf(`<section id="%s-2" class="widget widget_%s">`, s, ss))
|
|
|
|
|
wp.SetComponentsArgsForMap(h, s, "{$after_widget}", "</section>")
|
|
|
|
|
}
|
|
|
|
|
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
2023-02-28 11:44:19 +00:00
|
|
|
|
next(h)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 16:45:04 +00:00
|
|
|
|
var searchForm = `<form role="search" method="get" class="search-form" action="/">
|
|
|
|
|
<label for="search-form-1">
|
|
|
|
|
<span class="screen-reader-text">{$label}:</span>
|
|
|
|
|
</label>
|
|
|
|
|
<input type="search" id="search-form-1" class="search-field" placeholder="{$placeholder}…" value="{$value}" name="s">
|
|
|
|
|
<button type="submit" class="search-submit">
|
|
|
|
|
<svg class="icon icon-search" aria-hidden="true" role="img"> <use href="#icon-search" xlink:href="#icon-search"></use> </svg>
|
|
|
|
|
<span class="screen-reader-text">{$button}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</form>`
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
2023-02-28 15:38:23 +00:00
|
|
|
|
switch h.Scene() {
|
2023-02-23 15:42:51 +00:00
|
|
|
|
case constraints.Detail:
|
2023-02-24 11:34:19 +00:00
|
|
|
|
detail(next, h.Detail)
|
2023-02-23 15:42:51 +00:00
|
|
|
|
default:
|
2023-02-24 11:34:19 +00:00
|
|
|
|
index(next, h.Index)
|
2023-02-04 16:07:10 +00:00
|
|
|
|
}
|
2023-02-08 15:49:48 +00:00
|
|
|
|
}
|
2023-02-04 16:07:10 +00:00
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
|
|
|
|
|
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
|
2023-02-09 15:15:10 +00:00
|
|
|
|
"twentyseventeen_postThumbnail": postThumbnail,
|
|
|
|
|
})
|
2023-02-09 08:08:18 +00:00
|
|
|
|
}()
|
2023-02-04 16:07:10 +00:00
|
|
|
|
|
2023-03-02 12:36:58 +00:00
|
|
|
|
func errorsHandle(h *wp.Handle) {
|
|
|
|
|
h.SetTempl("twentyseventeen/posts/error.gohtml")
|
|
|
|
|
switch h.Stats {
|
|
|
|
|
case constraints.Error404, constraints.InternalErr:
|
|
|
|
|
logs.ErrPrintln(h.Err(), "报错:")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
|
|
|
|
|
err := i.BuildIndexData(wp.NewIndexParams(i.C))
|
2023-02-10 16:32:46 +00:00
|
|
|
|
if err != nil {
|
2023-03-02 12:36:58 +00:00
|
|
|
|
i.SetErr(err)
|
2023-02-10 16:32:46 +00:00
|
|
|
|
}
|
2023-02-28 15:59:17 +00:00
|
|
|
|
i.SetPageEle(paginate)
|
|
|
|
|
i.SetPostsPlugins(listPostsPlugins)
|
2023-02-24 11:34:19 +00:00
|
|
|
|
next(i.Handle)
|
2023-02-04 16:07:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
|
2023-02-11 15:51:07 +00:00
|
|
|
|
err := d.BuildDetailData()
|
|
|
|
|
if err != nil {
|
2023-03-02 12:36:58 +00:00
|
|
|
|
d.SetErr(err)
|
2023-02-09 12:49:33 +00:00
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
|
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{}
|
|
|
|
|
|
2023-01-26 16:07:42 +00:00
|
|
|
|
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 {
|
2023-01-26 16:07:42 +00:00
|
|
|
|
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)
|
2023-01-26 16:07:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func postThumbnail(next wp.Fn[models.Posts], h *wp.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-28 15:38:23 +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
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 15:42:51 +00:00
|
|
|
|
var header = reload.Vars(models.PostThumbnail{})
|
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
|
2023-02-23 15:42:51 +00:00
|
|
|
|
img := header.Load()
|
2023-02-24 16:56:52 +00:00
|
|
|
|
if img.Path != "" {
|
2023-03-01 15:34:05 +00:00
|
|
|
|
return img
|
2023-02-24 16:56:52 +00:00
|
|
|
|
}
|
|
|
|
|
image, rand := h.GetCustomHeader()
|
|
|
|
|
if image.Path != "" {
|
|
|
|
|
r = image
|
|
|
|
|
r.Sizes = "100vw"
|
2023-02-23 15:42:51 +00:00
|
|
|
|
if !rand {
|
|
|
|
|
header.Store(r)
|
|
|
|
|
}
|
2023-02-24 16:56:52 +00:00
|
|
|
|
return
|
2023-01-17 15:18:31 +00:00
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
|
r.Path = helper.CutUrlHost(h.CommonThemeMods().ThemeSupport.CustomHeader.DefaultImage)
|
2023-02-24 16:56:52 +00:00
|
|
|
|
r.Width = 2000
|
|
|
|
|
r.Height = 1200
|
|
|
|
|
header.Store(r)
|
2023-01-17 15:18:31 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-25 18:26:36 +00:00
|
|
|
|
|
2023-03-01 05:17:12 +00:00
|
|
|
|
func calClass(h *wp.Handle) {
|
2023-02-28 15:38:23 +00:00
|
|
|
|
themeMods := h.CommonThemeMods()
|
|
|
|
|
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
|
|
|
|
|
var class []string
|
2023-02-23 10:21:51 +00:00
|
|
|
|
if u != "" && u != "remove-header" {
|
2023-02-28 15:38:23 +00:00
|
|
|
|
class = append(class, "has-header-image")
|
2023-02-09 12:49:33 +00:00
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
|
if len(themeMods.SidebarsWidgets.Data.Sidebar1) > 0 {
|
|
|
|
|
class = append(class, "has-sidebar")
|
2023-02-24 16:56:52 +00:00
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
|
if themeMods.HeaderTextcolor == "blank" {
|
|
|
|
|
class = append(class, "title-tagline-hidden")
|
2023-02-24 16:56:52 +00:00
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
|
class = append(class, "hfeed")
|
|
|
|
|
class = append(class, str.Join("colors-", wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light")))
|
|
|
|
|
if h.Scene() == constraints.Archive {
|
|
|
|
|
if "one-column" == wpconfig.GetThemeModsVal(ThemeName, "page_layout", "") {
|
|
|
|
|
class = append(class, "page-one-column")
|
2023-02-23 10:21:51 +00:00
|
|
|
|
} else {
|
2023-02-28 15:38:23 +00:00
|
|
|
|
class = append(class, "page-two-column")
|
2023-02-04 16:07:10 +00:00
|
|
|
|
}
|
2023-01-25 18:26:36 +00:00
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
|
h.PushClass(class...)
|
2023-01-25 18:26:36 +00:00
|
|
|
|
}
|