-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreleaser.go
199 lines (189 loc) · 5.96 KB
/
releaser.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
// Package releaser provides string functions for cleaning and reformatting
// the names of release groups and partial URL paths.
package releaser
import (
"strings"
"github.com/Defacto2/releaser/fix"
"github.com/Defacto2/releaser/initialism"
"github.com/Defacto2/releaser/name"
)
var (
ins = initialism.Initialisms() // cache the initialisms
sp = name.Special() // cache the special names
)
// Cell formats the string to be used as a cell in a database table.
//
// - The removal of duplicate spaces
// - The removal of excess whitespace
// - If found "The " prefix from BBS and FTP named sites
// - The stripping of incompatible characters
//
// Compatible characters include: A-Z a-z À-Ö Ø-ö ø-ÿ 0-9 - , &
//
// Example:
//
// Cell(" Defacto2 demo group.") = "DEFACTO2 DEMO GROUP"
// Cell("the x bbs") = "X BBS"
// Cell("defacto2.net") = "DEFACTO2NET"
// Cell("TDT / TRSi") = "TDT TRSI"
// Cell("TDT,TRSi") = "TDT, TRSI"
func Cell(s string) string {
x := s
x = fix.StripChars(x)
x = fix.StripStart(x)
x = strings.TrimSpace(x)
x = fix.TrimThe(x)
x = fix.TrimSP(x)
return fix.Cell(x)
}
// Clean fixes the malformed string and applies title case formatting.
// It does not apply any name deobfuscations such as initials or abbreviations,
// as it only stylizes the string.
//
// - The removal of duplicate spaces
// - The removal of excess whitespace
// - If found "The " prefix from BBS and FTP named sites
// - The stripping of incompatible characters
//
// Compatible characters include: A-Z a-z À-Ö Ø-ö ø-ÿ 0-9 - , &
//
// Example:
//
// Clean(" Defacto2 demo group.") = "Defacto2 Demo Group"
// Clean("the x bbs") = "X BBS"
// Clean("The X Ftp") = "X FTP"
// Clean("tdt / trsi") = "Tdt Trsi" // behaves as a single group
// Clean("tdt,trsi") = "Tdt, TRSi" // behaves as two groups
func Clean(s string) string {
x := s
x = fix.StripChars(x)
x = fix.StripStart(x)
x = strings.TrimSpace(x)
x = fix.TrimThe(x)
x = fix.TrimSP(x)
return fix.Format(x)
}
// Humanize deobfuscates the URL path and returns the formatted, human-readable group name.
// The path is expected to be in the format of a URL path without the scheme or domain.
// If the URL path contains invalid characters then an empty string is returned.
//
// Example:
//
// Humanize("defacto2") = "Defacto2"
// Humanize("razor-1911-demo") = "Razor 1911 Demo"
// Humanize("razor-1911-demo-ampersand-skillion") = "Razor 1911 Demo & Skillion"
// Humanize("north-american-pirate_phreak-association") = "North American Pirate-Phreak Association"
// Humanize("coop") = "TDT / TRSi"
// Humanize("united-software-association*fairlight") =
// "United Software Association + Fairlight PC Division" // special name
// Humanize("razor-1911-demo*trsi") = "Razor 1911 Demo, TRSi"
// Humanize("razor-1911-demo#trsi") = "" // invalid # character
func Humanize(path string) string {
p := name.Path(strings.ToLower(path))
if special := p.String(); special != "" {
return special
}
s, err := name.Humanize(p)
if err != nil {
return ""
}
return Clean(s)
}
// Index deobfuscates the URL path and applies [releaser.Humanize] so that it can
// be stored in a database table as a releaser key and index in the database table.
func Index(path string) string {
p := name.Path(strings.ToLower(path))
s, err := name.Humanize(p)
if err != nil {
return ""
}
return strings.ToUpper(s)
}
// Link deobfuscates the URL path and applies [releaser.Humanize].
// In addition, the humanized name is formatted to be used as a link description.
// If the URL path contains invalid characters then an empty string is returned.
//
// Example:
//
// Link("razor-1911-demo*trsi") = "Razor 1911 Demo + TRSi"
// Link("class*paradigm*razor-1911") = "Class + Paradigm + Razor 1911"
// Link("united-software-association*fairlight") = "United Software Association + Fairlight PC Division"
func Link(path string) string {
s := Humanize(path)
return strings.ReplaceAll(s, ", ", " + ")
}
// Obfuscate cleans and formats the string for use as a URL path.
// The string is expected to be a release group name or an known initialism, acronym or special name.
//
// Beware that initialisms and acronyms often are not unique and an unexpected URL may be returned.
//
// Example:
//
// Obfuscate("ACiD Productions") = "acid-productions"
// Obfuscate("Razor 1911 Demo & Skillion") = "razor-1911-demo-ampersand-skillion"
// Obfuscate("TDU-Jam!") = "tdu_jam"
// Obfuscate("The 12AM BBS.") = "12am-bbs"
//
// Examples using unique, known initialisms:
//
// Obfuscate("fltdox") = "fairlight-dox"
// Obfuscate("tdt") = "the-dream-team"
//
// Examples using special names:
//
// Obfuscate("TDT / TRSi") = "coop"
// Obfuscate("United Software Association + Fairlight PC Division") = "united-software-association*fairlight"
func Obfuscate(s string) string {
x := s
x = fix.StripStart(x)
x = strings.TrimSpace(x)
for uri, special := range sp {
if strings.EqualFold(x, special) {
return string(uri)
}
}
for uri, initialisms := range ins {
for _, val := range initialisms {
if strings.EqualFold(x, val) {
return string(uri)
}
}
}
x = fix.StripChars(x)
x = fix.TrimThe(x)
x = fix.TrimSP(x)
c := name.Obfuscate(x)
return string(c)
}
// Title formats the string to be used as a title or the basis for a LIKE SQL query.
// Any known initialisms, acronyms or special names are deobfuscated.
//
// Example:
//
// Title("razor 1911") = "Razor 1911"
// Title("_.=[ RaZoR 1911 ]=._") = "Razor 1911"
// Title("COOP") = "TDT / TRSi"
// Title("tdt / trsi") = "TDT / TRSi"
// Title("nappa") = "North American Pirate-Phreak Association"
func Title(s string) string {
x := s
x = fix.StripStart(x)
x = strings.TrimSpace(x)
for _, special := range sp {
if strings.EqualFold(x, special) {
return special
}
}
for uri, initialisms := range ins {
for _, initialism := range initialisms {
if strings.EqualFold(x, initialism) {
return Humanize(string(uri))
}
}
}
x = fix.StripChars(x)
x = fix.TrimThe(x)
x = fix.TrimSP(x)
c := name.Obfuscate(x)
return Humanize(string(c))
}