Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resources/page: Expand parmalinks tokens in url #12695

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hugolib/page__paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ func createTargetPathDescriptor(p *pageState) (page.TargetPathDescriptor, error)
desc.PrefixFilePath = s.getLanguageTargetPathLang(alwaysInSubDir)
desc.PrefixLink = s.getLanguagePermalinkLang(alwaysInSubDir)

if desc.URL != "" && strings.IndexByte(desc.URL, ':') >= 0 {
// Attempt to parse and expand an url. Probably it's a good
// idea to cache the parsed url to avoid regexp matching and memory allocations.

opath, err := d.ResourceSpec.Permalinks.ExpandPattern(desc.URL, p)
n1xx1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return desc, err
}

if opath != "" {
opath, _ = url.QueryUnescape(opath)
desc.URL = opath
}
}

opath, err := d.ResourceSpec.Permalinks.Expand(p.Section(), p)
if err != nil {
return desc, err
Expand Down
2 changes: 2 additions & 0 deletions hugolib/page_permalink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func TestPermalink(t *testing.T) {

// test URL overrides
{"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"},
// test URL override with expands
{"x/y/z/boofar.md", "", "test", "/z/:slug/", false, false, "/z/test/", "/z/test/"},
n1xx1 marked this conversation as resolved.
Show resolved Hide resolved
}

for i, test := range tests {
Expand Down
87 changes: 52 additions & 35 deletions resources/page/permalinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]ma
return p, nil
}

// ExpandPattern expands the path in p with the specified expand pattern.
func (l PermalinkExpander) ExpandPattern(pattern string, p Page) (string, error) {
expander, err := l.parsePattern(pattern)
if err != nil {
return "", err
}

return expander(p)
}

// Expand expands the path in p according to the rules defined for the given key.
// If no rules are found for the given key, an empty string is returned.
func (l PermalinkExpander) Expand(key string, p Page) (string, error) {
Expand Down Expand Up @@ -129,56 +139,63 @@ func init() {
}
}

func (l PermalinkExpander) parse(patterns map[string]string) (map[string]func(Page) (string, error), error) {
expanders := make(map[string]func(Page) (string, error))

for k, pattern := range patterns {
k = strings.Trim(k, sectionCutSet)
func (l PermalinkExpander) parsePattern(pattern string) (func(Page) (string, error), error) {
if !l.validate(pattern) {
return nil, &permalinkExpandError{pattern: pattern, err: errPermalinkIllFormed}
}

if !l.validate(pattern) {
return nil, &permalinkExpandError{pattern: pattern, err: errPermalinkIllFormed}
}
matches := attributeRegexp.FindAllStringSubmatch(pattern, -1)

pattern := pattern
matches := attributeRegexp.FindAllStringSubmatch(pattern, -1)
callbacks := make([]pageToPermaAttribute, len(matches))
replacements := make([]string, len(matches))
for i, m := range matches {
replacement := m[0]
attr := replacement[1:]
replacements[i] = replacement
callback, ok := l.callback(attr)

callbacks := make([]pageToPermaAttribute, len(matches))
replacements := make([]string, len(matches))
for i, m := range matches {
replacement := m[0]
attr := replacement[1:]
replacements[i] = replacement
callback, ok := l.callback(attr)
if !ok {
return nil, &permalinkExpandError{pattern: pattern, err: errPermalinkAttributeUnknown}
}

if !ok {
return nil, &permalinkExpandError{pattern: pattern, err: errPermalinkAttributeUnknown}
}
callbacks[i] = callback
}

callbacks[i] = callback
expander := func(p Page) (string, error) {
if matches == nil {
return pattern, nil
}

expanders[k] = func(p Page) (string, error) {
if matches == nil {
return pattern, nil
newField := pattern

for i, replacement := range replacements {
attr := replacement[1:]
callback := callbacks[i]
newAttr, err := callback(p, attr)
if err != nil {
return "", &permalinkExpandError{pattern: pattern, err: err}
}

newField := pattern
newField = strings.Replace(newField, replacement, newAttr, 1)
}

for i, replacement := range replacements {
attr := replacement[1:]
callback := callbacks[i]
newAttr, err := callback(p, attr)
if err != nil {
return "", &permalinkExpandError{pattern: pattern, err: err}
}
return newField, nil
}
return expander, nil
}

newField = strings.Replace(newField, replacement, newAttr, 1)
func (l PermalinkExpander) parse(patterns map[string]string) (map[string]func(Page) (string, error), error) {
expanders := make(map[string]func(Page) (string, error))

}
for k, pattern := range patterns {
k = strings.Trim(k, sectionCutSet)

return newField, nil
expander, err := l.parsePattern(pattern)
if err != nil {
return nil, err
}

expanders[k] = expander
}

return expanders, nil
Expand Down
Loading