Change: split substitution (string and rune), add CustomRuneSub

This commit is contained in:
Dobrosław Żybort 2013-05-10 16:25:44 +02:00
parent 4d153f396b
commit 78906a146b

32
slug.go
View File

@ -13,7 +13,9 @@ import (
var ( var (
// Custom substitution map // Custom substitution map
CustomSub map[rune]string CustomSub map[string]string
// Custom rune substitution map
CustomRuneSub map[rune]string
) )
//============================================================================= //=============================================================================
@ -32,16 +34,19 @@ func MakeLang(s string, lang string) (slug string) {
// Select substitution language // Select substitution language
switch lang { switch lang {
case "de": case "de":
slug = Substitute(slug, deSub) slug = SubstituteRune(slug, deSub)
case "en": case "en":
slug = Substitute(slug, enSub) slug = SubstituteRune(slug, enSub)
case "pl": case "pl":
slug = Substitute(slug, plSub) slug = SubstituteRune(slug, plSub)
default: // fallback to "en" if lang not found default: // fallback to "en" if lang not found
slug = Substitute(slug, enSub) slug = SubstituteRune(slug, enSub)
} }
slug = Substitute(slug, defaultSub) slug = SubstituteRune(slug, defaultSub)
// Custom substitutions
slug = SubstituteRune(slug, CustomRuneSub)
slug = Substitute(slug, CustomSub) slug = Substitute(slug, CustomSub)
slug = unidecode.Unidecode(slug) slug = unidecode.Unidecode(slug)
@ -54,8 +59,19 @@ func MakeLang(s string, lang string) (slug string) {
return slug return slug
} }
// Substitute string chars with provided substitution map. // Substitute returns string with superseded all substrings from
func Substitute(s string, sub map[rune]string) (buf string) { // provided substitution map.
func Substitute(s string, sub map[string]string) (buf string) {
buf = s
for key, val := range sub {
buf = strings.Replace(s, key, val, -1)
}
return
}
// SubstituteRune substitutes string chars with provided rune
// substitution map.
func SubstituteRune(s string, sub map[rune]string) (buf string) {
for _, c := range s { for _, c := range s {
if d, ok := sub[c]; ok { if d, ok := sub[c]; ok {
buf += d buf += d