summaryrefslogtreecommitdiffstats
path: root/helpers/general.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-07-30 17:46:04 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-07-31 22:16:46 +0200
commit8fb594bfb090c017d4e5cbb2905780221e202c41 (patch)
treef622b6aa90757827ea8f07cc27be692fb37b76c4 /helpers/general.go
parent9b4170ce768717adfbe9d97c46e38ceaec2ce994 (diff)
Make the title case style guide configurable
This works for the `title` func and the other places where Hugo makes title case. * AP style (new default) * Chicago style * Go style (what we have today) Fixes #989
Diffstat (limited to 'helpers/general.go')
-rw-r--r--helpers/general.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/helpers/general.go b/helpers/general.go
index 552e4d0bf..a064309d3 100644
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -26,6 +26,8 @@ import (
"unicode"
"unicode/utf8"
+ "github.com/jdkato/prose/transform"
+
bp "github.com/gohugoio/hugo/bufferpool"
"github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman"
@@ -194,6 +196,29 @@ func ReaderContains(r io.Reader, subslice []byte) bool {
return false
}
+// GetTitleFunc returns a func that can be used to transform a string to
+// title case.
+//
+// The supported styles are
+//
+// - "Go" (strings.Title)
+// - "AP" (see https://www.apstylebook.com/)
+// - "Chicago" (see http://www.chicagomanualofstyle.org/home.html)
+//
+// If an unknown or empty style is provided, AP style is what you get.
+func GetTitleFunc(style string) func(s string) string {
+ switch strings.ToLower(style) {
+ case "go":
+ return strings.Title
+ case "chicago":
+ tc := transform.NewTitleConverter(transform.ChicagoStyle)
+ return tc.Title
+ default:
+ tc := transform.NewTitleConverter(transform.APStyle)
+ return tc.Title
+ }
+}
+
// HasStringsPrefix tests whether the string slice s begins with prefix slice s.
func HasStringsPrefix(s, prefix []string) bool {
return len(s) >= len(prefix) && compareStringSlices(s[0:len(prefix)], prefix)