标签闭合
This commit is contained in:
parent
2a6d9612f8
commit
735ae5c951
|
@ -126,3 +126,59 @@ func StripTagsX(str, allowable string) string {
|
|||
html := rex.ReplaceAllString(tmp, "<$1>")
|
||||
return html
|
||||
}
|
||||
|
||||
var tagx = regexp.MustCompile(`(</?[a-z0-9]+?)( |>)`)
|
||||
var selfCloseTags = []string{"area", "base", "basefont", "br", "col", "command", "embed", "frame", "hr", "img", "input", "isindex", "link", "meta", "param", "source", "track", "wbr"}
|
||||
|
||||
func CloseHtmlTag(str string) string {
|
||||
tags := tag.FindAllString(str, -1)
|
||||
if len(tags) < 1 {
|
||||
return str
|
||||
}
|
||||
var tagss = make([]string, 0, len(tags))
|
||||
for _, s := range tags {
|
||||
ss := strings.TrimSpace(tagx.FindString(s))
|
||||
if ss[len(ss)-1] != '>' {
|
||||
ss = fmt.Sprintf("%s>", ss)
|
||||
if IsContainInArr(ss, selfCloseTags) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
tagss = append(tagss, ss)
|
||||
}
|
||||
r := SliceMap[string, string](ClearClosedTag(tagss), func(s string) string {
|
||||
return fmt.Sprintf("</%s>", strings.Trim(s, "<>"))
|
||||
})
|
||||
return strings.Join(r, "")
|
||||
}
|
||||
|
||||
func ClearClosedTag(s []string) []string {
|
||||
i := 0
|
||||
for {
|
||||
if len(s[i:]) < 2 {
|
||||
return s
|
||||
}
|
||||
l := s[i]
|
||||
r := fmt.Sprintf(`</%s>`, strings.Trim(l, "<>"))
|
||||
if s[i+1] == r {
|
||||
if len(s[i+1:]) > 1 {
|
||||
ss := s[:i]
|
||||
s = append(ss, s[i+2:]...)
|
||||
|
||||
} else {
|
||||
s = s[:i]
|
||||
}
|
||||
i = 0
|
||||
continue
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
func SliceMap[T any, R any](arr []T, fun func(T) R) []R {
|
||||
r := make([]R, 0, len(arr))
|
||||
for _, t := range arr {
|
||||
r = append(r, fun(t))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
|
|
@ -263,3 +263,66 @@ func BenchmarkStripTagsX(b *testing.B) {
|
|||
StripTagsX(`<p>ppppp<span>ffff</span></p><img />`, "<p><img>")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseHtmlTag(t *testing.T) {
|
||||
type args struct {
|
||||
str string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "t1",
|
||||
args: args{str: `<pre class="wp-block-preformatted">GRANT privileges ON databasename.tablename TO 'username'@'h...<p class="read-more"><a href="/p/305">继续阅读</a></p>`},
|
||||
want: "</pre>",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := CloseHtmlTag(tt.args.str); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("CloseHtmlTag() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_clearTag(t *testing.T) {
|
||||
type args struct {
|
||||
s []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "t1",
|
||||
args: args{s: []string{"<pre>", "<p>", "<span>", "</span>"}},
|
||||
want: []string{"<pre>", "<p>"},
|
||||
},
|
||||
{
|
||||
name: "t2",
|
||||
args: args{s: []string{"<pre>", "</pre>", "<div>", "<span>", "</span>"}},
|
||||
want: []string{"<div>"},
|
||||
},
|
||||
{
|
||||
name: "t3",
|
||||
args: args{s: []string{"<pre>", "</pre>"}},
|
||||
want: []string{},
|
||||
},
|
||||
{
|
||||
name: "t4",
|
||||
args: args{s: []string{"<pre>", "<p>"}},
|
||||
want: []string{"<pre>", "<p>"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ClearClosedTag(tt.args.s); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ClearClosedTag() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ var tag = regexp.MustCompile(`<.*?>`)
|
|||
var limit = 300
|
||||
|
||||
func ExceptRaw(str string, limit, id int) string {
|
||||
|
||||
if r := more.FindString(str); r != "" {
|
||||
m := strings.Split(str, r)
|
||||
str = m[0]
|
||||
|
@ -40,7 +39,6 @@ func ExceptRaw(str string, limit, id int) string {
|
|||
count += len(t)
|
||||
l += len(t)
|
||||
}
|
||||
|
||||
if count > 0 && length > l {
|
||||
start = end
|
||||
end += count
|
||||
|
@ -48,8 +46,12 @@ func ExceptRaw(str string, limit, id int) string {
|
|||
break
|
||||
} else {
|
||||
content = string(ru[:end])
|
||||
content = fmt.Sprintf(`%s...<p class="read-more"><a href="/p/%d">继续阅读</a></p>`, content, id)
|
||||
// todo 标签闭合
|
||||
closeTag := CloseHtmlTag(content)
|
||||
if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") {
|
||||
content = fmt.Sprintf(`%s%s......<p class="read-more"><a href="/p/%d">继续阅读</a></p>`, content, closeTag, id)
|
||||
} else {
|
||||
content = fmt.Sprintf(`%s......%s<p class="read-more"><a href="/p/%d">继续阅读</a></p>`, content, closeTag, id)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -64,3 +66,25 @@ func Except(p *Plugin[models.WpPosts], c *gin.Context, post *models.WpPosts, sce
|
|||
post.PostContent = ExceptRaw(post.PostContent, limit, int(post.Id))
|
||||
|
||||
}
|
||||
|
||||
var tagx = regexp.MustCompile(`(</?[a-z0-9]+?)( |>)`)
|
||||
var tagAllow = regexp.MustCompile(`<(a|b|blockquote|cite|code|dd|del|div|dl|dt|em|h1|h2|h3|h4|h5|h6|i|li|ol|p|pre|span|strong|ul).*?>`)
|
||||
|
||||
func CloseHtmlTag(str string) string {
|
||||
tags := tagAllow.FindAllString(str, -1)
|
||||
if len(tags) < 1 {
|
||||
return str
|
||||
}
|
||||
var tagss = make([]string, 0, len(tags))
|
||||
for _, s := range tags {
|
||||
ss := strings.TrimSpace(tagx.FindString(s))
|
||||
if ss[len(ss)-1] != '>' {
|
||||
ss = fmt.Sprintf("%s>", ss)
|
||||
}
|
||||
tagss = append(tagss, ss)
|
||||
}
|
||||
r := helper.SliceMap[string, string](helper.ClearClosedTag(tagss), func(s string) string {
|
||||
return fmt.Sprintf("</%s>", strings.Trim(s, "<>"))
|
||||
})
|
||||
return strings.Join(r, "")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user