Compare commits

..

No commits in common. "da7dd410cd31906ca2f4c91b016a74d1bc6478fb" and "43754377e8d3c92ea081c5f3d0136cab9482b7c0" have entirely different histories.

10 changed files with 208 additions and 267 deletions

View File

@ -37,7 +37,7 @@ type Selectors struct {
}
func EnlighterJS(h *wp.Handle) {
h.PushGroupHeadScript("enlighterjs-css", 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 {
op := wpconfig.GetOption("enlighter-options")

View File

@ -67,7 +67,6 @@ func configs(h *wp.Handle) {
h.CommonComponents()
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
components.WidgetArea(h)
wp.ReplyCommentJs(h)
h.SetData("customHeader", customHeader(h))
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender"))

View File

@ -7,14 +7,14 @@ import (
)
func pushScripts(h *wp.Handle) {
h.PushCacheGroupHeadScript("{theme}.head", 30, func(h *wp.Handle) string {
h.PushCacheGroupHeadScript("head", 30, func(h *wp.Handle) string {
head := headScript
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
head = fmt.Sprintf("%s\n%s", headScript, ` <link rel="stylesheet" id="twentyseventeen-colors-dark-css" href="/wp-content/themes/twentyseventeen/assets/css/colors-dark.css?ver=20191025" media="all">`)
}
return head
})
h.PushGroupFooterScript("{theme}.footer", 20, footerScript)
h.PushGroupFooterScript(20, footerScript)
}

View File

@ -69,7 +69,6 @@ func configs(h *wp.Handle) {
)
h.CommonComponents()
h.Index.SetPageEle(paginate)
wp.ReplyCommentJs(h)
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender"))

View File

@ -1,200 +0,0 @@
package wp
import (
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"strings"
)
func (h *Handle) DeleteComponents(scene, name string) {
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
return c, c.Name != name
})
}
func (h *Handle) ReplaceComponents(scene, name string, components Components[string]) {
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
if c.Name == name {
c = components
}
return c, true
})
}
func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Components[string], bool)) {
h.componentHook[scene] = append(h.componentHook[scene], fn)
}
func CalComponents(h *Handle) {
for k, components := range h.components {
key := str.Join("calComponents-", k)
key = h.ComponentFilterFnHook("calComponents", key, k)
ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] {
r := slice.FilterAndMap(components, func(t Components[string]) (Components[string], bool) {
fns, ok := h.componentHook[k]
if !ok {
return t, true
}
for _, fn := range fns {
c, ok := fn(t)
if !ok {
return c, false
}
t = c
}
return t, true
})
slice.Sort(r, func(i, j Components[string]) bool {
return i.Order > j.Order
})
return r
})
var s = make([]string, 0, len(ss))
for _, component := range ss {
if component.Val != "" {
s = append(s, component.Val)
continue
}
if component.Fn != nil {
v := ""
if component.Cached {
v = reload.GetAnyValMapBy("cacheComponents", component.Name, h, component.Fn)
} else {
v = component.Fn(h)
}
if v != "" {
s = append(s, v)
}
}
}
h.ginH[k] = strings.Join(s, "\n")
}
}
func (h *Handle) PushComponents(name string, components ...Components[string]) {
h.components[name] = append(h.components[name], components...)
}
func (h *Handle) PushGroupComponentStr(componentType, name string, order int, strs ...string) {
var calls []Components[string]
for _, val := range strs {
calls = append(calls, Components[string]{
Val: val,
Order: order,
Name: name,
})
}
h.components[componentType] = append(h.components[componentType], calls...)
}
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
}
func (h *Handle) PushFooterScript(components ...Components[string]) {
h.PushComponents(constraints.FooterScript, components...)
}
func (h *Handle) PushGroupFooterScript(name string, order int, strs ...string) {
h.PushGroupComponentStr(constraints.FooterScript, name, order, strs...)
}
func (h *Handle) PushCacheGroupFooterScript(name string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.FooterScript, name, order, fns...)
}
func (h *Handle) PushGroupCacheComponentFn(componentType, name string, order int, fns ...func(*Handle) string) {
h.PushComponents(componentType, h.NewComponent(name, true, order, func(h *Handle) string {
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
return t(h)
}), "\n")
}))
}
func (h *Handle) NewComponent(name string, cached bool, order int, fn func(handle *Handle) string) Components[string] {
return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order}
}
func (h *Handle) AddCacheComponent(componentType, name string, order int, fn func(*Handle) string) {
h.components[componentType] = append(h.components[componentType], h.NewComponent(name, true, order, fn))
}
func (h *Handle) PushHeadScript(components ...Components[string]) {
h.PushComponents(constraints.HeadScript, components...)
}
func (h *Handle) PushGroupHeadScript(name string, order int, str ...string) {
h.PushGroupComponentStr(constraints.HeadScript, name, order, str...)
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
v, ok := h.componentsArgs[k]
if ok {
vv, ok := v.(T)
if ok {
return vv
}
}
return defaults
}
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
val, ok := h.componentsArgs[name]
if !ok {
var vv []T
vv = append(vv, v...)
h.componentsArgs[name] = vv
return
}
vv, ok := val.([]T)
if ok {
vv = append(vv, v...)
h.componentsArgs[name] = vv
}
}
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
val, ok := h.componentsArgs[name]
if !ok {
vv := make(map[K]V)
vv[key] = v
h.componentsArgs[name] = vv
return
}
vv, ok := val.(map[K]V)
if ok {
vv[key] = v
h.componentsArgs[name] = vv
}
}
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
val, ok := h.componentsArgs[name]
if !ok {
h.componentsArgs[name] = m
return
}
vv, ok := val.(map[K]V)
if ok {
h.componentsArgs[name] = maps.Merge(vv, m)
}
}
func (h *Handle) SetComponentsArgs(key string, value any) {
h.componentsArgs[key] = value
}
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
fn, ok := h.componentFilterFn[name]
return fn, ok
}
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) {
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
}
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
calls, ok := h.componentFilterFn[name]
if ok {
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
return fn(h, r, args...)
}, s)
}
return s
}

