diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /resources/page/permalinks.go | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) |
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
Diffstat (limited to 'resources/page/permalinks.go')
-rw-r--r-- | resources/page/permalinks.go | 24 |
1 files changed, 11 insertions, 13 deletions
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) } |