preserve alias case while lowercasing taxonomy

This commit is contained in:
Joel Scoble 2014-08-21 18:01:34 -05:00 committed by spf13
parent 348e123c9f
commit 4c735a7878
3 changed files with 35 additions and 3 deletions

View file

@ -36,6 +36,28 @@ func TestUgly(t *testing.T) {
} }
func TestMakePath(t *testing.T) { func TestMakePath(t *testing.T) {
tests := []struct {
input string
expected string
}{
{" Foo bar ", "Foo-bar"},
{"Foo.Bar/foo_Bar-Foo", "Foo.Bar/foo_Bar-Foo"},
{"fOO,bar:foo%bAR", "fOObarfoobAR"},
{"FOo/BaR.html", "FOo/BaR.html"},
{"трям/трям", "трям/трям"},
{"은행","은행"},
{"Банковский кассир","Банковский-кассир"},
}
for _, test := range tests {
output := MakePath(test.input)
if output != test.expected {
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
}
}
}
func TestMakeToLower(t *testing.T) {
tests := []struct { tests := []struct {
input string input string
expected string expected string
@ -45,6 +67,7 @@ func TestMakePath(t *testing.T) {
{"foo,bar:foo%bar", "foobarfoobar"}, {"foo,bar:foo%bar", "foobarfoobar"},
{"foo/bar.html", "foo/bar.html"}, {"foo/bar.html", "foo/bar.html"},
{"трям/трям", "трям/трям"}, {"трям/трям", "трям/трям"},
{"은행","은행"},
} }
for _, test := range tests { for _, test := range tests {

View file

@ -29,8 +29,17 @@ import (
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]") var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")
// Take a string with any characters and replace it so the string could be used in a path. // Take a string with any characters and replace it so the string could be used in a path.
// E.g. Social Media -> social-media // MakePath creates a Unicode sanitized string, with the spaces replaced, whilst
// preserving the original casing of the string.
// E.g. Social Media -> Social-Media
func MakePath(s string) string { func MakePath(s string) string {
return UnicodeSanitize(strings.Replace(strings.TrimSpace(s), " ", "-", -1))
}
// MakePathToLowerr creates a Unicode santized string, with the spaces replaced,
// and transformed to lower case.
// E.g. Social Media -> social-media
func MakePathToLower(s string) string {
return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1))) return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
} }

View file

@ -60,7 +60,7 @@ type OrderedTaxonomyEntry struct {
// KeyPrep... Taxonomies should be case insensitive. Can make it easily conditional later. // KeyPrep... Taxonomies should be case insensitive. Can make it easily conditional later.
func kp(in string) string { func kp(in string) string {
return helpers.MakePath(in) return helpers.MakePathToLower(in)
} }
func (i Taxonomy) Get(key string) WeightedPages { return i[kp(key)] } func (i Taxonomy) Get(key string) WeightedPages { return i[kp(key)] }