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
This commit is contained in:
Bjørn Erik Pedersen
2017-07-30 17:46:04 +02:00
parent 9b4170ce76
commit 8fb594bfb0
10 changed files with 77 additions and 7 deletions

View File

@@ -116,6 +116,7 @@ func init() {
[]string{"title"},
[][2]string{
{`{{title "Bat man"}}`, `Bat Man`},
{`{{title "somewhere over the rainbow"}}`, `Somewhere Over the Rainbow`},
},
)

View File

@@ -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

View File

@@ -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

View File

@@ -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{}