View File

@ -10,18 +10,17 @@ import (
)
var widgetFn = map[string]widgetComponent{
"search": {fn: widget.Search, name: "search"},
"recent-posts": {fn: widget.RecentPosts, name: "recent-posts"},
"recent-comments": {fn: widget.RecentComments, name: "recent-comments"},
"archives": {fn: widget.Archive, name: "archives"},
"categories": {fn: widget.Category, name: "categories"},
"meta": {fn: widget.Meta, name: "meta", cached: true},
"search": {fn: widget.Search},
"recent-posts": {fn: widget.RecentPosts},
"recent-comments": {fn: widget.RecentComments},
"archives": {fn: widget.Archive},
"categories": {fn: widget.Category},
"meta": {fn: widget.Meta, cacheKey: "widgetMeta"},
}
type widgetComponent struct {
fn func(h *wp.Handle, id string) string
cached bool
name string
fn func(h *wp.Handle, id string) string
cacheKey string
}
func WidgetArea(h *wp.Handle) {
@ -48,8 +47,7 @@ func sidebars() []wp.Components[string] {
component.Fn = fn
} else {
component.Fn = widget.Fn(id, widgetComponents.fn)
component.Name = widgetComponents.name
component.Cached = widgetComponents.cached
component.CacheKey = widgetComponents.cacheKey
}
component.Order = 10
return component, true

View File

@ -107,18 +107,13 @@ func DetailRender(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)
}
func Details(h *Handle) {
_ = h.Detail.BuildDetailData()
}
func ReplyCommentJs(h *Handle) {
h.PushFooterScript(h.NewComponent("comment-reply.js", false, 10, func(h *Handle) string {
reply := ""
if h.Detail.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
}
return reply
}))
}

View File

