From 2a6d9612f8b0392b0c14fb6a1725fdf59b9c8ac7 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 24 Sep 2022 17:52:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/index.go | 12 ++++++------ go.mod | 7 ++++--- helper/func.go | 24 ++++++++++++++++++++++++ helper/func_test.go | 39 +++++++++++++++++++++++++++++++++++++++ plugins/Excerpt.go | 13 ++++++++----- route/route.go | 3 ++- 6 files changed, 83 insertions(+), 15 deletions(-) diff --git a/actions/index.go b/actions/index.go index 1040fca..d53ad48 100644 --- a/actions/index.go +++ b/actions/index.go @@ -191,13 +191,13 @@ func Index(c *gin.Context) { post, _ := common.PostsCache.Load(v.Id) pp := post.(*models.WpPosts) px := *pp - common.PasswordProjectTitle(&px) - if px.PostPassword != "" && pw != px.PostPassword { - common.PasswdProjectContent(&px) - } postIds[i] = px - plugins.ApplyPlugin(plug, &postIds[i]) - + common.PasswordProjectTitle(&postIds[i]) + if px.PostPassword != "" && pw != px.PostPassword { + common.PasswdProjectContent(&postIds[i]) + } else { + plugins.ApplyPlugin(plug, &postIds[i]) + } } for i, post := range recent { diff --git a/go.mod b/go.mod index 717278a..2cc72d0 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( require ( github.com/dlclark/regexp2 v1.7.0 // indirect + github.com/gin-contrib/pprof v1.4.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect @@ -27,9 +28,9 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/ugorji/go/codec v1.2.7 // indirect - golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect - golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect - golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect + golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect + golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect + golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.1 // indirect ) diff --git a/helper/func.go b/helper/func.go index c07369a..4a15b87 100644 --- a/helper/func.go +++ b/helper/func.go @@ -102,3 +102,27 @@ func StripTags(str, allowable string) string { } return html } + +var tag = regexp.MustCompile(`<(.*?)>`) + +func StripTagsX(str, allowable string) string { + if allowable == "" { + return allHtmlTag.ReplaceAllString(str, "") + } + tags := tag.ReplaceAllString(allowable, "$1|") + or := strings.TrimRight(tags, "|") + reg := fmt.Sprintf(`<(/?(%s).*?)>`, or) + regx := fmt.Sprintf(`\{\[(/?(%s).*?)\]\}`, or) + cp, err := regexp.Compile(reg) + if err != nil { + return str + } + rep := cp.ReplaceAllString(str, "{[$1]}") + tmp := tag.ReplaceAllString(rep, "") + rex, err := regexp.Compile(regx) + if err != nil { + return str + } + html := rex.ReplaceAllString(tmp, "<$1>") + return html +} diff --git a/helper/func_test.go b/helper/func_test.go index c288899..c94f2de 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -224,3 +224,42 @@ func TestStripTags(t *testing.T) { }) } } + +func TestStripTagsX(t *testing.T) { + type args struct { + str string + allowable string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "t1", + args: args{ + str: "

pppppffff

", + allowable: "

", + }, + want: "

pppppffff

", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := StripTagsX(tt.args.str, tt.args.allowable); got != tt.want { + t.Errorf("StripTagsX() = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkStripTags(b *testing.B) { + for i := 0; i < b.N; i++ { + StripTags(`

pppppffff

`, "

") + } +} +func BenchmarkStripTagsX(b *testing.B) { + for i := 0; i < b.N; i++ { + StripTagsX(`

pppppffff

`, "

") + } +} diff --git a/plugins/Excerpt.go b/plugins/Excerpt.go index c41986b..87ad190 100644 --- a/plugins/Excerpt.go +++ b/plugins/Excerpt.go @@ -25,16 +25,18 @@ func ExceptRaw(str string, limit, id int) string { content := removeWpBlock.ReplaceAllString(str, "") content = strings.Trim(content, " \t\n\r\000\x0B") content = strings.Replace(content, "]]>", "]]>", -1) - content = helper.StripTags(content, "