diff --git a/internal/actions/detail.go b/internal/actions/detail.go
index 7d1a863..7b898ec 100644
--- a/internal/actions/detail.go
+++ b/internal/actions/detail.go
@@ -4,16 +4,13 @@ import (
"fmt"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"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/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
- "sort"
"strconv"
- "strings"
)
type detailHandler struct {
@@ -22,9 +19,6 @@ type detailHandler struct {
func Detail(c *gin.Context) {
var err error
- hh := detailHandler{
- c,
- }
recent := cache.RecentPosts(c, 5)
archive := cache.Archives(c)
categoryItems := cache.Categories(c)
@@ -92,7 +86,7 @@ func Detail(c *gin.Context) {
plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post)
comments, err := cache.PostComments(c, post.Id)
logs.ErrPrintln(err, "get post comment", post.Id)
- commentss := treeComments(comments)
+ ginH["comments"] = comments
prev, next, err := cache.GetContextPost(c, post.Id, post.PostDate)
logs.ErrPrintln(err, "get pre and next post", post.Id, post.PostDate)
ginH["title"] = fmt.Sprintf("%s-%s", post.PostTitle, wpconfig.Options.Value("blogname"))
@@ -105,158 +99,8 @@ func Detail(c *gin.Context) {
logs.ErrPrintln(err, "get comment depth ", depth)
d = 5
}
- ginH["comments"] = hh.formatComment(commentss, 1, d)
+ ginH["maxDep"] = d
ginH["next"] = next
ginH["user"] = user
ginH["scene"] = plugins.Detail
}
-
-type Comment struct {
- models.Comments
- Children []*Comment
-}
-
-type Comments []*Comment
-
-func (c Comments) Len() int {
- return len(c)
-}
-
-func (c Comments) Less(i, j int) bool {
- return c[i].CommentId < c[j].CommentId
-}
-
-func (c Comments) Swap(i, j int) {
- c[i], c[j] = c[j], c[i]
-}
-
-func (d detailHandler) formatComment(comments Comments, depth, maxDepth int) (html string) {
- s := strings.Builder{}
- if depth > maxDepth {
- comments = findComments(comments)
- }
- sort.Sort(comments)
- for i, comment := range comments {
- eo := "even"
- if (i+1)%2 == 0 {
- eo = "odd"
- }
- p := ""
- fl := false
- if len(comment.Children) > 0 && depth < maxDepth+1 {
- p = "parent"
- fl = true
- }
- s.WriteString(d.formatLi(comment.Comments, depth, eo, p))
- if fl {
- depth++
- s.WriteString(`
`)
- s.WriteString(d.formatComment(comment.Children, depth, maxDepth))
- s.WriteString(`
`)
- }
- s.WriteString("")
- i++
- if i >= len(comments) {
- break
- }
- }
-
- html = s.String()
- return
-}
-
-func findComments(comments Comments) Comments {
- var r Comments
- for _, comment := range comments {
- tmp := *comment
- tmp.Children = nil
- r = append(r, &tmp)
- if len(comment.Children) > 0 {
- t := findComments(comment.Children)
- r = append(r, t...)
- }
- }
- return r
-}
-
-func treeComments(comments []models.Comments) Comments {
- var r = map[uint64]*Comment{
- 0: {
- Comments: models.Comments{},
- },
- }
- var top []*Comment
- for _, comment := range comments {
- c := Comment{
- Comments: comment,
- Children: []*Comment{},
- }
- r[comment.CommentId] = &c
- if comment.CommentParent == 0 {
- top = append(top, &c)
- }
- }
- for id, son := range r {
- if id == son.CommentParent {
- continue
- }
- parent := r[son.CommentParent]
- parent.Children = append(parent.Children, son)
- }
- return top
-}
-
-func (d detailHandler) formatLi(comments models.Comments, depth int, eo, parent string) string {
- li := `
-")
+ }
+
+ html = s.String()
+ return
+}
+
+func (d CommentHandler) findComments(comments []*Comments) []*Comments {
+ var r []*Comments
+ for _, comment := range comments {
+ tmp := *comment
+ comment.Children = nil
+ r = append(r, &tmp)
+ if len(comment.Children) > 0 {
+ t := d.findComments(comment.Children)
+ r = append(r, t...)
+ }
+ }
+ return r
+}
+
+func treeComments(comments []models.Comments) []*Comments {
+ var r = map[uint64]*Comments{
+ 0: {
+ Comments: models.Comments{},
+ },
+ }
+ var top []*Comments
+ for _, comment := range comments {
+ c := Comments{
+ Comments: comment,
+ Children: []*Comments{},
+ }
+ r[comment.CommentId] = &c
+ if comment.CommentParent == 0 {
+ top = append(top, &c)
+ }
+ }
+ for id, son := range r {
+ if id == son.CommentParent {
+ continue
+ }
+ parent := r[son.CommentParent]
+ parent.Children = append(parent.Children, son)
+ }
+ return top
+}
+
+func CommonLi() string {
+ return li
+}
+
+var commonCommentFormat = CommonCommentFormat{}
+
+func CommentRender() CommonCommentFormat {
+ return commonCommentFormat
+}
+
+type CommonCommentFormat struct {
+}
+
+func (c CommonCommentFormat) Sort(i, j *Comments) bool {
+ return i.CommentDate.UnixNano() < j.CommentDate.UnixNano()
+}
+
+func (c CommonCommentFormat) FormatLi(ctx *gin.Context, m models.Comments, depth int, eo, parent string) string {
+ return FormatLi(CommonLi(), ctx, m, depth, eo, parent)
+}
+
+var li = `
+
{{CommentContent}}
-