From 78906a146b22b19a0e4d6c21ac94754b10650676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Fri, 10 May 2013 16:25:44 +0200 Subject: [PATCH] Change: split substitution (string and rune), add CustomRuneSub --- slug.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/slug.go b/slug.go index 026a738..0702f27 100644 --- a/slug.go +++ b/slug.go @@ -13,7 +13,9 @@ import ( var ( // 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 switch lang { case "de": - slug = Substitute(slug, deSub) + slug = SubstituteRune(slug, deSub) case "en": - slug = Substitute(slug, enSub) + slug = SubstituteRune(slug, enSub) case "pl": - slug = Substitute(slug, plSub) + slug = SubstituteRune(slug, plSub) 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 = unidecode.Unidecode(slug) @@ -54,8 +59,19 @@ func MakeLang(s string, lang string) (slug string) { return slug } -// Substitute string chars with provided substitution map. -func Substitute(s string, sub map[rune]string) (buf string) { +// Substitute returns string with superseded all substrings from +// 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 { if d, ok := sub[c]; ok { buf += d