This commit is contained in:
xing 2023-02-16 11:53:24 +08:00
parent c60c11769a
commit f722d685ea
6 changed files with 19 additions and 34 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/fthvgb1/wp-go module github.com/fthvgb1/wp-go
go 1.20 go 1.19
require ( require (
github.com/dlclark/regexp2 v1.7.0 github.com/dlclark/regexp2 v1.7.0

View File

@ -2,7 +2,6 @@ package strings
import ( import (
"crypto/md5" "crypto/md5"
"errors"
"fmt" "fmt"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
"io" "io"
@ -61,16 +60,14 @@ func NewBuilder() *Builder {
return &Builder{&strings.Builder{}} return &Builder{&strings.Builder{}}
} }
func (b *Builder) WriteString(s ...string) (count int, err error) { func (b *Builder) WriteString(s ...string) (count int) {
for _, ss := range s { for _, ss := range s {
i, er := b.Builder.WriteString(ss) i, _ := b.Builder.WriteString(ss)
if er != nil {
err = errors.Join(er)
}
count += i count += i
} }
return return
} }
func (b *Builder) Sprintf(format string, a ...any) (int, error) { func (b *Builder) Sprintf(format string, a ...any) int {
return b.WriteString(fmt.Sprintf(format, a...)) i, _ := fmt.Fprintf(b, format, a...)
return i
} }

View File

@ -86,11 +86,8 @@ func TestBuilder_WriteString(t *testing.T) {
b := &Builder{ b := &Builder{
Builder: tt.fields.Builder, Builder: tt.fields.Builder,
} }
gotCount, err := b.WriteString(tt.args.s...) gotCount := b.WriteString(tt.args.s...)
if (err != nil) != tt.wantErr {
t.Errorf("WriteString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotCount != tt.wantCount { if gotCount != tt.wantCount {
t.Errorf("WriteString() gotCount = %v, want %v", gotCount, tt.wantCount) t.Errorf("WriteString() gotCount = %v, want %v", gotCount, tt.wantCount)
} }

View File

@ -3,6 +3,7 @@ package middleware
import ( import (
"bytes" "bytes"
"fmt" "fmt"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/mail" "github.com/fthvgb1/wp-go/internal/mail"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
@ -64,17 +65,13 @@ var (
) )
func formatStack(s string) (r string) { func formatStack(s string) (r string) {
ss := strings.Builder{} ss := str.NewBuilder()
t := strings.Split(s, "\n") t := strings.Split(s, "\n")
for i, line := range t { for i, line := range t {
if i%2 == 0 { if i%2 == 0 {
ss.WriteString("<dt>") ss.WriteString("<dt>", line, "</dt>")
ss.WriteString(line)
ss.WriteString("</dt>")
} else { } else {
ss.WriteString("<dd>") ss.WriteString("<dt>", strings.Trim(line, "\t"), "</dt>")
ss.WriteString(strings.Trim(line, "\t"))
ss.WriteString("</dd>")
} }
} }
r = ss.String() r = ss.String()

View File

@ -2,6 +2,7 @@ package plugins
import ( import (
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/url" "net/url"
@ -50,7 +51,7 @@ func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, m
} }
func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html string) { func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html string) {
s := strings.Builder{} s := str.NewBuilder()
if d.depth > d.maxDepth { if d.depth > d.maxDepth {
comments = d.findComments(comments) comments = d.findComments(comments)
} }
@ -69,9 +70,7 @@ func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html st
s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, d.isTls, eo, parent)) s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, d.isTls, eo, parent))
if fl { if fl {
d.depth++ d.depth++
s.WriteString(`<ol class="children">`) s.WriteString(`<ol class="children">`, d.formatComment(comment.Children, false), `</ol>`)
s.WriteString(d.formatComment(comment.Children, false))
s.WriteString(`</ol>`)
if isTop { if isTop {
d.depth = 1 d.depth = 1
} }

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"strings"
) )
var postx = map[string]string{ var postx = map[string]string{
@ -50,11 +50,9 @@ func (h *Handle) CalCustomBackGround() (r string) {
if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor { if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor {
return return
} }
s := strings.Builder{} s := str.NewBuilder()
if mods.BackgroundImage != "" { if mods.BackgroundImage != "" {
s.WriteString(` background-image: url("`) s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage))
s.WriteString(helper.CutUrlHost(mods.BackgroundImage))
s.WriteString(`");`)
} }
backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX) backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX)
backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left") backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left")
@ -74,10 +72,7 @@ func (h *Handle) CalCustomBackGround() (r string) {
attachment := helper.Defaults(mods.BackgroundAttachment, mods.ThemeSupport.CustomBackground.DefaultAttachment) attachment := helper.Defaults(mods.BackgroundAttachment, mods.ThemeSupport.CustomBackground.DefaultAttachment)
attachment = helper.Or(attachment == "fixed", "fixed", "scroll") attachment = helper.Or(attachment == "fixed", "fixed", "scroll")
attachment = fmt.Sprintf(" background-attachment: %s;", attachment) attachment = fmt.Sprintf(" background-attachment: %s;", attachment)
s.WriteString(positon) s.WriteString(positon, siz, repeats, attachment)
s.WriteString(siz)
s.WriteString(repeats)
s.WriteString(attachment)
r = fmt.Sprintf(`<style id="custom-background-css"> r = fmt.Sprintf(`<style id="custom-background-css">
body.custom-background {%s} body.custom-background {%s}
</style>`, s.String()) </style>`, s.String())