| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "unicode" |
| 9 | |
| 10 | norm "golang.org/x/text/unicode/norm" |
| 11 | ) |
| 12 | |
| 13 | func envInt(key string, fallback int) int { |
| 14 | if value := os.Getenv(key); value != "" { |
| 15 | if parsed, err := strconv.Atoi(value); err == nil && parsed > 0 { |
| 16 | return parsed |
| 17 | } |
| 18 | } |
| 19 | return fallback |
| 20 | } |
| 21 | |
| 22 | func envString(key string, fallback string) string { |
| 23 | if value := os.Getenv(key); value != "" { |
| 24 | return value |
| 25 | } |
| 26 | return fallback |
| 27 | } |
| 28 | |
| 29 | var nonAlnum = regexp.MustCompile(`[^a-z0-9]+`) |
| 30 | |
| 31 | var translit = strings.NewReplacer( |
| 32 | "’", "", "'", "", |
| 33 | "“", "", "”", "", "‘", "", |
| 34 | "—", " ", "–", " ", "‑", " ", |
| 35 | "æ", "ae", "ø", "o", "ð", "d", "þ", "th", "ß", "ss", "œ", "oe", |
| 36 | ) |
| 37 | |
| 38 | func Slugify(s string) string { |
| 39 | s = strings.ToLower(s) |
| 40 | s = translit.Replace(s) |
| 41 | s = norm.NFD.String(s) |
| 42 | s = strings.Map(func(r rune) rune { |
| 43 | if unicode.Is(unicode.Mn, r) { |
| 44 | return -1 |
| 45 | } |
| 46 | return r |
| 47 | }, s) |
| 48 | s = nonAlnum.ReplaceAllString(s, "-") |
| 49 | return strings.Trim(s, "-") |
| 50 | } |