优化代码,实现新的插件机制
This commit is contained in:
parent
f0028e72f4
commit
439a49cf12
|
@ -66,6 +66,13 @@ func Reduce[R, T any](arr []T, fn func(T, R) R, r R) R {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReverseReduce[R, T any](a []T, fn func(T, R) R, r R) R {
|
||||||
|
for i := len(a) - 1; i >= 0; i-- {
|
||||||
|
r = fn(a[i], r)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func Reverse[T any](arr []T) []T {
|
func Reverse[T any](arr []T) []T {
|
||||||
var r = make([]T, 0, len(arr))
|
var r = make([]T, 0, len(arr))
|
||||||
for i := len(arr); i > 0; i-- {
|
for i := len(arr); i > 0; i-- {
|
||||||
|
|
|
@ -840,3 +840,36 @@ func TestShift(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReverseReduce(t *testing.T) {
|
||||||
|
type args[T int, R string] struct {
|
||||||
|
a []T
|
||||||
|
fn func(T, R) R
|
||||||
|
r R
|
||||||
|
}
|
||||||
|
type testCase[T int, R string] struct {
|
||||||
|
name string
|
||||||
|
args args[T, R]
|
||||||
|
want R
|
||||||
|
}
|
||||||
|
tests := []testCase[int, string]{
|
||||||
|
{
|
||||||
|
name: "t1",
|
||||||
|
args: args[int, string]{
|
||||||
|
a: number.Range(1, 10, 1),
|
||||||
|
fn: func(i int, r string) string {
|
||||||
|
return fmt.Sprintf("%s%d", r, i)
|
||||||
|
},
|
||||||
|
r: "",
|
||||||
|
},
|
||||||
|
want: "10987654321",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := ReverseReduce(tt.args.a, tt.args.fn, tt.args.r); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("ReverseReduce() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -29,10 +29,8 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
|
func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
|
||||||
r = make([]T, 0, len(arr))
|
r = make([]T, len(arr))
|
||||||
for _, t := range arr {
|
copy(r, arr)
|
||||||
r = append(r, t)
|
|
||||||
}
|
|
||||||
slice := anyArr[T]{
|
slice := anyArr[T]{
|
||||||
data: r,
|
data: r,
|
||||||
fn: fn,
|
fn: fn,
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"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"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme"
|
"github.com/fthvgb1/wp-go/internal/theme"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -31,6 +32,8 @@ func Detail(c *gin.Context) {
|
||||||
}
|
}
|
||||||
isApproveComment := false
|
isApproveComment := false
|
||||||
status := plugins.Ok
|
status := plugins.Ok
|
||||||
|
pw := sessions.Default(c).Get("post_password")
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
code := http.StatusOK
|
code := http.StatusOK
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,7 +47,14 @@ func Detail(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t := theme.GetTemplateName()
|
t := theme.GetTemplateName()
|
||||||
theme.Hook(t, code, c, ginH, plugins.Detail, status)
|
theme.Hook(t, common.Handle{
|
||||||
|
C: c,
|
||||||
|
GinH: ginH,
|
||||||
|
Password: "",
|
||||||
|
Scene: plugins.Detail,
|
||||||
|
Code: code,
|
||||||
|
Stats: status,
|
||||||
|
})
|
||||||
}()
|
}()
|
||||||
ID := str.ToInteger[uint64](c.Param("id"), 0)
|
ID := str.ToInteger[uint64](c.Param("id"), 0)
|
||||||
|
|
||||||
|
@ -57,7 +67,6 @@ func Detail(c *gin.Context) {
|
||||||
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pw := sessions.Default(c).Get("post_password")
|
|
||||||
showComment := false
|
showComment := false
|
||||||
if post.CommentCount > 0 || post.CommentStatus == "open" {
|
if post.CommentCount > 0 || post.CommentStatus == "open" {
|
||||||
showComment = true
|
showComment = true
|
||||||
|
@ -77,7 +86,6 @@ func Detail(c *gin.Context) {
|
||||||
isApproveComment = true
|
isApproveComment = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post)
|
|
||||||
comments, err := cache.PostComments(c, post.Id)
|
comments, err := cache.PostComments(c, post.Id)
|
||||||
logs.ErrPrintln(err, "get post comment", post.Id)
|
logs.ErrPrintln(err, "get post comment", post.Id)
|
||||||
ginH["comments"] = comments
|
ginH["comments"] = comments
|
||||||
|
|
|
@ -27,16 +27,17 @@ func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
|
||||||
func Feed(c *gin.Context) {
|
func Feed(c *gin.Context) {
|
||||||
if !isCacheExpired(c, cache.FeedCache().GetLastSetTime()) {
|
if !isCacheExpired(c, cache.FeedCache().GetLastSetTime()) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
return
|
||||||
r, err := cache.FeedCache().GetCache(c, time.Second, c)
|
|
||||||
if err != nil {
|
|
||||||
c.Status(http.StatusInternalServerError)
|
|
||||||
c.Abort()
|
|
||||||
c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setFeed(r[0], c, cache.FeedCache().GetLastSetTime())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r, err := cache.FeedCache().GetCache(c, time.Second, c)
|
||||||
|
if err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
c.Abort()
|
||||||
|
c.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setFeed(r[0], c, cache.FeedCache().GetLastSetTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFeed(s string, c *gin.Context, t time.Time) {
|
func setFeed(s string, c *gin.Context, t time.Time) {
|
||||||
|
@ -52,29 +53,29 @@ func PostFeed(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
if !isCacheExpired(c, cache.PostFeedCache().GetLastSetTime(c, id)) {
|
if !isCacheExpired(c, cache.PostFeedCache().GetLastSetTime(c, id)) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
return
|
||||||
s, err := cache.PostFeedCache().GetCache(c, id, time.Second, c, id)
|
|
||||||
if err != nil {
|
|
||||||
c.Status(http.StatusInternalServerError)
|
|
||||||
c.Abort()
|
|
||||||
c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setFeed(s, c, cache.PostFeedCache().GetLastSetTime(c, id))
|
|
||||||
}
|
}
|
||||||
|
s, err := cache.PostFeedCache().GetCache(c, id, time.Second, c, id)
|
||||||
|
if err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
c.Abort()
|
||||||
|
c.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setFeed(s, c, cache.PostFeedCache().GetLastSetTime(c, id))
|
||||||
}
|
}
|
||||||
|
|
||||||
func CommentsFeed(c *gin.Context) {
|
func CommentsFeed(c *gin.Context) {
|
||||||
if !isCacheExpired(c, cache.CommentsFeedCache().GetLastSetTime()) {
|
if !isCacheExpired(c, cache.CommentsFeedCache().GetLastSetTime()) {
|
||||||
c.Status(http.StatusNotModified)
|
c.Status(http.StatusNotModified)
|
||||||
} else {
|
return
|
||||||
r, err := cache.CommentsFeedCache().GetCache(c, time.Second, c)
|
|
||||||
if err != nil {
|
|
||||||
c.Status(http.StatusInternalServerError)
|
|
||||||
c.Abort()
|
|
||||||
c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setFeed(r[0], c, cache.CommentsFeedCache().GetLastSetTime())
|
|
||||||
}
|
}
|
||||||
|
r, err := cache.CommentsFeedCache().GetCache(c, time.Second, c)
|
||||||
|
if err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
c.Abort()
|
||||||
|
c.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setFeed(r[0], c, cache.CommentsFeedCache().GetLastSetTime())
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"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"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme"
|
"github.com/fthvgb1/wp-go/internal/theme"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/fthvgb1/wp-go/model"
|
"github.com/fthvgb1/wp-go/model"
|
||||||
"github.com/fthvgb1/wp-go/plugin/pagination"
|
"github.com/fthvgb1/wp-go/plugin/pagination"
|
||||||
|
@ -49,7 +50,7 @@ type indexHandle struct {
|
||||||
header string
|
header string
|
||||||
paginationStep int
|
paginationStep int
|
||||||
scene int
|
scene int
|
||||||
status int
|
stats int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIndexHandle(ctx *gin.Context) *indexHandle {
|
func newIndexHandle(ctx *gin.Context) *indexHandle {
|
||||||
|
@ -71,7 +72,7 @@ func newIndexHandle(ctx *gin.Context) *indexHandle {
|
||||||
postType: []any{"post"},
|
postType: []any{"post"},
|
||||||
postStatus: []any{"publish"},
|
postStatus: []any{"publish"},
|
||||||
scene: plugins.Home,
|
scene: plugins.Home,
|
||||||
status: plugins.Ok,
|
stats: plugins.Ok,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,20 +234,32 @@ func Index(c *gin.Context) {
|
||||||
"recentComments": recentComments,
|
"recentComments": recentComments,
|
||||||
"posts": posts,
|
"posts": posts,
|
||||||
}
|
}
|
||||||
|
pw := h.session.Get("post_password")
|
||||||
|
pws, ok := pw.(string)
|
||||||
|
if !ok {
|
||||||
|
pws = ""
|
||||||
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
code := http.StatusOK
|
code := http.StatusOK
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code = http.StatusNotFound
|
code = http.StatusNotFound
|
||||||
if h.status == plugins.InternalErr {
|
if h.stats == plugins.InternalErr {
|
||||||
code = http.StatusInternalServerError
|
code = http.StatusInternalServerError
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
h.status = plugins.Error
|
h.stats = plugins.Error
|
||||||
}
|
}
|
||||||
t := theme.GetTemplateName()
|
t := theme.GetTemplateName()
|
||||||
theme.Hook(t, code, c, ginH, h.scene, h.status)
|
theme.Hook(t, common.Handle{
|
||||||
|
C: c,
|
||||||
|
GinH: ginH,
|
||||||
|
Password: pws,
|
||||||
|
Scene: h.scene,
|
||||||
|
Code: code,
|
||||||
|
Stats: h.stats,
|
||||||
|
})
|
||||||
}()
|
}()
|
||||||
err = h.parseParams()
|
err = h.parseParams()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -264,29 +277,15 @@ func Index(c *gin.Context) {
|
||||||
posts, totalRaw, err = cache.PostLists(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.postStatus)
|
posts, totalRaw, err = cache.PostLists(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.postStatus)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.status = plugins.Error
|
h.stats = plugins.Error
|
||||||
logs.ErrPrintln(err, "获取数据错误")
|
logs.ErrPrintln(err, "获取数据错误")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(posts) < 1 && h.category != "" {
|
if len(posts) < 1 && h.category != "" {
|
||||||
h.titleL = "未找到页面"
|
h.titleL = "未找到页面"
|
||||||
h.status = plugins.Empty404
|
h.stats = plugins.Empty404
|
||||||
}
|
}
|
||||||
|
|
||||||
pw := h.session.Get("post_password")
|
|
||||||
plug := plugins.NewPostPlugin(c, h.scene)
|
|
||||||
for i, post := range posts {
|
|
||||||
if post.PostPassword != "" {
|
|
||||||
plugins.PasswordProjectTitle(&posts[i])
|
|
||||||
if pw != post.PostPassword {
|
|
||||||
plugins.PasswdProjectContent(&posts[i])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
plugins.ApplyPlugin(plug, &posts[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
q := c.Request.URL.Query().Encode()
|
q := c.Request.URL.Query().Encode()
|
||||||
if q != "" {
|
if q != "" {
|
||||||
q = fmt.Sprintf("?%s", q)
|
q = fmt.Sprintf("?%s", q)
|
||||||
|
|
2
internal/pkg/cache/feed.go
vendored
2
internal/pkg/cache/feed.go
vendored
|
@ -25,7 +25,7 @@ func InitFeed() {
|
||||||
AtomLink: fmt.Sprintf("%s/feed", wpconfig.Options.Value("home")),
|
AtomLink: fmt.Sprintf("%s/feed", wpconfig.Options.Value("home")),
|
||||||
Link: wpconfig.Options.Value("siteurl"),
|
Link: wpconfig.Options.Value("siteurl"),
|
||||||
Description: wpconfig.Options.Value("blogdescription"),
|
Description: wpconfig.Options.Value("blogdescription"),
|
||||||
Language: "zh-CN",
|
Language: wpconfig.GetLang(),
|
||||||
UpdatePeriod: "hourly",
|
UpdatePeriod: "hourly",
|
||||||
UpdateFrequency: 1,
|
UpdateFrequency: 1,
|
||||||
Generator: wpconfig.Options.Value("home"),
|
Generator: wpconfig.Options.Value("home"),
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/plugin/digest"
|
"github.com/fthvgb1/wp-go/plugin/digest"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -30,7 +29,7 @@ func FlushCache() {
|
||||||
func digestRaw(arg ...any) (string, error) {
|
func digestRaw(arg ...any) (string, error) {
|
||||||
str := arg[0].(string)
|
str := arg[0].(string)
|
||||||
id := arg[1].(uint64)
|
id := arg[1].(uint64)
|
||||||
limit := config.GetConfig().DigestWordCount
|
limit := arg[2].(int)
|
||||||
if limit < 0 {
|
if limit < 0 {
|
||||||
return str, nil
|
return str, nil
|
||||||
} else if limit == 0 {
|
} else if limit == 0 {
|
||||||
|
@ -39,20 +38,11 @@ func digestRaw(arg ...any) (string, error) {
|
||||||
return digest.Raw(str, limit, fmt.Sprintf("/p/%d", id)), nil
|
return digest.Raw(str, limit, fmt.Sprintf("/p/%d", id)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Digest(p *Plugin[models.Posts], c *gin.Context, post *models.Posts, scene int) {
|
func Digest(ctx context.Context, post *models.Posts, limit int) {
|
||||||
if scene == Detail {
|
content, _ := digestCache.GetCache(ctx, post.Id, time.Second, post.PostContent, post.Id, limit)
|
||||||
return
|
post.PostContent = content
|
||||||
}
|
|
||||||
if post.PostExcerpt != "" {
|
|
||||||
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
|
|
||||||
} else {
|
|
||||||
post.PostContent = DigestCache(c, post.Id, post.PostContent)
|
|
||||||
|
|
||||||
}
|
|
||||||
p.Next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DigestCache(ctx *gin.Context, id uint64, str string) string {
|
func PostExcerpt(post *models.Posts) {
|
||||||
content, _ := digestCache.GetCache(ctx, id, time.Second, str, id)
|
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
|
||||||
return content
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,18 +35,3 @@ type Plugin[T any] struct {
|
||||||
scene int
|
scene int
|
||||||
c *gin.Context
|
c *gin.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlugin[T any](calls []Func[T], index int, post *T, scene int, c *gin.Context) *Plugin[T] {
|
|
||||||
return &Plugin[T]{calls: calls, index: index, post: post, scene: scene, c: c}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin[T]) Push(call ...Func[T]) {
|
|
||||||
p.calls = append(p.calls, call...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin[T]) Next() {
|
|
||||||
p.index++
|
|
||||||
for ; p.index < len(p.calls); p.index++ {
|
|
||||||
p.calls[p.index](p, p.c, p.post, p.scene)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,21 +3,8 @@ package plugins
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPostPlugin(ctx *gin.Context, scene int) *Plugin[models.Posts] {
|
|
||||||
p := NewPlugin[models.Posts](nil, -1, nil, scene, ctx)
|
|
||||||
p.Push(Digest)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func ApplyPlugin(p *Plugin[models.Posts], post *models.Posts) {
|
|
||||||
p.post = post
|
|
||||||
p.Next()
|
|
||||||
p.index = -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func PasswordProjectTitle(post *models.Posts) {
|
func PasswordProjectTitle(post *models.Posts) {
|
||||||
post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle)
|
post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle)
|
||||||
}
|
}
|
||||||
|
|
55
internal/theme/common/common.go
Normal file
55
internal/theme/common/common.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handle struct {
|
||||||
|
C *gin.Context
|
||||||
|
GinH gin.H
|
||||||
|
Password string
|
||||||
|
Scene int
|
||||||
|
Code int
|
||||||
|
Stats int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Fn[T any] func(T) T
|
||||||
|
type Plugin[T any] func(next Fn[T], h Handle, t T) T
|
||||||
|
|
||||||
|
func PluginFn[T any](a []Plugin[T], h Handle, fn Fn[T]) Fn[T] {
|
||||||
|
return slice.ReverseReduce(a, func(next Plugin[T], forward Fn[T]) Fn[T] {
|
||||||
|
return func(t T) T {
|
||||||
|
return next(forward, h, t)
|
||||||
|
}
|
||||||
|
}, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Default[T any](t T) T {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r models.Posts) {
|
||||||
|
r = post
|
||||||
|
if post.PostPassword != "" {
|
||||||
|
plugins.PasswordProjectTitle(&r)
|
||||||
|
if h.Password != post.PostPassword {
|
||||||
|
plugins.PasswdProjectContent(&r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r = next(r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
|
||||||
|
if post.PostExcerpt != "" {
|
||||||
|
plugins.PostExcerpt(&post)
|
||||||
|
} else {
|
||||||
|
plugins.Digest(h.C, &post, config.GetConfig().DigestWordCount)
|
||||||
|
}
|
||||||
|
return next(post)
|
||||||
|
}
|
|
@ -1,30 +1,25 @@
|
||||||
package theme
|
package theme
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
||||||
"github.com/fthvgb1/wp-go/plugin/pagination"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var themeMap = map[string]func(int, *gin.Context, gin.H, int, int){}
|
var themeMap = map[string]func(handle common.Handle){}
|
||||||
|
|
||||||
func addThemeHookFunc(name string, fn func(int, *gin.Context, gin.H, int, int)) {
|
func addThemeHookFunc(name string, fn func(handle common.Handle)) {
|
||||||
if _, ok := themeMap[name]; ok {
|
if _, ok := themeMap[name]; ok {
|
||||||
panic("exists same name theme")
|
panic("exists same name theme")
|
||||||
}
|
}
|
||||||
themeMap[name] = fn
|
themeMap[name] = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
func Hook(themeName string, code int, c *gin.Context, h gin.H, scene, status int) {
|
func Hook(themeName string, handle common.Handle) {
|
||||||
fn, ok := themeMap[themeName]
|
fn, ok := themeMap[themeName]
|
||||||
if ok && fn != nil {
|
if ok && fn != nil {
|
||||||
fn(code, c, h, scene, status)
|
fn(handle)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := plugins.IndexSceneMap[scene]; ok {
|
/*if _, ok := plugins.IndexSceneMap[scene]; ok {
|
||||||
p, ok := h["pagination"]
|
p, ok := h["pagination"]
|
||||||
if ok {
|
if ok {
|
||||||
pp, ok := p.(pagination.ParsePagination)
|
pp, ok := p.(pagination.ParsePagination)
|
||||||
|
@ -39,5 +34,5 @@ func Hook(themeName string, code int, c *gin.Context, h gin.H, scene, status int
|
||||||
c.HTML(code, "twentyfifteen/posts/detail.gohtml", h)
|
c.HTML(code, "twentyfifteen/posts/detail.gohtml", h)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(errors.New("what happening"), " how reached here", themeName, code, h, scene, status)
|
logs.ErrPrintln(errors.New("what happening"), " how reached here", themeName, code, h, scene, status)*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"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"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||||
"github.com/fthvgb1/wp-go/plugin/pagination"
|
"github.com/fthvgb1/wp-go/plugin/pagination"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -26,53 +27,52 @@ var paginate = func() plugins.PageEle {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
type handle struct {
|
type handle struct {
|
||||||
c *gin.Context
|
common.Handle
|
||||||
ginH gin.H
|
templ string
|
||||||
scene int
|
|
||||||
status int
|
|
||||||
stats int
|
|
||||||
templ string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Hook(status int, c *gin.Context, ginH gin.H, scene, stats int) {
|
func Hook(h2 common.Handle) {
|
||||||
h := handle{
|
h := handle{
|
||||||
c: c,
|
Handle: h2,
|
||||||
ginH: ginH,
|
|
||||||
scene: scene,
|
|
||||||
status: status,
|
|
||||||
stats: stats,
|
|
||||||
templ: "twentyseventeen/posts/index.gohtml",
|
templ: "twentyseventeen/posts/index.gohtml",
|
||||||
}
|
}
|
||||||
ginH["HeaderImage"] = h.getHeaderImage(c)
|
h.GinH["HeaderImage"] = h.getHeaderImage(h.C)
|
||||||
if stats == plugins.Empty404 {
|
if h.Stats == plugins.Empty404 {
|
||||||
c.HTML(status, h.templ, ginH)
|
h.C.HTML(h.Code, h.templ, h.GinH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if scene == plugins.Detail {
|
if h.Scene == plugins.Detail {
|
||||||
h.detail()
|
h.detail()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.index()
|
h.index()
|
||||||
|
}
|
||||||
|
|
||||||
|
var plugin = []common.Plugin[models.Posts]{
|
||||||
|
common.PasswordProject, common.Digest,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h handle) index() {
|
func (h handle) index() {
|
||||||
posts := h.ginH["posts"].([]models.Posts)
|
if h.Stats != plugins.Empty404 {
|
||||||
p, ok := h.ginH["pagination"]
|
posts := h.GinH["posts"].([]models.Posts)
|
||||||
if ok {
|
posts = slice.Map(posts, common.PluginFn(plugin, h.Handle, common.Default[models.Posts]))
|
||||||
pp, ok := p.(pagination.ParsePagination)
|
p, ok := h.GinH["pagination"]
|
||||||
if ok {
|
if ok {
|
||||||
h.ginH["pagination"] = pagination.Paginate(paginate, pp)
|
pp, ok := p.(pagination.ParsePagination)
|
||||||
|
if ok {
|
||||||
|
h.GinH["pagination"] = pagination.Paginate(paginate, pp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
h.GinH["posts"] = h.postThumbnail(posts, h.Scene)
|
||||||
}
|
}
|
||||||
h.ginH["bodyClass"] = h.bodyClass()
|
|
||||||
h.ginH["posts"] = h.postThumbnail(posts, h.scene)
|
h.GinH["bodyClass"] = h.bodyClass()
|
||||||
h.c.HTML(h.status, h.templ, h.ginH)
|
h.C.HTML(h.Code, h.templ, h.GinH)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h handle) detail() {
|
func (h handle) detail() {
|
||||||
post := h.ginH["post"].(models.Posts)
|
post := h.GinH["post"].(models.Posts)
|
||||||
h.ginH["bodyClass"] = h.bodyClass()
|
h.GinH["bodyClass"] = h.bodyClass()
|
||||||
//host, _ := wpconfig.Options.Load("siteurl")
|
//host, _ := wpconfig.Options.Load("siteurl")
|
||||||
host := ""
|
host := ""
|
||||||
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
|
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
|
||||||
|
@ -81,12 +81,12 @@ func (h handle) detail() {
|
||||||
img.Sizes = "100vw"
|
img.Sizes = "100vw"
|
||||||
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
|
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
|
||||||
post.Thumbnail = img
|
post.Thumbnail = img
|
||||||
h.ginH["post"] = post
|
h.GinH["post"] = post
|
||||||
comments := h.ginH["comments"].([]models.Comments)
|
comments := h.GinH["comments"].([]models.Comments)
|
||||||
dep := h.ginH["maxDep"].(int)
|
dep := h.GinH["maxDep"].(int)
|
||||||
h.ginH["comments"] = plugins.FormatComments(h.c, comment{}, comments, dep)
|
h.GinH["comments"] = plugins.FormatComments(h.C, comment{}, comments, dep)
|
||||||
h.templ = "twentyseventeen/posts/detail.gohtml"
|
h.templ = "twentyseventeen/posts/detail.gohtml"
|
||||||
h.c.HTML(h.status, h.templ, h.ginH)
|
h.C.HTML(h.Code, h.templ, h.GinH)
|
||||||
}
|
}
|
||||||
|
|
||||||
type comment struct {
|
type comment struct {
|
||||||
|
@ -137,18 +137,18 @@ func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
|
||||||
|
|
||||||
func (h handle) bodyClass() string {
|
func (h handle) bodyClass() string {
|
||||||
s := ""
|
s := ""
|
||||||
switch h.scene {
|
switch h.Scene {
|
||||||
case plugins.Search:
|
case plugins.Search:
|
||||||
s = "search-no-results"
|
s = "search-no-results"
|
||||||
if len(h.ginH["posts"].([]models.Posts)) > 0 {
|
if len(h.GinH["posts"].([]models.Posts)) > 0 {
|
||||||
s = "search-results"
|
s = "search-results"
|
||||||
}
|
}
|
||||||
case plugins.Category, plugins.Tag:
|
case plugins.Category, plugins.Tag:
|
||||||
cat := h.c.Param("category")
|
cat := h.C.Param("category")
|
||||||
if cat == "" {
|
if cat == "" {
|
||||||
cat = h.c.Param("tag")
|
cat = h.C.Param("tag")
|
||||||
}
|
}
|
||||||
_, cate := slice.SearchFirst(cache.CategoriesTags(h.c, h.scene), func(my models.TermsMy) bool {
|
_, cate := slice.SearchFirst(cache.CategoriesTags(h.C, h.Scene), func(my models.TermsMy) bool {
|
||||||
return my.Name == cat
|
return my.Name == cat
|
||||||
})
|
})
|
||||||
if cate.Slug[0] != '%' {
|
if cate.Slug[0] != '%' {
|
||||||
|
@ -156,9 +156,9 @@ func (h handle) bodyClass() string {
|
||||||
}
|
}
|
||||||
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
|
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
|
||||||
case plugins.Detail:
|
case plugins.Detail:
|
||||||
s = fmt.Sprintf("postid-%d", h.ginH["post"].(models.Posts).Id)
|
s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id)
|
||||||
}
|
}
|
||||||
return str.Join(class[h.scene], s)
|
return str.Join(class[h.Scene], s)
|
||||||
}
|
}
|
||||||
|
|
||||||
var class = map[int]string{
|
var class = map[int]string{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user