diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-07-30 18:46:04 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-07-31 23:16:46 +0300 |
commit | 8fb594bfb090c017d4e5cbb2905780221e202c41 (patch) | |
tree | f622b6aa90757827ea8f07cc27be692fb37b76c4 /tpl | |
parent | 9b4170ce768717adfbe9d97c46e38ceaec2ce994 (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 'tpl')
-rw-r--r-- | tpl/strings/init.go | 1 | ||||
-rw-r--r-- | tpl/strings/init_test.go | 3 | ||||
-rw-r--r-- | tpl/strings/strings.go | 9 | ||||
-rw-r--r-- | tpl/strings/strings_test.go | 3 |
4 files changed, 11 insertions, 5 deletions
diff --git a/tpl/strings/init.go b/tpl/strings/init.go index 45d694b97..4f240415a 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -116,6 +116,7 @@ func init() { []string{"title"}, [][2]string{ {`{{title "Bat man"}}`, `Bat Man`}, + {`{{title "somewhere over the rainbow"}}`, `Somewhere Over the Rainbow`}, }, ) diff --git a/tpl/strings/init_test.go b/tpl/strings/init_test.go index a8ad8ffdf..904e486f7 100644 --- a/tpl/strings/init_test.go +++ b/tpl/strings/init_test.go @@ -18,6 +18,7 @@ import ( "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/tpl/internal" + "github.com/spf13/viper" "github.com/stretchr/testify/require" ) @@ -26,7 +27,7 @@ func TestInit(t *testing.T) { var ns *internal.TemplateFuncsNamespace for _, nsf := range internal.TemplateFuncsNamespaceRegistry { - ns = nsf(&deps.Deps{}) + ns = nsf(&deps.Deps{Cfg: viper.New()}) if ns.Name == name { found = true break diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index ec95be730..5fe920433 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -27,14 +27,17 @@ import ( // New returns a new instance of the strings-namespaced template functions. func New(d *deps.Deps) *Namespace { - return &Namespace{deps: d} + titleCaseStyle := d.Cfg.GetString("titleCaseStyle") + titleFunc := helpers.GetTitleFunc(titleCaseStyle) + return &Namespace{deps: d, titleFunc: titleFunc} } // Namespace provides template functions for the "strings" namespace. // Most functions mimic the Go stdlib, but the order of the parameters may be // different to ease their use in the Go template system. type Namespace struct { - deps *deps.Deps + titleFunc func(s string) string + deps *deps.Deps } // CountRunes returns the number of runes in s, excluding whitepace. @@ -303,7 +306,7 @@ func (ns *Namespace) Title(s interface{}) (string, error) { return "", err } - return _strings.Title(ss), nil + return ns.titleFunc(ss), nil } // ToLower returns a copy of the input s with all Unicode letters mapped to their diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go index ee10ac759..64ec0864f 100644 --- a/tpl/strings/strings_test.go +++ b/tpl/strings/strings_test.go @@ -19,11 +19,12 @@ import ( "testing" "github.com/gohugoio/hugo/deps" + "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -var ns = New(&deps.Deps{}) +var ns = New(&deps.Deps{Cfg: viper.New()}) type tstNoStringer struct{} |