2020-05-31 15:17:01 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Title returns the string with the first characters of each word converted to uppercase.
|
|
|
|
func Title(s string) string {
|
|
|
|
s = strings.ReplaceAll(s, "_", " ")
|
2020-06-01 11:23:15 +02:00
|
|
|
s = strings.Trim(s, "/ -")
|
2020-07-11 16:46:29 +02:00
|
|
|
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:23:15 +02:00
|
|
|
blocks := strings.Split(s, "/")
|
|
|
|
result := make([]string, 0, len(blocks))
|
2020-05-31 15:17:01 +02:00
|
|
|
|
2020-06-01 11:23:15 +02:00
|
|
|
for _, block := range blocks {
|
|
|
|
words := strings.Fields(block)
|
|
|
|
|
|
|
|
if len(words) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, w := range words {
|
2020-06-01 13:22:19 +02:00
|
|
|
search := strings.ToLower(strings.Trim(w, ":.,;!?"))
|
|
|
|
|
|
|
|
if match, ok := SpecialWords[search]; ok {
|
|
|
|
words[i] = strings.Replace(strings.ToLower(w), search, match, 1)
|
2022-09-14 20:37:24 +02:00
|
|
|
} else if i > 0 && SmallWords[search] == true {
|
2020-06-01 11:23:15 +02:00
|
|
|
words[i] = strings.ToLower(w)
|
|
|
|
} else {
|
|
|
|
prev := ' '
|
|
|
|
words[i] = strings.Map(
|
|
|
|
func(r rune) rune {
|
|
|
|
if isSeparator(prev) {
|
|
|
|
prev = r
|
|
|
|
return unicode.ToTitle(r)
|
|
|
|
}
|
|
|
|
prev = r
|
|
|
|
return r
|
|
|
|
},
|
|
|
|
w)
|
2020-05-31 15:17:01 +02:00
|
|
|
}
|
2020-06-01 11:23:15 +02:00
|
|
|
}
|
2020-05-31 15:17:01 +02:00
|
|
|
|
2020-06-01 11:23:15 +02:00
|
|
|
result = append(result, strings.Join(words, " "))
|
2020-05-31 15:17:01 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 11:23:15 +02:00
|
|
|
return strings.Join(result, " / ")
|
2020-05-31 15:17:01 +02:00
|
|
|
}
|