Align topdata wiki canonical paths (#11)
Reviewed-on: https://gitea.westgate.pw/ShadowsOverWestgate/sow-tools/pulls/11 Co-authored-by: Michał Piasecki <mpiasecki720@protonmail.com> Co-committed-by: Michał Piasecki <mpiasecki720@protonmail.com>
This commit is contained in:
@@ -8,6 +8,16 @@ import (
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
var reservedCanonicalWikiFirstSegments = map[string]struct{}{
|
||||
"admin": {},
|
||||
"api": {},
|
||||
"category": {},
|
||||
"compose": {},
|
||||
"edit": {},
|
||||
"namespace": {},
|
||||
"search": {},
|
||||
}
|
||||
|
||||
func canonicalWikiSegment(value string) string {
|
||||
value = html.UnescapeString(strings.TrimSpace(value))
|
||||
var b strings.Builder
|
||||
@@ -40,6 +50,72 @@ func canonicalWikiSegment(value string) string {
|
||||
return strings.Trim(b.String(), "_")
|
||||
}
|
||||
|
||||
func canonicalWikiSegmentFoldedKey(value string) string {
|
||||
return strings.Join(strings.Fields(strings.ToLower(strings.ReplaceAll(canonicalWikiSegment(value), "_", " "))), " ")
|
||||
}
|
||||
|
||||
func canonicalWikiSlashPath(value string) string {
|
||||
segments := []string{}
|
||||
for _, part := range strings.Split(strings.Trim(strings.TrimSpace(value), "/"), "/") {
|
||||
canonical := canonicalWikiSegment(part)
|
||||
if canonical != "" {
|
||||
segments = append(segments, canonical)
|
||||
}
|
||||
}
|
||||
return strings.Join(segments, "/")
|
||||
}
|
||||
|
||||
func canonicalWikiSlashPathFoldedKey(value string) string {
|
||||
segments := []string{}
|
||||
for _, part := range strings.Split(strings.Trim(strings.TrimSpace(value), "/"), "/") {
|
||||
folded := canonicalWikiSegmentFoldedKey(part)
|
||||
if folded != "" {
|
||||
segments = append(segments, folded)
|
||||
}
|
||||
}
|
||||
return strings.Join(segments, "/")
|
||||
}
|
||||
|
||||
func canonicalWikiSlashPathSegments(value string) []string {
|
||||
segments := []string{}
|
||||
for _, part := range strings.Split(strings.Trim(strings.TrimSpace(value), "/"), "/") {
|
||||
canonical := canonicalWikiSegment(part)
|
||||
if canonical != "" {
|
||||
segments = append(segments, canonical)
|
||||
}
|
||||
}
|
||||
return segments
|
||||
}
|
||||
|
||||
func isReservedCanonicalWikiFirstSegment(path string) bool {
|
||||
segments := canonicalWikiSlashPathSegments(path)
|
||||
if len(segments) == 0 {
|
||||
return false
|
||||
}
|
||||
first := strings.ToLower(segments[0])
|
||||
if isCanonicalWikiNumericSegment(first) {
|
||||
return true
|
||||
}
|
||||
_, reserved := reservedCanonicalWikiFirstSegments[first]
|
||||
return reserved
|
||||
}
|
||||
|
||||
func isCanonicalWikiNumericSegment(segment string) bool {
|
||||
if segment == "" {
|
||||
return false
|
||||
}
|
||||
for _, r := range segment {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isNestedCanonicalWikiPath(path string) bool {
|
||||
return len(canonicalWikiSlashPathSegments(path)) > 2
|
||||
}
|
||||
|
||||
func matchWikiTransliterationCase(r rune, value string) string {
|
||||
if unicode.IsUpper(r) {
|
||||
if len(value) == 1 {
|
||||
|
||||
Reference in New Issue
Block a user