-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
88 lines (74 loc) · 2.31 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package utils
import (
"encoding/json"
"html"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// CleanWikiaComment to remove wikia comment.
func CleanWikiaComment(str string) string {
return regexp.MustCompile(`<!--.+?-->`).ReplaceAllString(str, "")
}
// CleanWikiaTag to remove wikia tag.
func CleanWikiaTag(str, tag string, deleteContent bool) string {
str = regexp.MustCompile(`<`+tag+` .+/>`).ReplaceAllString(str, "")
if deleteContent {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(str))
doc.Find(tag).Remove()
str, _ = doc.Html()
return html.UnescapeString(str)
}
return regexp.MustCompile(`</*`+tag+`>`).ReplaceAllString(str, "")
}
// NormalizeNewLine to normalize new line to <br>.
func NormalizeNewLine(str string) string {
return regexp.MustCompile(`(?i)((\n|\\n|<br\s*\/*>)+)`).ReplaceAllString(str, "<br>")
}
// WikiaExternalLinkToStr to convert wikia external link to string.
func WikiaExternalLinkToStr(str string) string {
linkRegex := regexp.MustCompile(`\[.+?\s+(.+?)\]`)
if linkRegex.FindString(str) == "" {
return str
}
return linkRegex.ReplaceAllString(str, "$1")
}
// WikiaInternalLinkToStr to convert wikia internal link to string.
func WikiaInternalLinkToStr(str string) string {
linkRegex := regexp.MustCompile(`\[\[(.+?)\]\]`)
if linkRegex.FindString(str) == "" {
return str
}
return linkRegex.ReplaceAllString(str, "$1")
}
// NormalizeWikiaInternalLink to normalize wikia internal link.
// [[NIJISANJI (main branch)|NIJISANJI]] => [[NIJISANJI]]
func NormalizeWikiaInternalLink(str string) string {
linkRegex := regexp.MustCompile(`\[\[([^\|\]]+\|)*(.+?)(\]\])?\]\]`)
return linkRegex.ReplaceAllString(str, "[[$2]]")
}
// GetWikiaExternalLink to get wikia external link.
func GetWikiaExternalLink(str string) string {
linkRegex := regexp.MustCompile(`\[([^\]\s]+).*?\]?\]`)
if linkRegex.FindString(str) == "" {
return ""
}
submatch := linkRegex.FindStringSubmatch(str)
if len(submatch) < 2 {
return ""
}
return strings.TrimSpace(submatch[1])
}
// RemoveAllHTMLTag to remove all html tag.
func RemoveAllHTMLTag(str string) string {
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(str))
return html.UnescapeString(doc.Text())
}
//
func Marshal(strs []string) string {
if len(strs) == 0 {
return ""
}
j, _ := json.Marshal(strs)
return string(j)
}