This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.go
333 lines (310 loc) · 8.7 KB
/
rename.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Package rename handles the renaming and formatting of the group title.
package rename
import (
"database/sql"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/Defacto2/df2/pkg/database"
"github.com/Defacto2/df2/pkg/str"
"github.com/gookit/color"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const space = " "
// Clean a malformed group name and save the fix to the database.
func Clean(db *sql.DB, w io.Writer, name string) (bool, error) {
if db == nil {
return false, database.ErrDB
}
if w == nil {
w = io.Discard
}
fix := CleanS(name)
if fix == name {
return false, nil
}
status := str.Y()
count, err := Update(db, fix, name)
success := true
if err != nil {
status = str.X()
success = false
}
fmt.Fprintf(w, "%s %q %s %s (%d)\n",
status, name, color.Question.Sprint("⟫"), color.Info.Sprint(fix), count)
return success, nil
}
// CleanS fixes the malformed string.
func CleanS(s string) string {
f := database.TrimSP(s)
f = database.StripChars(f)
f = database.StripStart(f)
f = strings.TrimSpace(f)
f = TrimThe(f)
f = Format(f)
return f
}
// Format returns a copy of s with custom formatting.
func Format(s string) string {
const (
acronym = 3
separator = ", "
)
if len(s) <= acronym {
return strings.ToUpper(s)
}
groups := strings.Split(s, separator)
for j, group := range groups {
g := strings.ToLower(strings.TrimSpace(group))
g = FmtSyntax(g)
if fix := FmtExact(g); fix != "" {
groups[j] = fix
continue
}
words := strings.Split(g, space)
last := len(words) - 1
for i, word := range words {
word = TrimDot(word)
if fix := fixHyphens(word); fix != "" {
words[i] = fix
continue
}
words[i] = fixWord(word, i, last)
}
groups[j] = strings.Join(words, space)
}
return strings.Join(groups, separator)
}
// FmtSyntax formats the special ampersand (&) character
// to be usable with the URL in use by the group.
func FmtSyntax(w string) string {
if !strings.Contains(w, "&") {
return w
}
s := w
trimDupes := regexp.MustCompile(`\&+`)
s = trimDupes.ReplaceAllString(s, "&")
trimPrefix := regexp.MustCompile(`^\&+`)
s = trimPrefix.ReplaceAllString(s, "")
trimSuffix := regexp.MustCompile(`\&+$`)
s = trimSuffix.ReplaceAllString(s, "")
addWhitespace := regexp.MustCompile(`(\S)\&(\S)`) // \S matches any character that's not whitespace
s = addWhitespace.ReplaceAllString(s, "$1 & $2")
return s
}
// TrimDot removes any trailing dots from s.
func TrimDot(s string) string {
const short = 2
if len(s) < short {
return s
}
if l := s[len(s)-1:]; l == "." {
return s[:len(s)-1]
}
return s
}
// TrimThe removes 'the' prefix from s.
func TrimThe(s string) string {
const short = 2
a := strings.Split(s, space)
if len(a) < short {
return s
}
l := a[len(a)-1]
if strings.EqualFold(a[0], "the") && (l == "BBS" || l == "FTP") {
return strings.Join(a[1:], space) // drop "the" prefix
}
return s
}
// Update replaces all instances of the group name with a new group name.
func Update(db *sql.DB, newName, group string) (int64, error) {
stmt, err := db.Prepare("UPDATE `files` SET group_brand_for=?," +
" group_brand_by=? WHERE (group_brand_for=? OR group_brand_by=?)")
if err != nil {
return 0, fmt.Errorf("rename statement: %w", err)
}
defer stmt.Close()
res, err := stmt.Exec(newName, newName, group, group)
if err != nil {
return 0, fmt.Errorf("rename exec: %w", err)
}
count, err := res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("rename rows affected: %w", err)
}
return count, nil
}
// FmtExact matches the exact group name to apply a format.
func FmtExact(g string) string {
switch strings.ToLower(g) {
// all uppercase full groups
case "anz ftp", "mor ftp", "msv ftp", "nos ftp", "pox ftp", "scf ftp", "scsi ftp",
"tbb ftp", "tog ftp", "top ftp", "tph-qqt", "tpw ftp", "u4ea ftp", "zoo ftp",
"3wa bbs", "acb bbs", "bcp bbs", "cwl bbs", "es bbs", "dv8 bbs", "fic bbs",
"lms bbs", "lta bbs", "ls bbs", "lpc bbs", "og bbs", "okc bbs", "uct bbs", "tsi bbs",
"tsc bbs", "trt 2001 bbs", "tiw bbs", "tfz 2 bbs", "ppps bbs", "pp bbs", "pmc bbs",
"crsiso", "tus fx", "lsdiso", "cnx ftp", "tph-qqt ftp", "swat", "psxdox", "nsdap",
"new dtl", "lkcc", "core", "qed bbs", "psi bbs", "tcsm bbs",
"2nd2none bbs", "ckc bbs", "beer":
return strings.ToUpper(g)
case "scenet":
// all lowercase full groups
return strings.ToLower(g)
}
return FmtByName(g)
}
// FmtByName formats the group name using stylized casing.
func FmtByName(s string) string {
name := strings.ToLower(s)
fmtGroups := map[string]string{
"drm ftp": "dRM FTP",
"dst ftp": "dst FTP",
"nofx bbs": "NoFX BBS",
"noclass": "NoClass",
"pjs tower": "PJs Tower BBS",
"tsg ftp": "tSG FTP",
"xquizit ftp": "XquiziT FTP",
"vdr lake ftp": "VDR Lake FTP",
"ptl club": "PTL Club",
"dvtiso": "DVTiSO",
"rhvid": "RHViD",
"trsi": "TRSi",
"htbzine": "HTBZine",
"mci escapes": "mci escapes",
"79th trac": "79th TRAC",
"unreal magazine": "UnReal Magazine",
"ice weekly newsletter": "iCE Weekly Newsletter",
"biased": "bIASED",
"dreadloc": "DREADLoC",
"cybermail": "CyberMail",
"excretion anarchy": "eXCReTION Anarchy",
"pocketheaven": "PocketHeaven",
"rzsoft ftp": "RZSoft FTP",
}
for group, replace := range fmtGroups {
if name == group {
return replace
}
}
renGroups := map[string]string{
"2000 ad": "2000AD",
"hashx": "Hash X",
"phoenixbbs": "Phoenix BBS",
}
for group, replace := range renGroups {
if name == group {
return replace
}
}
return ""
}
func fmtWord(w string) string {
switch w {
case "3d", "abc", "acdc", "ad", "amf", "ansi", "asm", "au", "bbc", "bbs", "bc", "cd",
"cgi", "diz", "dox", "eu", "faq", "fbi", "ftp", "fr", "fx", "fxp", "hq", "id", "ii",
"iii", "iso", "kgb", "pc", "pcb", "pcp", "pda", "psx", "pwa", "ssd", "st", "tnt",
"tsr", "ufo", "uk", "us", "usa", "uss", "ussr", "vcd", "whq", "mp3", "rom", "fm",
"am", "pm", "gbc", "gif", "xxx", "rpm":
return strings.ToUpper(w)
case "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
"10th", "11th", "12th", "13th":
return strings.ToLower(w)
case "7of9":
return strings.ToLower(w)
default:
return ""
}
}
func fmtSuffix(w string, title cases.Caser) string {
uppers := []string{"ad", "bc", "am", "pm"}
for _, x := range uppers {
if val := upperSuffix(w, x); val != "" {
return val
}
}
switch {
case strings.HasSuffix(w, "dox"):
val := strings.TrimSuffix(w, "dox")
return fmt.Sprintf("%sDox", title.String(val))
case strings.HasSuffix(w, "fxp"):
val := strings.TrimSuffix(w, "fxp")
return fmt.Sprintf("%sFXP", title.String(val))
case strings.HasSuffix(w, "iso"):
val := strings.TrimSuffix(w, "iso")
return fmt.Sprintf("%sISO", title.String(val))
case strings.HasSuffix(w, "nfo"):
val := strings.TrimSuffix(w, "nfo")
return fmt.Sprintf("%sNFO", title.String(val))
case strings.HasPrefix(w, "pc-"):
val := strings.TrimPrefix(w, "pc-")
return fmt.Sprintf("PC-%s", title.String(val))
case strings.HasPrefix(w, "lsd"):
val := strings.TrimPrefix(w, "lsd")
return fmt.Sprintf("LSD%s", title.String(val))
}
return ""
}
func upperSuffix(s, suffix string) string {
if !strings.HasSuffix(s, suffix) {
return ""
}
x := strings.TrimSuffix(s, suffix)
if val, err := strconv.Atoi(x); err == nil {
return fmt.Sprintf("%d%s", val, strings.ToUpper(suffix))
}
return ""
}
func fmtSequence(w string, i int) string {
if i != 0 {
return ""
}
switch w { //nolint:gocritic
case "inc":
// note: Format() applies UPPER to all 3 letter or smaller words
return strings.ToUpper(w)
}
return ""
}
func fmtConnect(w string, position, last int) string {
const first = 0
if position == first || position == last {
return ""
}
switch w {
case "a", "as", "and", "at", "by", "el", "of", "for", "from", "in", "is", "or", "tha",
"the", "to", "with":
return strings.ToLower(w)
}
return ""
}
func fixWord(w string, position, last int) string {
if fix := fmtConnect(w, position, last); fix != "" {
return fix
}
if fix := fmtWord(w); fix != "" {
return fix
}
title := cases.Title(language.English, cases.NoLower)
if fix := fmtSuffix(w, title); fix != "" {
return fix
}
if fix := fmtSequence(w, position); fix != "" {
return fix
}
return title.String(w)
}
func fixHyphens(w string) string {
const hyphen = "-"
if !strings.Contains(w, hyphen) {
return ""
}
compounds := strings.Split(w, hyphen)
last := len(compounds) - 1
for i, word := range compounds {
compounds[i] = fixWord(word, i, last)
}
return strings.Join(compounds, hyphen)
}