diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-28 23:05:13 +0200 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-28 23:05:17 +0200 |
commit | be7404e3371caa2851111bb3bcdc1c26f7ed47f5 (patch) | |
tree | 30ea58c5d0c6c046860fe27c4a2c3766b442d114 /helpers | |
parent | be535832f7f4889351df84bad5059bae4db5a95d (diff) |
Support `Fish and Chips` section
Section names are also used as the title of the list pages, but naming section folders as `Fish and Chips` and similar didn't work very well.
This commit fixes that.
This commit also changes the title casing of the section titles. Some may argue that this is a breaking change, but the old behaviour was also pretty broken,
even for languages that use title capitalizations, as it didn't follow any particular style guide, `fish and chips` became `Fish And Chips` etc.
Now it just turns the first letter into upper case, so `Fish and Chips` will be left as `Fish and Chips`.
People wanting the good old behaviour can use the `title` template func.
Fixes #1176
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/general.go | 11 | ||||
-rw-r--r-- | helpers/general_test.go | 18 | ||||
-rw-r--r-- | helpers/path.go | 6 |
3 files changed, 32 insertions, 3 deletions
diff --git a/helpers/general.go b/helpers/general.go index df08c887f..908751da0 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -25,6 +25,8 @@ import ( "reflect" "strings" "sync" + "unicode" + "unicode/utf8" "github.com/spf13/cast" bp "github.com/spf13/hugo/bufferpool" @@ -78,6 +80,15 @@ func GuessType(in string) string { return "unknown" } +// FirstUpper returns a string with the first character as upper case. +func FirstUpper(s string) string { + if s == "" { + return "" + } + r, n := utf8.DecodeRuneInString(s) + return string(unicode.ToUpper(r)) + s[n:] +} + // ReaderToBytes takes an io.Reader argument, reads from it // and returns bytes. func ReaderToBytes(lines io.Reader) []byte { diff --git a/helpers/general_test.go b/helpers/general_test.go index 31c478200..c801ad441 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -33,6 +33,24 @@ func TestGuessType(t *testing.T) { } } +func TestFirstUpper(t *testing.T) { + for i, this := range []struct { + in string + expect string + }{ + {"foo", "Foo"}, + {"foo bar", "Foo bar"}, + {"Foo Bar", "Foo Bar"}, + {"", ""}, + {"å", "Å"}, + } { + result := FirstUpper(this.in) + if result != this.expect { + t.Errorf("[%d] got %s but expected %s", i, result, this.expect) + } + } +} + func TestBytesToReader(t *testing.T) { asBytes := ReaderToBytes(strings.NewReader("Hello World!")) asReader := BytesToReader(asBytes) diff --git a/helpers/path.go b/helpers/path.go index 0dee8db4c..9f47549d2 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -92,7 +92,7 @@ func UnicodeSanitize(s string) string { target := make([]rune, 0, len(source)) for _, r := range source { - if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.' || r == '/' || r == '_' || r == '-' || r == '#' { + if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.' || r == '/' || r == '\\' || r == '_' || r == '-' || r == '#' { target = append(target, r) } } @@ -332,8 +332,8 @@ func GetRelativePath(path, base string) (final string, err error) { return "", err } - if strings.HasSuffix(path, "/") && !strings.HasSuffix(name, "/") { - name += "/" + if strings.HasSuffix(filepath.FromSlash(path), FilePathSeparator) && !strings.HasSuffix(name, FilePathSeparator) { + name += FilePathSeparator } return name, nil } |