From 241b21b0fd34d91fccb2ce69874110dceae6f926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 4 Jan 2023 18:24:36 +0100 Subject: 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 --- resources/page/permalinks.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'resources/page/permalinks.go') diff --git a/resources/page/permalinks.go b/resources/page/permalinks.go index dbf10220c..3dfc36937 100644 --- a/resources/page/permalinks.go +++ b/resources/page/permalinks.go @@ -37,7 +37,7 @@ type PermalinkExpander struct { expanders map[string]func(Page) (string, error) - ps *helpers.PathSpec + urlize func(uri string) string } // Time for checking date formats. Every field is different than the @@ -67,9 +67,9 @@ func (p PermalinkExpander) callback(attr string) (pageToPermaAttribute, bool) { } // NewPermalinkExpander creates a new PermalinkExpander configured by the given -// PathSpec. -func NewPermalinkExpander(ps *helpers.PathSpec) (PermalinkExpander, error) { - p := PermalinkExpander{ps: ps} +// urlize func. +func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]string) (PermalinkExpander, error) { + p := PermalinkExpander{urlize: urlize} p.knownPermalinkAttributes = map[string]pageToPermaAttribute{ "year": p.pageToPermalinkDate, @@ -87,11 +87,6 @@ func NewPermalinkExpander(ps *helpers.PathSpec) (PermalinkExpander, error) { "filename": p.pageToPermalinkFilename, } - patterns := ps.Cfg.GetStringMapString("permalinks") - if patterns == nil { - return p, nil - } - e, err := p.parse(patterns) if err != nil { return p, err @@ -180,6 +175,9 @@ var attributeRegexp = regexp.MustCompile(`:\w+(\[.+?\])?`) // validate determines if a PathPattern is well-formed func (l PermalinkExpander) validate(pp string) bool { + if len(pp) == 0 { + return false + } fragments := strings.Split(pp[1:], "/") bail := false for i := range fragments { @@ -244,7 +242,7 @@ func (l PermalinkExpander) pageToPermalinkDate(p Page, dateField string) (string // pageToPermalinkTitle returns the URL-safe form of the title func (l PermalinkExpander) pageToPermalinkTitle(p Page, _ string) (string, error) { - return l.ps.URLize(p.Title()), nil + return l.urlize(p.Title()), nil } // pageToPermalinkFilename returns the URL-safe form of the filename @@ -256,13 +254,13 @@ func (l PermalinkExpander) pageToPermalinkFilename(p Page, _ string) (string, er _, name = filepath.Split(dir) } - return l.ps.URLize(name), nil + return l.urlize(name), nil } // if the page has a slug, return the slug, else return the title func (l PermalinkExpander) pageToPermalinkSlugElseTitle(p Page, a string) (string, error) { if p.Slug() != "" { - return l.ps.URLize(p.Slug()), nil + return l.urlize(p.Slug()), nil } return l.pageToPermalinkTitle(p, a) } @@ -270,7 +268,7 @@ func (l PermalinkExpander) pageToPermalinkSlugElseTitle(p Page, a string) (strin // if the page has a slug, return the slug, else return the filename func (l PermalinkExpander) pageToPermalinkSlugElseFilename(p Page, a string) (string, error) { if p.Slug() != "" { - return l.ps.URLize(p.Slug()), nil + return l.urlize(p.Slug()), nil } return l.pageToPermalinkFilename(p, a) } -- cgit v1.2.3