wp-go/plugins/digest.go

134 lines
3.3 KiB
Go
Raw Normal View History

2022-09-26 08:35:38 +00:00
package plugins
import (
"fmt"
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/cache"
2022-11-04 02:38:59 +00:00
"github/fthvgb1/wp-go/config"
2022-09-26 08:35:38 +00:00
"github/fthvgb1/wp-go/helper"
2022-11-05 02:32:57 +00:00
"github/fthvgb1/wp-go/models/wp"
2022-09-26 08:35:38 +00:00
"regexp"
"strings"
"time"
"unicode/utf8"
)
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
var more = regexp.MustCompile("<!--more(.*?)?-->")
var digestCache *cache.MapCache[uint64, string]
var quto = regexp.MustCompile(`&quot; *|&amp; *|&lt; *|&gt; ?|&nbsp; *`)
func InitDigestCache() {
2022-11-15 08:36:21 +00:00
digestCache = cache.NewMapCacheByFn[uint64](digestRaw, config.Conf.Load().DigestCacheTime)
2022-09-28 13:16:05 +00:00
}
func ClearDigestCache() {
digestCache.ClearExpired()
2022-09-26 08:35:38 +00:00
}
2022-11-15 08:36:21 +00:00
func FlushCache() {
digestCache.Flush()
}
2022-09-26 08:35:38 +00:00
func digestRaw(arg ...any) (string, error) {
str := arg[0].(string)
id := arg[1].(uint64)
2022-11-15 08:36:21 +00:00
limit := config.Conf.Load().DigestWordCount
2022-09-26 08:35:38 +00:00
if limit < 0 {
return str, nil
} else if limit == 0 {
return "", nil
}
2022-10-08 06:01:05 +00:00
return DigestRaw(str, limit, fmt.Sprintf("/p/%d", id)), nil
2022-09-26 08:35:38 +00:00
}
func DigestCache(ctx *gin.Context, id uint64, str string) string {
content, _ := digestCache.GetCache(ctx, id, time.Second, str, id)
return content
}
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)
content = helper.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>")
return str
}
2022-10-08 06:01:05 +00:00
func DigestRaw(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)
content = helper.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>")
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 {
ints := helper.SliceMap(index[0], func(t int) int {
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])
closeTag := helper.CloseHtmlTag(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
}
2022-11-05 14:40:02 +00:00
func Digest(p *Plugin[wp.Posts], c *gin.Context, post *wp.Posts, scene uint) {
2022-09-26 08:35:38 +00:00
if scene == Detail {
return
}
if post.PostExcerpt != "" {
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
} else {
post.PostContent = DigestCache(c, post.Id, post.PostContent)
}
2022-09-29 09:23:02 +00:00
p.Next()
2022-09-26 08:35:38 +00:00
}