-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathutils.go
263 lines (208 loc) · 5.83 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"html"
"os"
"regexp"
"strings"
)
const (
linkPattern = `<a.+?href="(.+?)".*?>(.+?)<\/a>`
externalURLPattern = `--(http[s]:\/\/.+?)--`
internalURLPattern = `--(\/.+?)--`
anchorURLPattern = `--(#.+?)--`
imagePattern = `<img.+?alt="(.+?)".*?>`
tagNlPattern = `<(?:p|div|li|blockquote|br).*?>`
tagPattern = `<.+?>`
tagElemPattern = `<.+?>(.+?)<\/.+?>`
multiSpacePattern = `(\s)\s+`
)
var (
linkRegexp = regexp.MustCompile(linkPattern)
externalURLRegexp = regexp.MustCompile(externalURLPattern)
internalURLRegexp = regexp.MustCompile(internalURLPattern)
anchorURLRegexp = regexp.MustCompile(anchorURLPattern)
imageRegexp = regexp.MustCompile(imagePattern)
tagRegexp = regexp.MustCompile(tagPattern)
tagNlRegexp = regexp.MustCompile(tagNlPattern)
tagElemRegexp = regexp.MustCompile(tagElemPattern)
multiSpaceRegexp = regexp.MustCompile(multiSpacePattern)
)
func logInfof(format string, args ...any) {
fmt.Printf("[INFO] "+format+"\n", args...)
}
func logErrorf(format string, args ...any) {
fmt.Printf("[ERROR] "+format+"\n", args...)
}
func exitOnErr(err error) {
if err != nil {
logErrorf("%v", err)
os.Exit(1)
}
}
func join(lines []string) string {
return strings.Join(lines, "")
}
func forEach(lines []string, op func(text string) string) []string {
var newLines []string
for _, line := range lines {
newLines = append(newLines, op(line))
}
return newLines
}
func splitNl(text string) []string {
return strings.Split(text, "\n")
}
func removeNl(text string) string {
return strings.ReplaceAll(text, "\n", "")
}
func replaceHTML(text string) string {
text = imageRegexp.ReplaceAllString(text, "$1")
text = linkRegexp.ReplaceAllString(text, "$2 --$1--")
text = externalURLRegexp.ReplaceAllString(text, "($1)")
text = internalURLRegexp.ReplaceAllString(text, fmt.Sprintf("(%s$1)", baseURL))
text = anchorURLRegexp.ReplaceAllString(text, fmt.Sprintf("(%s$1)", docsURL))
text = tagNlRegexp.ReplaceAllString(text, "\n")
text = tagRegexp.ReplaceAllString(text, "")
text = html.UnescapeString(text)
text = trimSpaces(text)
return text
}
func removeHTML(text string) string {
text = tagElemRegexp.ReplaceAllString(text, "$1")
text = tagRegexp.ReplaceAllString(text, "")
text = html.UnescapeString(text)
return text
}
func splitTextToFitLine(text string) []string {
words := strings.Split(text, " ")
result := make([]string, 0)
line := strings.Builder{}
for _, word := range words {
if strings.Contains(word, "\n") {
ws := strings.Split(word, "\n")
if len(ws) != 2 {
os.Exit(2)
}
if line.Len()+len(ws[0])+1 > maxLineLen {
result = append(result, line.String())
line.Reset()
}
line.WriteString(ws[0] + " ")
result = append(result, line.String())
line.Reset()
word = ws[1]
}
if line.Len()+len(word)+1 > maxLineLen {
result = append(result, line.String())
line.Reset()
}
line.WriteString(word + " ")
}
if line.Len() != 0 {
result = append(result, line.String())
}
return result
}
func fitTextToLine(text, delimiter string) string {
lines := splitTextToFitLine(delimiter + text)
return strings.Join(lines, "\n"+delimiter)
}
func trimSpaces(text string) string {
text = strings.TrimSpace(text)
text = multiSpaceRegexp.ReplaceAllString(text, "$1")
return text
}
func preparePattern(pattern string) string {
return join(forEach(splitNl(pattern), strings.TrimSpace))
}
func snakeToCamelCase(text string) string {
nextUpper := true
result := strings.Builder{}
result.Grow(len(text))
for _, currentChar := range []byte(text) {
if currentChar == '_' {
nextUpper = true
continue
}
if nextUpper {
nextUpper = false
currentChar += 'A'
currentChar -= 'a'
result.WriteByte(currentChar)
} else {
result.WriteByte(currentChar)
}
}
return result.String()
}
func parseType(text string, optional bool) string {
text = removeHTML(text)
switch text {
case "String":
return "string"
case "Integer", "Int":
return "int"
case "Float number", "Float":
return "float64"
case "Boolean", "True":
return "bool"
case "Integer or String":
return "ChatID"
case "InputFile or String":
if optional {
return "*InputFile"
}
return "InputFile"
default:
if strings.HasPrefix(text, "Array of ") || strings.HasPrefix(text, "array of ") {
text = strings.TrimPrefix(strings.TrimPrefix(text, "Array of "), "array of ")
return "[]" + parseType(text, false)
}
if optional {
return "*" + text
}
return text
}
}
func uppercaseWords(text string) string {
text = strings.ReplaceAll(text, "Id ", "ID ")
text = strings.ReplaceAll(text, "Id\n", "ID\n")
text = strings.ReplaceAll(text, " id\n", " ID\n")
text = strings.ReplaceAll(text, "Id)", "ID)")
text = strings.ReplaceAll(text, "Ids", "IDs")
text = strings.ReplaceAll(text, "Id,", "ID,")
text = strings.ReplaceAll(text, "Id{", "ID{")
text = strings.ReplaceAll(text, " id ", " ID ")
text = strings.ReplaceAll(text, "Url ", "URL ")
text = strings.ReplaceAll(text, " url ", " URL ")
text = strings.ReplaceAll(text, " url's ", " URL's ")
text = strings.ReplaceAll(text, "url\n", "URL\n")
text = strings.ReplaceAll(text, "IpAddress", "IPAddress")
text = strings.ReplaceAll(text, "Botfather", "BotFather")
text = strings.ReplaceAll(text, "@channelusername", "@channel_username")
text = strings.ReplaceAll(text, "@supergroupusername", "@supergroup_username")
text = strings.ReplaceAll(text, "uRL", "url")
text = strings.ReplaceAll(text, "iPAddress", "ipAddress")
return text
}
func firstToLower(text string) string {
switch len(text) {
case 0:
return text
case 1:
return string(text[0] | ('a' - 'A'))
default:
return string(text[0]|('a'-'A')) + text[1:]
}
}
func firstToUpper(text string) string {
switch len(text) {
case 0:
return text
case 1:
return string(text[0] & ('a' - 'A' ^ 0xff))
default:
return string(text[0]&('a'-'A'^0xff)) + text[1:]
}
}