2023-01-12 12:42:16 +00:00
|
|
|
package digest
|
2022-09-26 08:35:38 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
)
|
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
var quto = regexp.MustCompile(`" *|& *|< *|> ?| *`)
|
2022-09-26 08:35:38 +00:00
|
|
|
|
2023-04-18 13:05:37 +00:00
|
|
|
func StripTags(content, allowTag string) string {
|
2022-10-08 08:37:27 +00:00
|
|
|
content = strings.Trim(content, " \t\n\r\000\x0B")
|
|
|
|
content = strings.Replace(content, "]]>", "]]>", -1)
|
2023-04-18 13:05:37 +00:00
|
|
|
content = html.StripTagsX(content, allowTag)
|
|
|
|
return content
|
2022-10-08 08:37:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:05:37 +00:00
|
|
|
func Html(content string, limit int) (string, string) {
|
|
|
|
closeTag := ""
|
2022-09-26 08:35:38 +00:00
|
|
|
length := utf8.RuneCountInString(content) + 1
|
2023-04-18 13:05:37 +00:00
|
|
|
if length <= limit {
|
|
|
|
return content, ""
|
|
|
|
}
|
|
|
|
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 := slice.Map(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:]
|
2022-09-26 08:35:38 +00:00
|
|
|
end++
|
2023-04-18 13:05:37 +00:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
break
|
2022-09-26 08:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 13:05:37 +00:00
|
|
|
if end >= limit || i >= total {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ru[i] == l {
|
|
|
|
tagIn = true
|
|
|
|
continue
|
|
|
|
} else if ru[i] == r {
|
|
|
|
tagIn = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tagIn == false {
|
|
|
|
end++
|
2022-09-26 08:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-18 13:05:37 +00:00
|
|
|
if i > total {
|
|
|
|
i = total
|
|
|
|
}
|
|
|
|
content = string(ru[:i])
|
|
|
|
closeTag = html.CloseTag(content)
|
|
|
|
return content, closeTag
|
2022-09-26 08:35:38 +00:00
|
|
|
}
|