@ -93,17 +93,17 @@ func NewIndexParams(ctx *gin.Context) *IndexParams {
PostStatus: []any{"publish"},
BlogName: wpconfig.GetOption("blogname"),
}
i.ParseSearch = i.ParseSearchs
i.ParseArchive = i.ParseArchives
i.ParseCategory = i.ParseCategorys
i.ParseTag = i.ParseTags
i.CategoryCondition = i.CategoryConditions
i.ParseAuthor = i.ParseAuthors
i.ParseParams = i.ParseParamss
i.ParseSearch = i.parseSearch
i.ParseArchive = i.parseArchive
i.ParseCategory = i.parseCategory
i.ParseTag = i.parseTag
i.CategoryCondition = i.categoryCondition
i.ParseAuthor = i.parseAuthor
i.ParseParams = i.parseParams
return i
}
func (i *IndexParams) ParseSearchs() {
func (i *IndexParams) parseSearch() {
s := i.Ctx.Query("s")
if s != "" {
q := str.Join("%", s, "%")
@ -118,7 +118,7 @@ func (i *IndexParams) ParseSearchs() {
i.Search = s
}
}
func (i *IndexParams) ParseArchives() error {
func (i *IndexParams) parseArchive() error {
year := i.Ctx.Param("year")
if year != "" {
y := str.ToInteger(year, -1)
@ -148,7 +148,7 @@ func (i *IndexParams) ParseArchives() error {
return nil
}
func (i *IndexParams) ParseCategorys() error {
func (i *IndexParams) parseCategory() error {
category := i.Ctx.Param("category")
if category != "" {
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Category), category) {
@ -161,7 +161,7 @@ func (i *IndexParams) ParseCategorys() error {
}
return nil
}
func (i *IndexParams) ParseTags() error {
func (i *IndexParams) parseTag() error {
tag := i.Ctx.Param("tag")
if tag != "" {
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Tag), tag) {
@ -175,7 +175,7 @@ func (i *IndexParams) ParseTags() error {
return nil
}
func (i *IndexParams) CategoryConditions() {
func (i *IndexParams) categoryCondition() {
if i.Category != "" {
i.Where = append(i.Where, []string{
"d.name", i.Category,
@ -190,7 +190,7 @@ func (i *IndexParams) CategoryConditions() {
i.setTitleLR(i.Category, i.BlogName)
}
}
func (i *IndexParams) ParseAuthors() (err error) {
func (i *IndexParams) parseAuthor() (err error) {
username := i.Ctx.Param("author")
if username != "" {
allUsername, er := cache.GetAllUsername(i.Ctx)
@ -215,7 +215,7 @@ func (i *IndexParams) ParseAuthors() (err error) {
return
}
func (i *IndexParams) ParseParamss() {
func (i *IndexParams) parseParams() {
i.Order = i.Ctx.Query("Order")
if !maps.IsExists(orders, i.Order) {
order := config.GetConfig().PostOrder

View File

@ -1,8 +1,8 @@
package wp
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
)
@ -65,8 +65,7 @@ func PipeHandle(pipeScene string, keyFn func(*Handle, string) string, fn func(*H
}
func PipeKey(h *Handle, pipScene string) string {
key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats)
return h.ComponentFilterFnHook("pipeKey", key, pipScene)
return fmt.Sprintf("pipekey-%s-%s-%s", pipScene, h.scene, h.scene)
}
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall) (handlers []HandleCall) {

View File

@ -2,6 +2,7 @@ package wp
import (
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
@ -11,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
"html/template"
"net/http"
"strings"
)
type Handle struct {
@ -26,7 +28,6 @@ type Handle struct {
Stats string
templ string
components map[string][]Components[string]
componentHook map[string][]func(Components[string]) (Components[string], bool)
themeMods wpconfig.ThemeMods
handlers map[string]map[string][]HandleCall
handleHook map[string][]func(HandleCall) (HandleCall, bool)
@ -38,22 +39,6 @@ type Handle struct {
template *template.Template
}
func (h *Handle) Components() map[string][]Components[string] {
return h.components
}
func (h *Handle) ComponentHook() map[string][]func(Components[string]) (Components[string], bool) {
return h.componentHook
}
func (h *Handle) Handlers() map[string]map[string][]HandleCall {
return h.handlers
}
func (h *Handle) HandleHook() map[string][]func(HandleCall) (HandleCall, bool) {
return h.handleHook
}
func (h *Handle) SetTemplate(template *template.Template) {
h.template = template
}
@ -66,11 +51,10 @@ type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行
type Components[T any] struct {
Name string
Val T
Fn func(*Handle) T
Order int
Cached bool
Val T
Fn func(*Handle) T
Order int
CacheKey string
}
type HandleFn[T any] func(T)
@ -94,6 +78,13 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
inited = true
return *h
})
m := make(map[string][]Components[string])
for k, v := range hh.components {
vv := make([]Components[string], len(v))
copy(vv, v)
m[k] = vv
}
h.components = m
h.ginH = maps.Copy(hh.ginH)
h.ginH["calPostClass"] = postClass(h)
h.ginH["calBodyClass"] = bodyClass(h)
@ -101,17 +92,33 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
if inited {
return
}
h.components = hh.components
h.Index.postsPlugin = hh.Index.postsPlugin
h.Index.pageEle = hh.Index.pageEle
h.Detail.CommentRender = hh.Detail.CommentRender
h.handlers = hh.handlers
h.handleHook = hh.handleHook
h.componentHook = hh.componentHook
h.componentsArgs = hh.componentsArgs
h.componentFilterFn = hh.componentFilterFn
}
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
fn, ok := h.componentFilterFn[name]
return fn, ok
}
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) {
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
}
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
calls, ok := h.componentFilterFn[name]
if ok {
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
return fn(h, r, args...)
}, s)
}
return s
}
func (h *Handle) Abort() {
h.abort = true
}
@ -150,6 +157,61 @@ func (h *Handle) SetData(k string, v any) {
h.ginH[k] = v
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
v, ok := h.componentsArgs[k]
if ok {
vv, ok := v.(T)
if ok {
return vv
}
}
return defaults
}
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
val, ok := h.componentsArgs[name]
if !ok {
var vv []T
vv = append(vv, v...)
h.componentsArgs[name] = vv
return
}
vv, ok := val.([]T)
if ok {
vv = append(vv, v...)
h.componentsArgs[name] = vv
}
}
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
val, ok := h.componentsArgs[name]
if !ok {
vv := make(map[K]V)
vv[key] = v
h.componentsArgs[name] = vv
return
}
vv, ok := val.(map[K]V)
if ok {
vv[key] = v
h.componentsArgs[name] = vv
}
}
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
val, ok := h.componentsArgs[name]
if !ok {
h.componentsArgs[name] = m
return
}
vv, ok := val.(map[K]V)
if ok {
h.componentsArgs[name] = maps.Merge(vv, m)
}
}
func (h *Handle) SetComponentsArgs(key string, value any) {
h.componentsArgs[key] = value
}
func NewHandle(c *gin.Context, scene string, theme string) *Handle {
mods, err := wpconfig.GetThemeMods(theme)
logs.IfError(err, "获取mods失败")
@ -163,6 +225,43 @@ func NewHandle(c *gin.Context, scene string, theme string) *Handle {
}
}
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components[string] {
return Components[string]{Fn: fn, CacheKey: name, Order: order}
}
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
h.components[name] = append(h.components[name], h.NewCacheComponent(name, 10, fn))
}
func (h *Handle) PushHeadScript(fn ...Components[string]) {
h.PushComponents(constraints.HeadScript, fn...)
}
func (h *Handle) PushGroupHeadScript(order int, str ...string) {
h.PushGroupComponentStrs(constraints.HeadScript, order, str...)
}
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
}
func (h *Handle) PushFooterScript(fn ...Components[string]) {
h.PushComponents(constraints.FooterScript, fn...)
}
func (h *Handle) PushGroupFooterScript(order int, fns ...string) {
h.PushGroupComponentStrs(constraints.FooterScript, order, fns...)
}
func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.FooterScript, key, order, fns...)
}
func (h *Handle) PushGroupCacheComponentFn(name, key string, order int, fns ...func(*Handle) string) {
h.PushComponents(name, h.NewCacheComponent(key, order, func(h *Handle) string {
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
return t(h)
}), "\n")
}))
}
func (h *Handle) GetPassword() {
pw := h.Session.Get("post_password")
if pw != nil {
@ -203,6 +302,58 @@ func RenderTemplate(h *Handle) {
h.StopPipe()
}
func (h *Handle) PushComponents(name string, components ...Components[string]) {
h.components[name] = append(h.components[name], components...)
}
func (h *Handle) PushGroupComponentStrs(name string, order int, str ...string) {
var calls []Components[string]
for _, fn := range str {
calls = append(calls, Components[string]{
Val: fn,
Order: order,
})
}
h.components[name] = append(h.components[name], calls...)
}
func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) {
var calls []Components[string]
for _, fn := range fns {
calls = append(calls, Components[string]{
Fn: fn,
Order: order,
})
}
h.components[name] = append(h.components[name], calls...)
}
func CalComponents(h *Handle) {
for k, ss := range h.components {
slice.Sort(ss, func(i, j Components[string]) bool {
return i.Order > j.Order
})
var s []string
for _, component := range ss {
if component.Val != "" {
s = append(s, component.Val)
continue
}
if component.Fn != nil {
v := ""
if component.CacheKey != "" {
v = reload.GetAnyValMapBy("calComponent", component.CacheKey, h, component.Fn)
} else {
v = component.Fn(h)
}
if v != "" {
s = append(s, v)
}
}
}
h.ginH[k] = strings.Join(s, "\n")
}
}
func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
return HandleCall{Fn: fn, Order: order, Name: name}
}