wp-go/plugin/digest/digest.go

88 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-01-12 12:42:16 +00:00
package digest
2022-09-26 08:35:38 +00:00
import (
"fmt"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/html"
"github.com/fthvgb1/wp-go/helper/slice"
2022-09-26 08:35:38 +00:00
"regexp"
"strings"
"unicode/utf8"
)
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
var more = regexp.MustCompile("<!--more(.*?)?-->")
2023-01-12 12:42:16 +00:00
var quto = regexp.MustCompile(`&quot; *|&amp; *|&lt; *|&gt; ?|&nbsp; *`)
2022-09-26 08:35:38 +00:00
2022-10-08 08:37:27 +00:00
func ClearHtml(str string) string {
content := removeWpBlock.ReplaceAllString(str, "")
content = strings.Trim(content, " \t\n\r\000\x0B")
content = strings.Replace(content, "]]>", "]]&gt;", -1)
2023-01-21 11:31:23 +00:00
content = html.StripTagsX(content, "<a><b><blockquote><br><cite><code><dd><del><div><dl><dt><em><h1><h2><h3><h4><h5><h6><i><img><li><ol><p><pre><span><strong><ul>")
2022-10-08 08:37:27 +00:00
return str
}
2023-01-12 12:42:16 +00:00
func Raw(str string, limit int, u string) string {
2022-09-26 08:35:38 +00:00
if r := more.FindString(str); r != "" {
m := strings.Split(str, r)
str = m[0]
return ""
}
content := removeWpBlock.ReplaceAllString(str, "")
content = strings.Trim(content, " \t\n\r\000\x0B")
content = strings.Replace(content, "]]>", "]]&gt;", -1)
2023-01-21 11:31:23 +00:00
content = html.StripTagsX(content, "<a><b><blockquote><br><cite><code><dd><del><div><dl><dt><em><h1><h2><h3><h4><h5><h6><i><img><li><ol><p><pre><span><strong><ul>")
2022-09-26 08:35:38 +00:00
length := utf8.RuneCountInString(content) + 1
if length > limit {
index := quto.FindAllStringIndex(content, -1)
end := 0
ru := []rune(content)
tagIn := false
total := len(ru)
l, r := '<', '>'
i := -1
for {
i++
for len(index) > 0 {
2023-01-21 11:31:23 +00:00
ints := slice.Map(index[0], func(t int) int {
2022-09-26 08:35:38 +00:00
return utf8.RuneCountInString(content[:t])
})
if ints[0] <= i {
i = i + i - ints[0] + ints[1] - ints[0]
index = index[1:]
end++
continue
} else {
break
}
}
if end >= limit || i >= total-1 {
break
}
if ru[i] == l {
tagIn = true
continue
} else if ru[i] == r {
tagIn = false
continue
}
if tagIn == false {
end++
}
}
if i > total-1 {
i = total - 1
}
content = string(ru[:i])
2023-01-21 11:31:23 +00:00
closeTag := html.CloseTag(content)
2022-10-08 06:01:05 +00:00
tmp := `%s......%s<p class="read-more"><a href="%s">继续阅读</a></p>`
2022-09-26 08:35:38 +00:00
if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") {
2022-10-08 06:01:05 +00:00
tmp = `%s%s......<p class="read-more"><a href="%s">继续阅读</a></p>`
2022-09-26 08:35:38 +00:00
}
2022-10-08 06:01:05 +00:00
content = fmt.Sprintf(tmp, content, closeTag, u)
2022-09-26 08:35:38 +00:00
}
return content
}