revise digest add set tag occupied number
This commit is contained in:
parent
6209f2b5e5
commit
a78815f3d3
|
@ -4,10 +4,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/app/pkg/config"
|
"github.com/fthvgb1/wp-go/app/pkg/config"
|
||||||
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||||
|
"github.com/fthvgb1/wp-go/cache/reload"
|
||||||
"github.com/fthvgb1/wp-go/helper"
|
"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/plugin/digest"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -18,10 +22,45 @@ var more = regexp.MustCompile("<!--more(.*?)?-->")
|
||||||
|
|
||||||
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
|
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
|
||||||
|
|
||||||
|
var digestConfig *safety.Var[DigestConfig]
|
||||||
|
|
||||||
func InitDigestCache() {
|
func InitDigestCache() {
|
||||||
cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
|
cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
|
||||||
return config.GetConfig().CacheTime.DigestCacheTime
|
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
|
||||||
|
}
|
||||||
|
if len(c.DigestTagOccupyNum) > 0 {
|
||||||
|
c.tagNum = map[string]int{}
|
||||||
|
for _, item := range c.DigestTagOccupyNum {
|
||||||
|
tags := strings.Split(item.Tag, "<")
|
||||||
|
for _, tag := range tags {
|
||||||
|
if tag == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.tagNum[str.Join("<", tag)] = item.Num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}, "digestConfig")
|
||||||
|
}
|
||||||
|
|
||||||
|
type DigestConfig struct {
|
||||||
|
DigestWordCount int `yaml:"digestWordCount"`
|
||||||
|
DigestAllowTag string `yaml:"digestAllowTag"`
|
||||||
|
DigestTagOccupyNum []struct {
|
||||||
|
Tag string `yaml:"tag"`
|
||||||
|
Num int `yaml:"num"`
|
||||||
|
} `yaml:"digestTagOccupyNum"`
|
||||||
|
tagNum map[string]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveWpBlock(s string) string {
|
func RemoveWpBlock(s string) string {
|
||||||
|
@ -45,7 +84,8 @@ func digestRaw(ctx context.Context, id uint64, arg ...any) (string, error) {
|
||||||
func Digests(content string, id uint64, limit int, fn func(id uint64, content, closeTag string) string) string {
|
func Digests(content string, id uint64, limit int, fn func(id uint64, content, closeTag string) string) string {
|
||||||
closeTag := ""
|
closeTag := ""
|
||||||
content = RemoveWpBlock(content)
|
content = RemoveWpBlock(content)
|
||||||
tag := config.GetConfig().DigestAllowTag
|
c := digestConfig.Load()
|
||||||
|
tag := c.DigestAllowTag
|
||||||
if tag == "" {
|
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>"
|
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>"
|
||||||
}
|
}
|
||||||
|
@ -54,7 +94,7 @@ func Digests(content string, id uint64, limit int, fn func(id uint64, content, c
|
||||||
if length <= limit {
|
if length <= limit {
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
content, closeTag = digest.Html(content, limit)
|
content, closeTag = digest.Html(content, limit, c.tagNum)
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
return PostsMore(id, content, closeTag)
|
return PostsMore(id, content, closeTag)
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,15 @@ cacheTime:
|
||||||
digestWordCount: 300
|
digestWordCount: 300
|
||||||
# 摘要允许的标签 默认为<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>
|
# 摘要允许的标签 默认为<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>
|
||||||
digestTag: "<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>"
|
digestTag: "<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>"
|
||||||
|
|
||||||
|
# 可以设置每个标签占用的字数,默认都为0 set tag occupied num, default every tag occupied 0
|
||||||
|
digestTagOccupyNum: [
|
||||||
|
{
|
||||||
|
tag: "<img><table>",
|
||||||
|
num: 2
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
# 到达指定并发请求数时随机sleep
|
# 到达指定并发请求数时随机sleep
|
||||||
maxRequestSleepNum: 100
|
maxRequestSleepNum: 100
|
||||||
# 全局最大请求数,超过直接403
|
# 全局最大请求数,超过直接403
|
||||||
|
@ -79,13 +88,13 @@ gzip: false
|
||||||
# 提交评论url host需为ip形式
|
# 提交评论url host需为ip形式
|
||||||
postCommentUrl: http://127.0.0.1/wp-comments-post.php
|
postCommentUrl: http://127.0.0.1/wp-comments-post.php
|
||||||
# TrustIps
|
# TrustIps
|
||||||
trustIps: []
|
trustIps: [ ]
|
||||||
# 分页器间隔数
|
# 分页器间隔数
|
||||||
paginationStep: 1
|
paginationStep: 1
|
||||||
# 显示查询的sql语句
|
# 显示查询的sql语句
|
||||||
showQuerySql: false
|
showQuerySql: false
|
||||||
# trust servername 信任的域名
|
# trust servername 信任的域名
|
||||||
trustServerNames: ["xy.test","blog.xy.test"]
|
trustServerNames: [ "xy.test","blog.xy.test" ]
|
||||||
# 主题 为空值为option template,没有就默认为twentyfifteen
|
# 主题 为空值为option template,没有就默认为twentyfifteen
|
||||||
theme: "twentyfifteen"
|
theme: "twentyfifteen"
|
||||||
# 文档排序默认升序还是降序
|
# 文档排序默认升序还是降序
|
||||||
|
@ -95,10 +104,10 @@ wpDir: "/var/www/html/wordpress"
|
||||||
# pprof route path 为空表示不开启pprof,否则为pprof的路由
|
# pprof route path 为空表示不开启pprof,否则为pprof的路由
|
||||||
pprof: "/debug/pprof"
|
pprof: "/debug/pprof"
|
||||||
# 要使用的程序插件名
|
# 要使用的程序插件名
|
||||||
plugins: ["enlightjs"]
|
plugins: [ "enlightjs" ]
|
||||||
# 插件存放路径
|
# 插件存放路径
|
||||||
pluginPath: "./plugins"
|
pluginPath: "./plugins"
|
||||||
# 列表页面post使用的插件
|
# 列表页面post使用的插件
|
||||||
listPagePlugins: ["digest"]
|
listPagePlugins: [ "digest" ]
|
||||||
# 额外引入的脚本 第一个为head 第二个为footer
|
# 额外引入的脚本 第一个为head 第二个为footer
|
||||||
externScript: ["",""]
|
externScript: [ "","" ]
|
|
@ -3,6 +3,7 @@ package digest
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/helper/html"
|
"github.com/fthvgb1/wp-go/helper/html"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -17,7 +18,7 @@ func StripTags(content, allowTag string) string {
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
func Html(content string, limit int) (string, string) {
|
func Html(content string, limit int, m map[string]int) (string, string) {
|
||||||
closeTag := ""
|
closeTag := ""
|
||||||
length := utf8.RuneCountInString(content) + 1
|
length := utf8.RuneCountInString(content) + 1
|
||||||
if length <= limit {
|
if length <= limit {
|
||||||
|
@ -38,6 +39,7 @@ func Html(content string, limit int) (string, string) {
|
||||||
total := len(ru)
|
total := len(ru)
|
||||||
l, r := '<', '>'
|
l, r := '<', '>'
|
||||||
i := -1
|
i := -1
|
||||||
|
var tag []rune
|
||||||
for {
|
for {
|
||||||
i++
|
i++
|
||||||
for len(runeIndex) > 0 && i >= runeIndex[0][0] {
|
for len(runeIndex) > 0 && i >= runeIndex[0][0] {
|
||||||
|
@ -60,10 +62,19 @@ func Html(content string, limit int) (string, string) {
|
||||||
continue
|
continue
|
||||||
} else if ru[i] == r {
|
} else if ru[i] == r {
|
||||||
tagIn = false
|
tagIn = false
|
||||||
|
if len(m) > 0 {
|
||||||
|
tags := str.Join("<", strings.Split(string(tag), " ")[0], ">")
|
||||||
|
tag = tag[:0]
|
||||||
|
if n, ok := m[tags]; ok && n > 0 {
|
||||||
|
end += n
|
||||||
|
}
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if tagIn == false {
|
if tagIn == false {
|
||||||
end++
|
end++
|
||||||
|
} else if len(m) > 0 {
|
||||||
|
tag = append(tag, ru[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if i > total {
|
if i > total {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user