refactor(internal): compile regex once while clearing text
Regex and Replacer can be reused. No need to recreate for each invocation. Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
This commit is contained in:
committed by
LinkinStars
parent
09b6b34b05
commit
a15dd41550
+22
-26
@@ -34,38 +34,34 @@ import (
|
||||
"github.com/mozillazg/go-pinyin"
|
||||
)
|
||||
|
||||
// ClearText clear HTML, get the clear text
|
||||
func ClearText(html string) (text string) {
|
||||
if len(html) == 0 {
|
||||
text = html
|
||||
return
|
||||
}
|
||||
var (
|
||||
reCode = regexp.MustCompile(`(?ism)<(pre)>.*<\/pre>`)
|
||||
reCodeReplace = "{code...}"
|
||||
reLink = regexp.MustCompile(`(?ism)<a.*?[^<]>(.*)?<\/a>`)
|
||||
reLinkReplace = " [$1] "
|
||||
reSpace = regexp.MustCompile(` +`)
|
||||
reSpaceReplace = " "
|
||||
|
||||
var (
|
||||
re *regexp.Regexp
|
||||
codeReg = `(?ism)<(pre)>.*<\/pre>`
|
||||
codeRepl = "{code...}"
|
||||
linkReg = `(?ism)<a.*?[^<]>(.*)?<\/a>`
|
||||
linkRepl = " [$1] "
|
||||
spaceReg = ` +`
|
||||
spaceRepl = " "
|
||||
)
|
||||
re = regexp.MustCompile(codeReg)
|
||||
html = re.ReplaceAllString(html, codeRepl)
|
||||
|
||||
re = regexp.MustCompile(linkReg)
|
||||
html = re.ReplaceAllString(html, linkRepl)
|
||||
|
||||
text = strings.NewReplacer(
|
||||
spaceReplacer = strings.NewReplacer(
|
||||
"\n", " ",
|
||||
"\r", " ",
|
||||
"\t", " ",
|
||||
).Replace(strip.StripTags(html))
|
||||
)
|
||||
)
|
||||
|
||||
// ClearText clear HTML, get the clear text
|
||||
func ClearText(html string) string {
|
||||
if html == "" {
|
||||
return html
|
||||
}
|
||||
|
||||
html = reCode.ReplaceAllString(html, reCodeReplace)
|
||||
html = reLink.ReplaceAllString(html, reLinkReplace)
|
||||
|
||||
text := spaceReplacer.Replace(strip.StripTags(html))
|
||||
|
||||
// replace multiple spaces to one space
|
||||
re = regexp.MustCompile(spaceReg)
|
||||
text = strings.TrimSpace(re.ReplaceAllString(text, spaceRepl))
|
||||
return
|
||||
return strings.TrimSpace(reSpace.ReplaceAllString(text, reSpaceReplace))
|
||||
}
|
||||
|
||||
func UrlTitle(title string) (text string) {
|
||||
|
||||
Reference in New Issue
Block a user