Skip to content

Commit

Permalink
Misc permalinks adjustments
Browse files Browse the repository at this point in the history
* Move config loading to the page package
* Fix a lower bound panic for the `:sections` slice syntax.
* Always return the `:title`
* Add some permalinks integration tests
* Also see issues below

Fixes gohugoio#9448
Fixes gohugoio#11184
See gohugoio#8523
  • Loading branch information
bep committed Jun 29, 2023
1 parent 1264675 commit d25ce22
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 89 deletions.
46 changes: 3 additions & 43 deletions config/allconfig/alldecoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,49 +206,9 @@ var allDecoderSetups = map[string]decodeWeight{
"permalinks": {
key: "permalinks",
decode: func(d decodeWeight, p decodeConfig) error {
p.c.Permalinks = make(map[string]map[string]string)

p.c.Permalinks["page"] = make(map[string]string)
p.c.Permalinks["section"] = make(map[string]string)
p.c.Permalinks["taxonomy"] = make(map[string]string)
p.c.Permalinks["term"] = make(map[string]string)

config := maps.CleanConfigStringMap(p.p.GetStringMap(d.key))
for k, v := range config {
switch v := v.(type) {
case string:
// [permalinks]
// key = '...'

// To sucessfully be backward compatible, "default" patterns need to be set for both page and term
p.c.Permalinks["page"][k] = v
p.c.Permalinks["term"][k] = v

case maps.Params:
// [permalinks.key]
// xyz = ???

if (k == "page") || (k == "section") || (k == "taxonomy") || (k == "term") {
// TODO: warn if we overwrite an already set value
for k2, v2 := range v {
switch v2 := v2.(type) {
case string:
p.c.Permalinks[k][k2] = v2

default:
return fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q for kind %q", v2, k2, k)
}
}
} else {
return fmt.Errorf("permalinks configuration only allows per-kind configuration 'page', 'section', 'taxonomy' and 'term'; unknown kind: %q", k)
}

default:
return fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q", v, k)
}
}

return nil
var err error
p.c.Permalinks, err = page.DecodePermalinksConfig(p.p.GetStringMap(d.key))
return err
},
},
"sitemap": {
Expand Down
1 change: 0 additions & 1 deletion hugofs/files/classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ const (
ContentClassBranch ContentClass = "branch"
ContentClassFile ContentClass = "zfile" // Sort below
ContentClassContent ContentClass = "zcontent"
ContentClassZero ContentClass = "zero" // Special value for zeroFile
)

func (c ContentClass) IsBundle() bool {
Expand Down
9 changes: 8 additions & 1 deletion hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,14 @@ func (m *contentMap) splitKey(k string) []string {
return nil
}

return strings.Split(k, "/")[1:]
parts := strings.Split(k, "/")[1:]
if len(parts) == 0 {
return nil
}
if parts[len(parts)-1] == "" {
parts = parts[:len(parts)-1]
}
return parts
}

func (m *contentMap) testDump() string {
Expand Down
4 changes: 4 additions & 0 deletions hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
return s
}

func (s *IntegrationTestBuilder) LogString() string {
return s.logBuff.String()
}

func (s *IntegrationTestBuilder) BuildE() (*IntegrationTestBuilder, error) {
s.Helper()
if err := s.initBuilder(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions hugolib/page__paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.Target
)

d := s.Deps
classifier := files.ContentClassZero
var classifier files.ContentClass

if !p.File().IsZero() {
dir = p.File().Dir()
baseName = p.File().TranslationBaseName()
contentBaseName = p.File().ContentBaseName()
classifier = p.File().Classifier()
classifier = p.File().FileInfo().Meta().Classifier
}

if classifier == files.ContentClassLeaf {
Expand Down
22 changes: 22 additions & 0 deletions hugolib/site_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,25 @@ Do not go gentle into that good night.
th.assertFileContent(filepath.Join("public", "ss1", "index.html"), "P1|URL: /ss1/|Next: /ss1/page/2/")
th.assertFileContent(filepath.Join("public", "ss1", "page", "2", "index.html"), "P2|URL: /ss1/page/2/|Next: /ss1/page/3/")
}

func TestSectionsEntries(t *testing.T) {
files := `
-- hugo.toml --
-- content/withfile/_index.md --
-- content/withoutfile/p1.md --
-- layouts/_default/list.html --
SectionsEntries: {{ .SectionsEntries }}
`

b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/withfile/index.html", "SectionsEntries: [withfile]")
b.AssertFileContent("public/withoutfile/index.html", "SectionsEntries: [withoutfile]")
}
80 changes: 71 additions & 9 deletions resources/page/permalinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

"errors"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/helpers"

)

// PermalinkExpander holds permalin mappings per section.
Expand Down Expand Up @@ -252,16 +252,12 @@ 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) {
if p.File().TranslationBaseName() == "_index" {
return "", nil
}

return l.urlize(p.Title()), nil
}

// pageToPermalinkFilename returns the URL-safe form of the filename
func (l PermalinkExpander) pageToPermalinkFilename(p Page, _ string) (string, error) {
name := p.File().TranslationBaseName()
name := l.translationBaseName(p)
if name == "index" {
// Page bundles; the directory name will hopefully have a better name.
dir := strings.TrimSuffix(p.File().Dir(), helpers.FilePathSeparator)
Expand Down Expand Up @@ -297,6 +293,13 @@ func (l PermalinkExpander) pageToPermalinkSections(p Page, _ string) (string, er
return p.CurrentSection().SectionsPath(), nil
}

func (l PermalinkExpander) translationBaseName(p Page) string {
if p.File().IsZero() {
return ""
}
return p.File().TranslationBaseName()
}

var (
nilSliceFunc = func(s []string) []string {
return nil
Expand Down Expand Up @@ -349,7 +352,10 @@ func (l PermalinkExpander) toSliceFunc(cut string) func(s []string) []string {
return func(ss []string) int {
// Prevent out of bound situations. It would not make
// much sense to panic here.
if n > len(ss) {
if n >= len(ss) {
if low {
return -1
}
return len(ss)
}
return n
Expand All @@ -365,7 +371,11 @@ func (l PermalinkExpander) toSliceFunc(cut string) func(s []string) []string {
if len(s) == 0 {
return nil
}
v := s[toN(s)]
n := toN(s)
if n < 0 {
return []string{}
}
v := s[n]
if v == "" {
return nil
}
Expand All @@ -379,7 +389,59 @@ func (l PermalinkExpander) toSliceFunc(cut string) func(s []string) []string {
if len(s) == 0 {
return nil
}
return s[toN1(s):toN2(s)]
n1, n2 := toN1(s), toN2(s)
if n1 < 0 || n2 < 0 {
return []string{}
}
return s[n1:n2]
}

}

var permalinksKindsSuppurt = []string{KindPage, KindSection, KindTaxonomy, KindTerm}

// DecodePermalinksConfig decodes the permalinks configuration in the given map
func DecodePermalinksConfig(m map[string]any) (map[string]map[string]string, error) {
permalinksConfig := make(map[string]map[string]string)

permalinksConfig[KindPage] = make(map[string]string)
permalinksConfig[KindSection] = make(map[string]string)
permalinksConfig[KindTaxonomy] = make(map[string]string)
permalinksConfig[KindTerm] = make(map[string]string)

config := maps.CleanConfigStringMap(m)
for k, v := range config {
switch v := v.(type) {
case string:
// [permalinks]
// key = '...'

// To sucessfully be backward compatible, "default" patterns need to be set for both page and term
permalinksConfig[KindPage][k] = v
permalinksConfig[KindTerm][k] = v

case maps.Params:
// [permalinks.key]
// xyz = ???

if helpers.InStringArray(permalinksKindsSuppurt, k) {
// TODO: warn if we overwrite an already set value
for k2, v2 := range v {
switch v2 := v2.(type) {
case string:
permalinksConfig[k][k2] = v2

default:
return nil, fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q for kind %q", v2, k2, k)
}
}
} else {
return nil, fmt.Errorf("permalinks configuration not supported for kind %q, supported kinds are %v", k, permalinksKindsSuppurt)
}

default:
return nil, fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q", v, k)
}
}
return permalinksConfig, nil
}
Loading

0 comments on commit d25ce22

Please sign in to comment.