Create a struct with all of Hugo's config options

Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
This commit is contained in:
Bjørn Erik Pedersen
2023-01-04 18:24:36 +01:00
parent 6aededf6b4
commit 241b21b0fd
337 changed files with 13377 additions and 14898 deletions

View File

@@ -31,7 +31,7 @@ import (
// FrontMatterHandler maps front matter into Page fields and .Params.
// Note that we currently have only extracted the date logic.
type FrontMatterHandler struct {
fmConfig frontmatterConfig
fmConfig FrontmatterConfig
dateHandler frontMatterFieldHandler
lastModHandler frontMatterFieldHandler
@@ -159,11 +159,15 @@ func (f FrontMatterHandler) newChainedFrontMatterFieldHandler(handlers ...frontM
}
}
type frontmatterConfig struct {
date []string
lastmod []string
publishDate []string
expiryDate []string
type FrontmatterConfig struct {
// Controls how the Date is set from front matter.
Date []string
// Controls how the Lastmod is set from front matter.
Lastmod []string
// Controls how the PublishDate is set from front matter.
PublishDate []string
// Controls how the ExpiryDate is set from front matter.
ExpiryDate []string
}
const (
@@ -185,16 +189,16 @@ const (
)
// This is the config you get when doing nothing.
func newDefaultFrontmatterConfig() frontmatterConfig {
return frontmatterConfig{
date: []string{fmDate, fmPubDate, fmLastmod},
lastmod: []string{fmGitAuthorDate, fmLastmod, fmDate, fmPubDate},
publishDate: []string{fmPubDate, fmDate},
expiryDate: []string{fmExpiryDate},
func newDefaultFrontmatterConfig() FrontmatterConfig {
return FrontmatterConfig{
Date: []string{fmDate, fmPubDate, fmLastmod},
Lastmod: []string{fmGitAuthorDate, fmLastmod, fmDate, fmPubDate},
PublishDate: []string{fmPubDate, fmDate},
ExpiryDate: []string{fmExpiryDate},
}
}
func newFrontmatterConfig(cfg config.Provider) (frontmatterConfig, error) {
func DecodeFrontMatterConfig(cfg config.Provider) (FrontmatterConfig, error) {
c := newDefaultFrontmatterConfig()
defaultConfig := c
@@ -204,13 +208,13 @@ func newFrontmatterConfig(cfg config.Provider) (frontmatterConfig, error) {
loki := strings.ToLower(k)
switch loki {
case fmDate:
c.date = toLowerSlice(v)
c.Date = toLowerSlice(v)
case fmPubDate:
c.publishDate = toLowerSlice(v)
c.PublishDate = toLowerSlice(v)
case fmLastmod:
c.lastmod = toLowerSlice(v)
c.Lastmod = toLowerSlice(v)
case fmExpiryDate:
c.expiryDate = toLowerSlice(v)
c.ExpiryDate = toLowerSlice(v)
}
}
}
@@ -221,10 +225,10 @@ func newFrontmatterConfig(cfg config.Provider) (frontmatterConfig, error) {
return out
}
c.date = expander(c.date, defaultConfig.date)
c.publishDate = expander(c.publishDate, defaultConfig.publishDate)
c.lastmod = expander(c.lastmod, defaultConfig.lastmod)
c.expiryDate = expander(c.expiryDate, defaultConfig.expiryDate)
c.Date = expander(c.Date, defaultConfig.Date)
c.PublishDate = expander(c.PublishDate, defaultConfig.PublishDate)
c.Lastmod = expander(c.Lastmod, defaultConfig.Lastmod)
c.ExpiryDate = expander(c.ExpiryDate, defaultConfig.ExpiryDate)
return c, nil
}
@@ -264,16 +268,11 @@ func toLowerSlice(in any) []string {
// NewFrontmatterHandler creates a new FrontMatterHandler with the given logger and configuration.
// If no logger is provided, one will be created.
func NewFrontmatterHandler(logger loggers.Logger, cfg config.Provider) (FrontMatterHandler, error) {
func NewFrontmatterHandler(logger loggers.Logger, frontMatterConfig FrontmatterConfig) (FrontMatterHandler, error) {
if logger == nil {
logger = loggers.NewErrorLogger()
}
frontMatterConfig, err := newFrontmatterConfig(cfg)
if err != nil {
return FrontMatterHandler{}, err
}
allDateKeys := make(map[string]bool)
addKeys := func(vals []string) {
for _, k := range vals {
@@ -283,10 +282,10 @@ func NewFrontmatterHandler(logger loggers.Logger, cfg config.Provider) (FrontMat
}
}
addKeys(frontMatterConfig.date)
addKeys(frontMatterConfig.expiryDate)
addKeys(frontMatterConfig.lastmod)
addKeys(frontMatterConfig.publishDate)
addKeys(frontMatterConfig.Date)
addKeys(frontMatterConfig.ExpiryDate)
addKeys(frontMatterConfig.Lastmod)
addKeys(frontMatterConfig.PublishDate)
f := FrontMatterHandler{logger: logger, fmConfig: frontMatterConfig, allDateKeys: allDateKeys}
@@ -300,7 +299,7 @@ func NewFrontmatterHandler(logger loggers.Logger, cfg config.Provider) (FrontMat
func (f *FrontMatterHandler) createHandlers() error {
var err error
if f.dateHandler, err = f.createDateHandler(f.fmConfig.date,
if f.dateHandler, err = f.createDateHandler(f.fmConfig.Date,
func(d *FrontMatterDescriptor, t time.Time) {
d.Dates.FDate = t
setParamIfNotSet(fmDate, t, d)
@@ -308,7 +307,7 @@ func (f *FrontMatterHandler) createHandlers() error {
return err
}
if f.lastModHandler, err = f.createDateHandler(f.fmConfig.lastmod,
if f.lastModHandler, err = f.createDateHandler(f.fmConfig.Lastmod,
func(d *FrontMatterDescriptor, t time.Time) {
setParamIfNotSet(fmLastmod, t, d)
d.Dates.FLastmod = t
@@ -316,7 +315,7 @@ func (f *FrontMatterHandler) createHandlers() error {
return err
}
if f.publishDateHandler, err = f.createDateHandler(f.fmConfig.publishDate,
if f.publishDateHandler, err = f.createDateHandler(f.fmConfig.PublishDate,
func(d *FrontMatterDescriptor, t time.Time) {
setParamIfNotSet(fmPubDate, t, d)
d.Dates.FPublishDate = t
@@ -324,7 +323,7 @@ func (f *FrontMatterHandler) createHandlers() error {
return err
}
if f.expiryDateHandler, err = f.createDateHandler(f.fmConfig.expiryDate,
if f.expiryDateHandler, err = f.createDateHandler(f.fmConfig.ExpiryDate,
func(d *FrontMatterDescriptor, t time.Time) {
setParamIfNotSet(fmExpiryDate, t, d)
d.Dates.FExpiryDate = t

View File

@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package pagemeta
package pagemeta_test
import (
"strings"
@@ -19,54 +19,20 @@ import (
"time"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/testconfig"
"github.com/gohugoio/hugo/resources/page/pagemeta"
"github.com/gohugoio/hugo/resources/resource"
qt "github.com/frankban/quicktest"
)
func TestDateAndSlugFromBaseFilename(t *testing.T) {
t.Parallel()
c := qt.New(t)
tests := []struct {
name string
date string
slug string
}{
{"page.md", "0001-01-01", ""},
{"2012-09-12-page.md", "2012-09-12", "page"},
{"2018-02-28-page.md", "2018-02-28", "page"},
{"2018-02-28_page.md", "2018-02-28", "page"},
{"2018-02-28 page.md", "2018-02-28", "page"},
{"2018-02-28page.md", "2018-02-28", "page"},
{"2018-02-28-.md", "2018-02-28", ""},
{"2018-02-28-.md", "2018-02-28", ""},
{"2018-02-28.md", "2018-02-28", ""},
{"2018-02-28-page", "2018-02-28", "page"},
{"2012-9-12-page.md", "0001-01-01", ""},
{"asdfasdf.md", "0001-01-01", ""},
}
for _, test := range tests {
expecteFDate, err := time.Parse("2006-01-02", test.date)
c.Assert(err, qt.IsNil)
gotDate, gotSlug := dateAndSlugFromBaseFilename(time.UTC, test.name)
c.Assert(gotDate, qt.Equals, expecteFDate)
c.Assert(gotSlug, qt.Equals, test.slug)
}
}
func newTestFd() *FrontMatterDescriptor {
return &FrontMatterDescriptor{
func newTestFd() *pagemeta.FrontMatterDescriptor {
return &pagemeta.FrontMatterDescriptor{
Frontmatter: make(map[string]any),
Params: make(map[string]any),
Dates: &resource.Dates{},
PageURLs: &URLPath{},
PageURLs: &pagemeta.URLPath{},
Location: time.UTC,
}
}
@@ -83,21 +49,21 @@ func TestFrontMatterNewConfig(t *testing.T) {
"publishDate": []string{"date"},
})
fc, err := newFrontmatterConfig(cfg)
fc, err := pagemeta.DecodeFrontMatterConfig(cfg)
c.Assert(err, qt.IsNil)
c.Assert(fc.date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
c.Assert(fc.expiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
c.Assert(fc.publishDate, qt.DeepEquals, []string{"date"})
c.Assert(fc.Date, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.Lastmod, qt.DeepEquals, []string{"publishdate", "pubdate", "published"})
c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"lastmod", "modified"})
c.Assert(fc.PublishDate, qt.DeepEquals, []string{"date"})
// Default
cfg = config.New()
fc, err = newFrontmatterConfig(cfg)
fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
c.Assert(err, qt.IsNil)
c.Assert(fc.date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
c.Assert(fc.expiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
c.Assert(fc.publishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
c.Assert(fc.Date, qt.DeepEquals, []string{"date", "publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.Lastmod, qt.DeepEquals, []string{":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"expirydate", "unpublishdate"})
c.Assert(fc.PublishDate, qt.DeepEquals, []string{"publishdate", "pubdate", "published", "date"})
// :default keyword
cfg.Set("frontmatter", map[string]any{
@@ -106,12 +72,12 @@ func TestFrontMatterNewConfig(t *testing.T) {
"expiryDate": []string{"d3", ":default"},
"publishDate": []string{"d4", ":default"},
})
fc, err = newFrontmatterConfig(cfg)
fc, err = pagemeta.DecodeFrontMatterConfig(cfg)
c.Assert(err, qt.IsNil)
c.Assert(fc.date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
c.Assert(fc.expiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
c.Assert(fc.publishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
c.Assert(fc.Date, qt.DeepEquals, []string{"d1", "date", "publishdate", "pubdate", "published", "lastmod", "modified"})
c.Assert(fc.Lastmod, qt.DeepEquals, []string{"d2", ":git", "lastmod", "modified", "date", "publishdate", "pubdate", "published"})
c.Assert(fc.ExpiryDate, qt.DeepEquals, []string{"d3", "expirydate", "unpublishdate"})
c.Assert(fc.PublishDate, qt.DeepEquals, []string{"d4", "publishdate", "pubdate", "published", "date"})
}
func TestFrontMatterDatesHandlers(t *testing.T) {
@@ -124,8 +90,8 @@ func TestFrontMatterDatesHandlers(t *testing.T) {
cfg.Set("frontmatter", map[string]any{
"date": []string{handlerID, "date"},
})
handler, err := NewFrontmatterHandler(nil, cfg)
conf := testconfig.GetTestConfig(nil, cfg)
handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
c.Assert(err, qt.IsNil)
d1, _ := time.Parse("2006-01-02", "2018-02-01")
@@ -166,7 +132,8 @@ func TestFrontMatterDatesCustomConfig(t *testing.T) {
"publishdate": []string{"publishdate"},
})
handler, err := NewFrontmatterHandler(nil, cfg)
conf := testconfig.GetTestConfig(nil, cfg)
handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
c.Assert(err, qt.IsNil)
testDate, err := time.Parse("2006-01-02", "2018-02-01")
@@ -213,7 +180,8 @@ func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
"publishdate": []string{":default", "mypubdate"},
})
handler, err := NewFrontmatterHandler(nil, cfg)
conf := testconfig.GetTestConfig(nil, cfg)
handler, err := pagemeta.NewFrontmatterHandler(nil, conf.GetConfigSection("frontmatter").(pagemeta.FrontmatterConfig))
c.Assert(err, qt.IsNil)
testDate, _ := time.Parse("2006-01-02", "2018-02-01")
@@ -230,28 +198,3 @@ func TestFrontMatterDatesDefaultKeyword(t *testing.T) {
c.Assert(d.Dates.FPublishDate.Day(), qt.Equals, 4)
c.Assert(d.Dates.FExpiryDate.IsZero(), qt.Equals, true)
}
func TestExpandDefaultValues(t *testing.T) {
c := qt.New(t)
c.Assert(expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"a", "b", "c", "d"})
c.Assert(expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"}), qt.DeepEquals, []string{"a", "b", "c"})
c.Assert(expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"b", "c", "a", "b", "c", "d"})
}
func TestFrontMatterDateFieldHandler(t *testing.T) {
t.Parallel()
c := qt.New(t)
handlers := new(frontmatterFieldHandlers)
fd := newTestFd()
d, _ := time.Parse("2006-01-02", "2018-02-01")
fd.Frontmatter["date"] = d
h := handlers.newDateFieldHandler("date", func(d *FrontMatterDescriptor, t time.Time) { d.Dates.FDate = t })
handled, err := h(fd)
c.Assert(handled, qt.Equals, true)
c.Assert(err, qt.IsNil)
c.Assert(fd.Dates.FDate, qt.Equals, d)
}

View File

@@ -16,6 +16,7 @@ package pagemeta
import (
"fmt"
"testing"
"time"
"github.com/gohugoio/hugo/htesting/hqt"
@@ -90,3 +91,46 @@ publishResources = true`
}
}
func TestDateAndSlugFromBaseFilename(t *testing.T) {
t.Parallel()
c := qt.New(t)
tests := []struct {
name string
date string
slug string
}{
{"page.md", "0001-01-01", ""},
{"2012-09-12-page.md", "2012-09-12", "page"},
{"2018-02-28-page.md", "2018-02-28", "page"},
{"2018-02-28_page.md", "2018-02-28", "page"},
{"2018-02-28 page.md", "2018-02-28", "page"},
{"2018-02-28page.md", "2018-02-28", "page"},
{"2018-02-28-.md", "2018-02-28", ""},
{"2018-02-28-.md", "2018-02-28", ""},
{"2018-02-28.md", "2018-02-28", ""},
{"2018-02-28-page", "2018-02-28", "page"},
{"2012-9-12-page.md", "0001-01-01", ""},
{"asdfasdf.md", "0001-01-01", ""},
}
for _, test := range tests {
expecteFDate, err := time.Parse("2006-01-02", test.date)
c.Assert(err, qt.IsNil)
gotDate, gotSlug := dateAndSlugFromBaseFilename(time.UTC, test.name)
c.Assert(gotDate, qt.Equals, expecteFDate)
c.Assert(gotSlug, qt.Equals, test.slug)
}
}
func TestExpandDefaultValues(t *testing.T) {
c := qt.New(t)
c.Assert(expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"a", "b", "c", "d"})
c.Assert(expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"}), qt.DeepEquals, []string{"a", "b", "c"})
c.Assert(expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"b", "c", "a", "b", "c", "d"})
}