wp-go/app/plugins/digest.go

180 lines
5.1 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package plugins
import (
2023-02-02 11:16:18 +00:00
"context"
2023-01-12 12:42:16 +00:00
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/logs"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/models"
2023-11-07 07:18:34 +00:00
"github.com/fthvgb1/wp-go/cache/cachemanager"
"github.com/fthvgb1/wp-go/cache/reload"
2023-04-18 13:05:37 +00:00
"github.com/fthvgb1/wp-go/helper"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/plugin/digest"
"github.com/fthvgb1/wp-go/safety"
2023-04-18 13:05:37 +00:00
"regexp"
2023-01-12 12:42:16 +00:00
"strings"
"time"
2023-10-14 06:57:57 +00:00
"unicode/utf8"
2023-01-12 12:42:16 +00:00
)
2023-04-18 13:05:37 +00:00
var more = regexp.MustCompile("<!--more(.*?)?-->")
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
2024-04-09 15:51:45 +00:00
type DigestConfig struct {
DigestWordCount int `yaml:"digestWordCount"`
DigestAllowTag string `yaml:"digestAllowTag"`
DigestRegex string `yaml:"digestRegex"`
DigestTagOccupyNum []struct {
Tag string `yaml:"tag"`
Num int `yaml:"num"`
ChuckOvered bool `yaml:"chuckOvered"`
EscapeCharacter []struct {
Tags string `yaml:"tags"`
Character []string `yaml:"character"`
Num int `yaml:"num"`
ChuckOvered bool `yaml:"chuckOvered"`
} `yaml:"escapeCharacter"`
} `yaml:"digestTagOccupyNum"`
specialSolve map[string]digest.SpecialSolveConf
}
var digestConfig *safety.Var[DigestConfig]
2023-01-12 12:42:16 +00:00
func InitDigestCache() {
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
return config.GetConfig().CacheTime.DigestCacheTime
})
digestConfig = reload.VarsBy(func() DigestConfig {
c, err := config.GetCustomizedConfig[DigestConfig]()
if err != nil {
logs.Error(err, "get digest config fail")
c.DigestWordCount = config.GetConfig().DigestWordCount
c.DigestAllowTag = config.GetConfig().DigestAllowTag
return c
}
2024-04-09 15:51:45 +00:00
if c.DigestRegex != "" {
digest.SetQutos(c.DigestRegex)
}
if len(c.DigestTagOccupyNum) <= 1 {
return c
}
2024-04-10 14:31:08 +00:00
c.specialSolve = ParseDigestConf(c)
return c
}, "digestConfig")
}
func ParseDigestConf(c DigestConfig) map[string]digest.SpecialSolveConf {
specialSolve := map[string]digest.SpecialSolveConf{}
for _, item := range c.DigestTagOccupyNum {
tags := strings.Split(strings.ReplaceAll(item.Tag, " ", ""), "<")
for _, tag := range tags {
if tag == "" {
continue
}
tag = str.Join("<", tag)
var ec map[rune]digest.SpecialSolve
var specialTags map[string]digest.SpecialSolve
if len(item.EscapeCharacter) > 0 {
ec = make(map[rune]digest.SpecialSolve)
2024-04-10 14:41:26 +00:00
specialTags = make(map[string]digest.SpecialSolve)
2024-04-10 14:31:08 +00:00
for _, esc := range item.EscapeCharacter {
for _, i := range esc.Character {
s := []rune(i)
if len(s) == 1 {
ec[s[0]] = digest.SpecialSolve{
Num: esc.Num,
ChuckOvered: esc.ChuckOvered,
2024-04-09 15:51:45 +00:00
}
}
2024-04-10 14:31:08 +00:00
}
if esc.Tags == "" {
continue
}
tagss := strings.Split(strings.ReplaceAll(esc.Tags, " ", ""), "<")
for _, t := range tagss {
if t == "" {
2024-04-09 15:51:45 +00:00
continue
}
2024-04-10 14:31:08 +00:00
t = str.Join("<", t)
specialTags[t] = digest.SpecialSolve{
Num: esc.Num,
ChuckOvered: esc.ChuckOvered,
2024-04-09 15:51:45 +00:00
}
}
}
}
2024-04-10 14:31:08 +00:00
specialSolve[tag] = digest.SpecialSolveConf{
Num: item.Num,
ChuckOvered: item.ChuckOvered,
EscapeCharacter: ec,
Tags: specialTags,
}
}
2024-04-10 14:31:08 +00:00
}
return specialSolve
}
2023-04-18 13:05:37 +00:00
func RemoveWpBlock(s string) string {
return removeWpBlock.ReplaceAllString(s, "")
}
func digestRaw(ctx context.Context, id uint64, arg ...any) (string, error) {
2023-04-18 13:05:37 +00:00
s := arg[1].(string)
limit := arg[3].(int)
2023-01-12 12:42:16 +00:00
if limit < 0 {
2023-04-18 13:05:37 +00:00
return s, nil
2023-01-12 12:42:16 +00:00
} else if limit == 0 {
return "", nil
}
2023-04-18 13:05:37 +00:00
s = more.ReplaceAllString(s, "")
fn := helper.GetContextVal(ctx, "postMoreFn", PostsMore)
return Digests(s, id, limit, fn), nil
}
func Digests(content string, id uint64, limit int, fn func(id uint64, content, closeTag string) string) string {
closeTag := ""
content = RemoveWpBlock(content)
c := digestConfig.Load()
tag := c.DigestAllowTag
2023-04-18 13:05:37 +00:00
if tag == "" {
tag = "<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>"
}
content = digest.StripTags(content, tag)
2023-10-14 06:57:57 +00:00
length := utf8.RuneCountInString(content) + 1
if length <= limit {
return content
}
2024-04-09 15:51:45 +00:00
if len(c.specialSolve) > 0 {
content, closeTag = digest.CustomizeHtml(content, limit, c.specialSolve)
} else {
content, closeTag = digest.Html(content, limit)
}
2023-04-18 13:05:37 +00:00
if fn == nil {
return PostsMore(id, content, closeTag)
}
return fn(id, content, closeTag)
}
func PostsMore(id uint64, content, closeTag string) string {
tmp := `%s......%s<p class="read-more"><a href="/p/%d">继续阅读</a></p>`
if strings.Contains(closeTag, "pre") || strings.Contains(closeTag, "code") {
tmp = `%s%s......<p class="read-more"><a href="/p/%d">继续阅读</a></p>`
}
content = fmt.Sprintf(tmp, content, closeTag, id)
return content
2023-01-12 12:42:16 +00:00
}
func Digest(ctx context.Context, post *models.Posts, limit int) {
content, _ := cachemanager.GetBy[string]("digestPlugin", ctx, post.Id, time.Second, ctx, post.PostContent, post.Id, limit)
post.PostContent = content
2023-01-12 12:42:16 +00:00
}
func PostExcerpt(post *models.Posts) {
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
2023-01-12 12:42:16 +00:00
}