-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
243 lines (200 loc) · 5.12 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package utils
import (
"errors"
"html"
"math"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"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())
}
// PtrToBool to convert pointer to bool.
func PtrToBool(b *bool) bool {
if b != nil {
return *b
}
return false
}
// StrToPtrBool to convert string to bool pointer.
func StrToPtrBool(str string) *bool {
if str == "" {
return nil
}
b, _ := strconv.ParseBool(str)
return &b
}
// StrToStrSlice to convert string to slice of string.
func StrToStrSlice(str string) []string {
if str == "" {
return nil
}
return strings.Split(str, ",")
}
// GetLastPathFromURL to get the last path from url.
func GetLastPathFromURL(str string) string {
url, err := url.Parse(str)
if err != nil {
return ""
}
if url.Path[len(url.Path)-1] == '/' {
url.Path = url.Path[:len(url.Path)-1]
}
splitPath := strings.Split(url.Path, "/")
return splitPath[len(splitPath)-1]
}
const (
parsingPeriod = iota
parsingTime
)
// ParseDuration to parse ISO 8601 duration.
func ParseDuration(durationStr string, startFromTime ...bool) (time.Duration, error) {
state := parsingPeriod
if len(startFromTime) > 0 && startFromTime[0] {
state = parsingTime
}
duration := 0 * time.Second
num := ""
err := errors.New("invalid duration")
for _, c := range durationStr {
switch c {
case 'P', 'p':
state = parsingPeriod
case 'T', 't':
state = parsingTime
case 'Y', 'y':
if state != parsingPeriod {
return 0, err
}
y, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(y)) * 365 * 24 * time.Hour
num = ""
case 'M', 'm':
if state == parsingPeriod {
m, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(m)) * 30 * 24 * time.Hour
num = ""
} else if state == parsingTime {
m, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(m)) * time.Minute
num = ""
}
case 'W', 'w':
if state != parsingPeriod {
return 0, err
}
w, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(w)) * 7 * 24 * time.Hour
num = ""
case 'D', 'd':
if state != parsingPeriod {
return 0, err
}
d, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(d)) * 24 * time.Hour
num = ""
case 'H', 'h':
if state != parsingTime {
return 0, err
}
h, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(h)) * time.Hour
num = ""
case 'S', 's':
if state != parsingTime {
return 0, err
}
s, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}
duration += time.Duration(math.Round(s)) * time.Second
num = ""
default:
if unicode.IsNumber(c) || c == '.' {
num += string(c)
continue
}
return 0, err
}
}
return duration, nil
}