Compare commits
No commits in common. "e505ee2e03f6f91d75d5618a76852f3e6abdf2cc" and "9e293e7f39f8a30fde7cd35acf854585ff1cda8f" have entirely different histories.
e505ee2e03
...
9e293e7f39
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
.idea
|
.idea
|
||||||
wp-go.iml
|
wp-go.iml
|
||||||
config.yaml
|
config.yaml
|
||||||
err.log
|
|
||||||
|
@ -12,10 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var logs = safety.NewVar[*log.Logger](nil)
|
var logs = safety.NewVar[*log.Logger](nil)
|
||||||
var logFile = safety.NewVar[*os.File](nil)
|
|
||||||
|
|
||||||
func InitLogger() error {
|
func InitLogger() error {
|
||||||
preFD := logFile.Load()
|
|
||||||
l := &log.Logger{}
|
l := &log.Logger{}
|
||||||
c := config.GetConfig()
|
c := config.GetConfig()
|
||||||
if c.LogOutput == "" {
|
if c.LogOutput == "" {
|
||||||
@ -33,10 +31,6 @@ func InitLogger() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
out = file
|
out = file
|
||||||
logFile.Store(file)
|
|
||||||
}
|
|
||||||
if preFD != nil {
|
|
||||||
_ = preFD.Close()
|
|
||||||
}
|
}
|
||||||
logs.Store(l)
|
logs.Store(l)
|
||||||
l.SetFlags(log.Ldate | log.Ltime)
|
l.SetFlags(log.Ldate | log.Ltime)
|
||||||
|
@ -1,82 +1,16 @@
|
|||||||
package enlightjs
|
package enlightjs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/helper/maps"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/phphelper"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
||||||
"github.com/goccy/go-json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Selectors Selectors `json:"selectors"`
|
|
||||||
Options Options `json:"options"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Options struct {
|
|
||||||
Indent int64 `json:"indent,omitempty"`
|
|
||||||
AmpersandCleanup bool `json:"ampersandCleanup,omitempty"`
|
|
||||||
Linehover bool `json:"linehover,omitempty"`
|
|
||||||
RawcodeDbclick bool `json:"rawcodeDbclick,omitempty"`
|
|
||||||
TextOverflow string `json:"textOverflow,omitempty"`
|
|
||||||
Linenumbers int64 `json:"linenumbers,omitempty"`
|
|
||||||
Theme string `json:"theme,omitempty"`
|
|
||||||
Language string `json:"language,omitempty"`
|
|
||||||
RetainCssClasses bool `json:"retainCssClasses,omitempty"`
|
|
||||||
Collapse bool `json:"collapse,omitempty"`
|
|
||||||
ToolbarOuter string `json:"toolbarOuter,omitempty"`
|
|
||||||
ToolbarTop string `json:"toolbarTop,omitempty"`
|
|
||||||
ToolbarBottom string `json:"toolbarBottom,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Selectors struct {
|
|
||||||
Block string `json:"block,omitempty"`
|
|
||||||
Inline string `json:"inline,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnlighterJS(h *wp.Handle) {
|
func EnlighterJS(h *wp.Handle) {
|
||||||
h.PushGroupHeadScript(20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
|
h.PushGroupHeadScript(20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
|
||||||
|
|
||||||
h.PushCacheGroupFooterScript("enlighterJs", 10, func(h *wp.Handle) string {
|
h.PushGroupFooterScript(10, str.Join(`<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>`, "\n", enlighterjs))
|
||||||
op := wpconfig.GetOption("enlighter-options")
|
|
||||||
opp, err := phphelper.UnPHPSerializeToStrAnyMap(op)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err, "获取enlighter-option失败", op)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
v := Config{
|
|
||||||
Selectors: Selectors{
|
|
||||||
Block: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-selector-block", "pre.EnlighterJSRAW"),
|
|
||||||
Inline: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-selector-inline", "code.EnlighterJSRAW"),
|
|
||||||
},
|
|
||||||
Options: Options{
|
|
||||||
Indent: maps.GetStrAnyValWithDefaults[int64](opp, "enlighterjs-indent", 4),
|
|
||||||
AmpersandCleanup: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-ampersandcleanup", true),
|
|
||||||
Linehover: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-linehover", true),
|
|
||||||
RawcodeDbclick: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-rawcodedbclick", true),
|
|
||||||
TextOverflow: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-textoverflow", "break"),
|
|
||||||
Linenumbers: maps.GetStrAnyValWithDefaults[int64](opp, "enlighterjs-linenumbers", 1),
|
|
||||||
Theme: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-theme", "enlighter"),
|
|
||||||
Language: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-language", "generic"),
|
|
||||||
RetainCssClasses: maps.GetStrAnyValWithDefaults(opp, "enlighterjs-retaincss", false),
|
|
||||||
Collapse: false,
|
|
||||||
ToolbarOuter: "",
|
|
||||||
ToolbarTop: "{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}",
|
|
||||||
ToolbarBottom: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
conf, err := json.Marshal(v)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err, "json化enlighterjs配置失败")
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(enlighterjs, conf)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var enlighterjs = `<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>
|
var enlighterjs = `<script id='enlighterjs-js-after'>
|
||||||
<script id='enlighterjs-js-after'>
|
!function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
|
||||||
!function(e,n){if("undefined"!=typeof EnlighterJS){var o=%s;(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
|
|
||||||
</script>`
|
</script>`
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
|
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components"
|
||||||
@ -63,15 +62,12 @@ func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) {
|
func configs(h *wp.Handle) {
|
||||||
conf := config.GetConfig()
|
|
||||||
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
|
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
|
||||||
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
||||||
})
|
})
|
||||||
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
|
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||||
wphandle.RegisterPlugins(h, conf.Plugins...)
|
|
||||||
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
|
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
|
||||||
h.CommonComponents()
|
h.CommonComponents()
|
||||||
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
|
|
||||||
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
|
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper"
|
"github.com/fthvgb1/wp-go/helper"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
@ -49,8 +50,7 @@ func Hook(h *wp.Handle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configs(h *wp.Handle) {
|
func configs(h *wp.Handle) {
|
||||||
conf := config.GetConfig()
|
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||||
wphandle.RegisterPlugins(h, conf.Plugins...)
|
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
||||||
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
||||||
components.WidgetArea(h)
|
components.WidgetArea(h)
|
||||||
@ -64,8 +64,6 @@ func configs(h *wp.Handle) {
|
|||||||
})
|
})
|
||||||
h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreTemplate, errorsHandle)
|
h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreTemplate, errorsHandle)
|
||||||
h.CommonComponents()
|
h.CommonComponents()
|
||||||
h.Index.SetPageEle(paginate)
|
|
||||||
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
|
|
||||||
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
||||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
|
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
|
||||||
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
|
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
|
||||||
@ -87,6 +85,12 @@ var searchForm = `<form role="search" method="get" class="search-form" action="/
|
|||||||
</button>
|
</button>
|
||||||
</form>`
|
</form>`
|
||||||
|
|
||||||
|
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
|
||||||
|
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
|
||||||
|
"twentyseventeen_postThumbnail": postThumbnail,
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
|
||||||
func errorsHandle(h *wp.Handle) {
|
func errorsHandle(h *wp.Handle) {
|
||||||
switch h.Stats {
|
switch h.Stats {
|
||||||
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
|
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
|
||||||
@ -116,6 +120,8 @@ func index(h *wp.Handle) {
|
|||||||
i.SetErr(err)
|
i.SetErr(err)
|
||||||
}
|
}
|
||||||
h.SetData("scene", h.Scene())
|
h.SetData("scene", h.Scene())
|
||||||
|
i.SetPageEle(paginate)
|
||||||
|
i.SetPostsPlugins(listPostsPlugins)
|
||||||
}
|
}
|
||||||
|
|
||||||
func detail(h *wp.Handle) {
|
func detail(h *wp.Handle) {
|
||||||
@ -153,13 +159,14 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls
|
|||||||
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
|
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func postThumbnail(h *wp.Handle, posts *models.Posts) {
|
func postThumbnail(next wp.Fn[models.Posts], h *wp.Handle, t models.Posts) models.Posts {
|
||||||
if posts.Thumbnail.Path != "" {
|
if t.Thumbnail.Path != "" {
|
||||||
posts.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
|
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
|
||||||
if h.Scene() == constraints.Detail {
|
if h.Scene() == constraints.Detail {
|
||||||
posts.Thumbnail.Sizes = "100vw"
|
t.Thumbnail.Sizes = "100vw"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return next(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
var header = reload.Vars(models.PostThumbnail{})
|
var header = reload.Vars(models.PostThumbnail{})
|
||||||
|
@ -100,6 +100,21 @@ func (d *DetailHandle) ContextPost() {
|
|||||||
d.ginH["prev"] = prev
|
d.ginH["prev"] = prev
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DetailHandle) Render() {
|
||||||
|
d.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
|
||||||
|
d.PasswordProject()
|
||||||
|
d.RenderComment()
|
||||||
|
d.ginH["post"] = d.Post
|
||||||
|
reply := ""
|
||||||
|
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
|
||||||
|
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
|
||||||
|
}
|
||||||
|
d.PushGroupFooterScript(10, reply)
|
||||||
|
}, 10))
|
||||||
|
|
||||||
|
d.Handle.Render()
|
||||||
|
}
|
||||||
|
|
||||||
func DetailRender(h *Handle) {
|
func DetailRender(h *Handle) {
|
||||||
if h.Stats != constraints.Ok {
|
if h.Stats != constraints.Ok {
|
||||||
return
|
return
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/helper/number"
|
"github.com/fthvgb1/wp-go/helper/number"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
@ -20,15 +21,7 @@ type IndexHandle struct {
|
|||||||
Posts []models.Posts
|
Posts []models.Posts
|
||||||
pageEle pagination.Elements
|
pageEle pagination.Elements
|
||||||
TotalRows int
|
TotalRows int
|
||||||
postsPlugin PostsPlugin
|
postsPlugins map[string]Plugin[models.Posts, *Handle]
|
||||||
}
|
|
||||||
|
|
||||||
func (i *IndexHandle) ListPlugin() func(*Handle, *models.Posts) {
|
|
||||||
return i.postsPlugin
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *IndexHandle) SetListPlugin(listPlugin func(*Handle, *models.Posts)) {
|
|
||||||
i.postsPlugin = listPlugin
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) PageEle() pagination.Elements {
|
func (i *IndexHandle) PageEle() pagination.Elements {
|
||||||
@ -39,6 +32,14 @@ func (i *IndexHandle) SetPageEle(pageEle pagination.Elements) {
|
|||||||
i.pageEle = pageEle
|
i.pageEle = pageEle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) PostsPlugins() map[string]Plugin[models.Posts, *Handle] {
|
||||||
|
return i.postsPlugins
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) SetPostsPlugins(postsPlugins map[string]Plugin[models.Posts, *Handle]) {
|
||||||
|
i.postsPlugins = postsPlugins
|
||||||
|
}
|
||||||
|
|
||||||
func NewIndexHandle(handle *Handle) *IndexHandle {
|
func NewIndexHandle(handle *Handle) *IndexHandle {
|
||||||
return &IndexHandle{Handle: handle}
|
return &IndexHandle{Handle: handle}
|
||||||
}
|
}
|
||||||
@ -125,16 +126,34 @@ func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
|
|||||||
}
|
}
|
||||||
i.Posts = posts
|
i.Posts = posts
|
||||||
i.TotalRows = totalRows
|
i.TotalRows = totalRows
|
||||||
|
|
||||||
i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize)
|
i.ginH["totalPage"] = number.CalTotalPage(totalRows, i.Param.PageSize)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) ExecPostsPlugin() {
|
func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
|
||||||
if i.postsPlugin != nil {
|
|
||||||
for j := range i.Posts {
|
pluginConf := config.GetConfig().ListPagePlugins
|
||||||
i.postsPlugin(i.Handle, &i.Posts[j])
|
|
||||||
}
|
postsPlugins := i.postsPlugins
|
||||||
|
if postsPlugins == nil {
|
||||||
|
postsPlugins = pluginFns
|
||||||
}
|
}
|
||||||
|
plugin := GetListPostPlugins(pluginConf, postsPlugins)
|
||||||
|
|
||||||
|
i.Posts = slice.Map(i.Posts, PluginFn[models.Posts](plugin, i.Handle, Defaults(calls...)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) Render() {
|
||||||
|
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
|
||||||
|
i.ExecPostsPlugin()
|
||||||
|
i.Pagination()
|
||||||
|
i.ginH["posts"] = i.Posts
|
||||||
|
}, 10))
|
||||||
|
|
||||||
|
i.Handle.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
func IndexRender(h *Handle) {
|
func IndexRender(h *Handle) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package wp
|
package wp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/fthvgb1/wp-go/helper/maps"
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
@ -12,53 +13,47 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/plugins/wpposts"
|
"github.com/fthvgb1/wp-go/internal/plugins/wpposts"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PostsPlugin func(*Handle, *models.Posts)
|
type Fn[T any] func(T) T
|
||||||
|
type Plugin[T, H any] func(initialFn Fn[T], h H, t T) T
|
||||||
|
|
||||||
func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *models.Posts)) PostsPlugin {
|
func PluginFn[T, H any](a []Plugin[T, H], h H, fn Fn[T]) Fn[T] {
|
||||||
return slice.ReverseReduce(calls, func(t func(PostsPlugin, *Handle, *models.Posts), r PostsPlugin) PostsPlugin {
|
return slice.ReverseReduce(a, func(next Plugin[T, H], fn Fn[T]) Fn[T] {
|
||||||
return func(handle *Handle, posts *models.Posts) {
|
return func(t T) T {
|
||||||
t(r, handle, posts)
|
return next(fn, h, t)
|
||||||
}
|
}
|
||||||
}, initial)
|
}, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
var pluginFns = map[string]func(PostsPlugin, *Handle, *models.Posts){
|
var pluginFns = map[string]Plugin[models.Posts, *Handle]{
|
||||||
"passwordProject": PasswordProject,
|
"passwordProject": PasswordProject,
|
||||||
"digest": Digest,
|
"digest": Digest,
|
||||||
}
|
}
|
||||||
|
|
||||||
// PasswordProject 标题和内容密码保护
|
// PasswordProject 标题和内容密码保护
|
||||||
func PasswordProject(next PostsPlugin, h *Handle, post *models.Posts) {
|
func PasswordProject(next Fn[models.Posts], h *Handle, post models.Posts) (r models.Posts) {
|
||||||
r := post
|
r = post
|
||||||
if post.PostPassword != "" {
|
if post.PostPassword != "" {
|
||||||
wpposts.PasswordProjectTitle(r)
|
wpposts.PasswordProjectTitle(&r)
|
||||||
if h.password != post.PostPassword {
|
if h.password != post.PostPassword {
|
||||||
wpposts.PasswdProjectContent(r)
|
wpposts.PasswdProjectContent(&r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next(h, r)
|
r = next(r)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Digest 生成摘要
|
// Digest 生成摘要 注意放到最后,不继续往下执行
|
||||||
func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
|
func Digest(next Fn[models.Posts], h *Handle, post models.Posts) models.Posts {
|
||||||
if post.PostExcerpt != "" {
|
if post.PostExcerpt != "" {
|
||||||
plugins.PostExcerpt(post)
|
plugins.PostExcerpt(&post)
|
||||||
} else {
|
} else {
|
||||||
plugins.Digest(h.C, post, config.GetConfig().DigestWordCount)
|
plugins.Digest(h.C, &post, config.GetConfig().DigestWordCount)
|
||||||
}
|
}
|
||||||
next(h, post)
|
return post
|
||||||
}
|
}
|
||||||
|
|
||||||
func PostPlugin(calls ...func(h *Handle, posts *models.Posts)) PostsPlugin {
|
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
|
||||||
return func(h *Handle, posts *models.Posts) {
|
|
||||||
for _, call := range calls {
|
|
||||||
call(h, posts)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ListPostPlugins() map[string]func(PostsPlugin, *Handle, *models.Posts) {
|
|
||||||
return maps.Copy(pluginFns)
|
return maps.Copy(pluginFns)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +64,8 @@ func ProjectTitle(t models.Posts) models.Posts {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, *models.Posts)) []func(PostsPlugin, *Handle, *models.Posts) {
|
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
|
||||||
return slice.FilterAndMap(name, func(t string) (func(PostsPlugin, *Handle, *models.Posts), bool) {
|
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
|
||||||
v, ok := m[t]
|
v, ok := m[t]
|
||||||
if ok {
|
if ok {
|
||||||
return v, true
|
return v, true
|
||||||
@ -79,3 +74,38 @@ func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, *
|
|||||||
return nil, false
|
return nil, false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] {
|
||||||
|
return func(post models.Posts) models.Posts {
|
||||||
|
if post.PostExcerpt != "" {
|
||||||
|
plugins.PostExcerpt(&post)
|
||||||
|
} else {
|
||||||
|
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
|
||||||
|
}
|
||||||
|
if len(calls) > 0 {
|
||||||
|
for _, call := range calls {
|
||||||
|
call(&post)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return post
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
|
||||||
|
|
||||||
|
pluginConf := config.GetConfig().ListPagePlugins
|
||||||
|
|
||||||
|
plugin := GetListPostPlugins(pluginConf, m)
|
||||||
|
|
||||||
|
i.ginH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
|
||||||
|
return func(posts models.Posts) models.Posts {
|
||||||
|
for _, fn := range call {
|
||||||
|
fn(&posts)
|
||||||
|
}
|
||||||
|
return posts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -66,7 +66,6 @@ type HandleCall struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
||||||
var inited = false
|
|
||||||
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
|
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
|
||||||
h.components = make(map[string][]Components[string])
|
h.components = make(map[string][]Components[string])
|
||||||
h.handleFns = make(map[int][]HandleCall)
|
h.handleFns = make(map[int][]HandleCall)
|
||||||
@ -74,7 +73,6 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
|||||||
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
|
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
|
||||||
h.ginH = gin.H{}
|
h.ginH = gin.H{}
|
||||||
fn(h)
|
fn(h)
|
||||||
inited = true
|
|
||||||
return *h
|
return *h
|
||||||
})
|
})
|
||||||
m := make(map[string][]Components[string])
|
m := make(map[string][]Components[string])
|
||||||
@ -84,16 +82,10 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
|||||||
m[k] = vv
|
m[k] = vv
|
||||||
}
|
}
|
||||||
h.components = m
|
h.components = m
|
||||||
h.ginH = maps.Copy(hh.ginH)
|
|
||||||
if inited {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.Index.postsPlugin = hh.Index.postsPlugin
|
|
||||||
h.Index.pageEle = hh.Index.pageEle
|
|
||||||
h.Detail.CommentRender = hh.Detail.CommentRender
|
|
||||||
h.handleFns = hh.handleFns
|
h.handleFns = hh.handleFns
|
||||||
h.componentsArgs = hh.componentsArgs
|
h.componentsArgs = hh.componentsArgs
|
||||||
h.componentFilterFn = hh.componentFilterFn
|
h.componentFilterFn = hh.componentFilterFn
|
||||||
|
h.ginH = maps.Copy(hh.ginH)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
|
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
|
||||||
@ -438,3 +430,12 @@ func ExecuteHandleFn(h *Handle) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Render(h *Handle) {
|
||||||
|
switch h.scene {
|
||||||
|
case constraints.Detail:
|
||||||
|
h.Detail.Render()
|
||||||
|
default:
|
||||||
|
h.Index.Render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user