Add unicode support for aliases, indexes, urlize template filter.

Now aliases and indexes are not restricted ASCII letters and can include
any unicode letters.
This commit is contained in:
Anton Ageev
2014-02-02 18:18:01 +04:00
committed by spf13
parent 24ffe04360
commit 11ca84f8cb
5 changed files with 77 additions and 4 deletions

View File

@@ -14,16 +14,43 @@
package helpers
import (
"net/url"
"regexp"
"strings"
"unicode"
)
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")
func Urlize(url string) string {
return Sanitize(strings.ToLower(strings.Replace(strings.TrimSpace(url), " ", "-", -1)))
func MakePath(s string) string {
return unicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
}
func Urlize(uri string) string {
sanitized := MakePath(uri)
// escape unicode letters
parsedUri, err := url.Parse(sanitized)
if err != nil {
// if net/url can not parse URL it's meaning Sanitize works incorrect
panic(err)
}
return parsedUri.String()
}
func Sanitize(s string) string {
return sanitizeRegexp.ReplaceAllString(s, "")
}
func unicodeSanitize(s string) string {
source := []rune(s)
target := make([]rune, 0, len(source))
for _, r := range source {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.' || r == '/' || r == '_' || r == '-' {
target = append(target, r)
}
}
return string(target)
}

45
helpers/templates_test.go Normal file
View File

@@ -0,0 +1,45 @@
package helpers
import (
"testing"
)
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 TestUrlize(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"},
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
}
for _, test := range tests {
output := Urlize(test.input)
if output != test.expected {
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
}
}
}