Add titleCaseStyle none and firstupper

Fixes #11204
This commit is contained in:
Bjørn Erik Pedersen 2023-07-05 19:32:57 +02:00
parent bf7ee8a91a
commit 12d3469dd1
3 changed files with 16 additions and 2 deletions

View file

@ -623,7 +623,13 @@ status = 404
Set `titleCaseStyle` to specify the title style used by the [title](/functions/title/) template function and the automatic section titles in Hugo. Set `titleCaseStyle` to specify the title style used by the [title](/functions/title/) template function and the automatic section titles in Hugo.
By default, Hugo adheres to the capitalization rules in the [Associated Press (AP) Stylebook]. Set `titleCaseStyle` to `chicago` if you would prefer to follow the [Chicago Manual of Style], or set if to `go` to use Go's convention of capitalizing every word. Can be one of:
* `ap` (default), the capitalization rules in the [Associated Press (AP) Stylebook]
* `chicago`, the [Chicago Manual of Style]
* `go`, Go's convention of capitalizing every word.
* `firstupper`, capitalize the first letter of the first word.
* `none`, no capitalization.
[Associated Press (AP) Stylebook]: https://www.apstylebook.com/ [Associated Press (AP) Stylebook]: https://www.apstylebook.com/
[Chicago Manual of Style]: https://www.chicagomanualofstyle.org/home.html [Chicago Manual of Style]: https://www.chicagomanualofstyle.org/home.html

View file

@ -205,6 +205,8 @@ func ReaderContains(r io.Reader, subslice []byte) bool {
// - "Go" (strings.Title) // - "Go" (strings.Title)
// - "AP" (see https://www.apstylebook.com/) // - "AP" (see https://www.apstylebook.com/)
// - "Chicago" (see http://www.chicagomanualofstyle.org/home.html) // - "Chicago" (see http://www.chicagomanualofstyle.org/home.html)
// - "FirstUpper" (only the first character is upper case)
// - "None" (no transformation)
// //
// If an unknown or empty style is provided, AP style is what you get. // If an unknown or empty style is provided, AP style is what you get.
func GetTitleFunc(style string) func(s string) string { func GetTitleFunc(style string) func(s string) string {
@ -214,6 +216,10 @@ func GetTitleFunc(style string) func(s string) string {
case "chicago": case "chicago":
tc := transform.NewTitleConverter(transform.ChicagoStyle) tc := transform.NewTitleConverter(transform.ChicagoStyle)
return tc.Title return tc.Title
case "none":
return func(s string) string { return s }
case "firstupper":
return FirstUpper
default: default:
tc := transform.NewTitleConverter(transform.APStyle) tc := transform.NewTitleConverter(transform.APStyle)
return tc.Title return tc.Title

View file

@ -203,7 +203,7 @@ func TestReaderContains(t *testing.T) {
} }
func TestGetTitleFunc(t *testing.T) { func TestGetTitleFunc(t *testing.T) {
title := "somewhere over the rainbow" title := "somewhere over the Rainbow"
c := qt.New(t) c := qt.New(t)
c.Assert(helpers.GetTitleFunc("go")(title), qt.Equals, "Somewhere Over The Rainbow") c.Assert(helpers.GetTitleFunc("go")(title), qt.Equals, "Somewhere Over The Rainbow")
@ -213,6 +213,8 @@ func TestGetTitleFunc(t *testing.T) {
c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow") c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow")
c.Assert(helpers.GetTitleFunc("")(title), qt.Equals, "Somewhere Over the Rainbow") c.Assert(helpers.GetTitleFunc("")(title), qt.Equals, "Somewhere Over the Rainbow")
c.Assert(helpers.GetTitleFunc("unknown")(title), qt.Equals, "Somewhere Over the Rainbow") c.Assert(helpers.GetTitleFunc("unknown")(title), qt.Equals, "Somewhere Over the Rainbow")
c.Assert(helpers.GetTitleFunc("none")(title), qt.Equals, title)
c.Assert(helpers.GetTitleFunc("firstupper")(title), qt.Equals, "Somewhere over the Rainbow")
} }
func BenchmarkReaderContains(b *testing.B) { func BenchmarkReaderContains(b *testing.B) {