From f722d685eacc41796a5e0eae676393a61d98f3d6 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 16 Feb 2023 11:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- helper/strings/strings.go | 13 +++++-------- helper/strings/strings_test.go | 7 ++----- internal/middleware/sendmail.go | 11 ++++------- internal/plugins/comment.go | 7 +++---- internal/theme/common/custombackground.go | 13 ++++--------- 6 files changed, 19 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index 38dbb1c..12c9ce7 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/fthvgb1/wp-go -go 1.20 +go 1.19 require ( github.com/dlclark/regexp2 v1.7.0 diff --git a/helper/strings/strings.go b/helper/strings/strings.go index ee0811d..3df5779 100644 --- a/helper/strings/strings.go +++ b/helper/strings/strings.go @@ -2,7 +2,6 @@ package strings import ( "crypto/md5" - "errors" "fmt" "golang.org/x/exp/constraints" "io" @@ -61,16 +60,14 @@ func NewBuilder() *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 { - i, er := b.Builder.WriteString(ss) - if er != nil { - err = errors.Join(er) - } + i, _ := b.Builder.WriteString(ss) count += i } return } -func (b *Builder) Sprintf(format string, a ...any) (int, error) { - return b.WriteString(fmt.Sprintf(format, a...)) +func (b *Builder) Sprintf(format string, a ...any) int { + i, _ := fmt.Fprintf(b, format, a...) + return i } diff --git a/helper/strings/strings_test.go b/helper/strings/strings_test.go index 86d10f3..33f75f8 100644 --- a/helper/strings/strings_test.go +++ b/helper/strings/strings_test.go @@ -86,11 +86,8 @@ func TestBuilder_WriteString(t *testing.T) { b := &Builder{ Builder: tt.fields.Builder, } - gotCount, err := b.WriteString(tt.args.s...) - if (err != nil) != tt.wantErr { - t.Errorf("WriteString() error = %v, wantErr %v", err, tt.wantErr) - return - } + gotCount := b.WriteString(tt.args.s...) + if gotCount != tt.wantCount { t.Errorf("WriteString() gotCount = %v, want %v", gotCount, tt.wantCount) } diff --git a/internal/middleware/sendmail.go b/internal/middleware/sendmail.go index aded58d..427c8c1 100644 --- a/internal/middleware/sendmail.go +++ b/internal/middleware/sendmail.go @@ -3,6 +3,7 @@ package middleware import ( "bytes" "fmt" + str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/mail" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/logs" @@ -64,17 +65,13 @@ var ( ) func formatStack(s string) (r string) { - ss := strings.Builder{} + ss := str.NewBuilder() t := strings.Split(s, "\n") for i, line := range t { if i%2 == 0 { - ss.WriteString("
") - ss.WriteString(line) - ss.WriteString("
") + ss.WriteString("
", line, "
") } else { - ss.WriteString("
") - ss.WriteString(strings.Trim(line, "\t")) - ss.WriteString("
") + ss.WriteString("
", strings.Trim(line, "\t"), "
") } } r = ss.String() diff --git a/internal/plugins/comment.go b/internal/plugins/comment.go index d3e3cff..72c689e 100644 --- a/internal/plugins/comment.go +++ b/internal/plugins/comment.go @@ -2,6 +2,7 @@ package plugins import ( "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/gin-gonic/gin" "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) { - s := strings.Builder{} + s := str.NewBuilder() if d.depth > d.maxDepth { 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)) if fl { d.depth++ - s.WriteString(`
    `) - s.WriteString(d.formatComment(comment.Children, false)) - s.WriteString(`
`) + s.WriteString(`
    `, d.formatComment(comment.Children, false), `
`) if isTop { d.depth = 1 } diff --git a/internal/theme/common/custombackground.go b/internal/theme/common/custombackground.go index f786f2e..35a5ab2 100644 --- a/internal/theme/common/custombackground.go +++ b/internal/theme/common/custombackground.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/fthvgb1/wp-go/helper" "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/safety" - "strings" ) var postx = map[string]string{ @@ -50,11 +50,9 @@ func (h *Handle) CalCustomBackGround() (r string) { if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor { return } - s := strings.Builder{} + s := str.NewBuilder() if mods.BackgroundImage != "" { - s.WriteString(` background-image: url("`) - s.WriteString(helper.CutUrlHost(mods.BackgroundImage)) - s.WriteString(`");`) + s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage)) } backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX) 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.Or(attachment == "fixed", "fixed", "scroll") attachment = fmt.Sprintf(" background-attachment: %s;", attachment) - s.WriteString(positon) - s.WriteString(siz) - s.WriteString(repeats) - s.WriteString(attachment) + s.WriteString(positon, siz, repeats, attachment) r = fmt.Sprintf(``, s.String())