summaryrefslogtreecommitdiffstats
path: root/resources
diff options
context:
space:
mode:
authorMai-Lapyst <67418776+Mai-Lapyst@users.noreply.github.com>2023-06-26 15:31:01 +0200
committerGitHub <noreply@github.com>2023-06-26 15:31:01 +0200
commitcc14c6a52c215fc43716f7b266c4c340b3d28230 (patch)
tree0d371fe0f9421d52a42e203ade99127c2d11806d /resources
parente3308a0bbc2de1cb4d7571521abeab07049ce31f (diff)
resources/page: Allow section and taxonomy pages to have a permalink configuration
Allows using permalink configuration for sections (branch bundles) and also for taxonomy pages. Extends the current permalink configuration to be able to specified per page kind while also staying backward compatible: all permalink patterns not dedicated to a certain kind, get automatically added for both normal pages and term pages. Fixes #8523
Diffstat (limited to 'resources')
-rw-r--r--resources/page/page_generate/generate_page_wrappers.go7
-rw-r--r--resources/page/permalinks.go32
-rw-r--r--resources/page/permalinks_test.go35
-rw-r--r--resources/page/zero_file.autogen.go6
4 files changed, 60 insertions, 20 deletions
diff --git a/resources/page/page_generate/generate_page_wrappers.go b/resources/page/page_generate/generate_page_wrappers.go
index f4b40f717..c034bc002 100644
--- a/resources/page/page_generate/generate_page_wrappers.go
+++ b/resources/page/page_generate/generate_page_wrappers.go
@@ -221,7 +221,7 @@ func generateFileIsZeroWrappers(c *codegen.Inspector) error {
methods := c.MethodsFromTypes([]reflect.Type{reflect.TypeOf((*source.File)(nil)).Elem()}, nil)
for _, m := range methods {
- if m.Name == "IsZero" {
+ if m.Name == "IsZero" || m.Name == "Classifier" {
continue
}
fmt.Fprint(&buff, m.DeclarationNamed("zeroFile"))
@@ -255,6 +255,11 @@ func (zeroFile) IsZero() bool {
return true
}
+func (z zeroFile) Classifier() files.ContentClass {
+ z.log.Warnln(".File.Classifier on zero object. Wrap it in if or with: {{ with .File }}{{ .Classifier }}{{ end }}")
+ return files.ContentClassZero
+}
+
%s
`, header, importsString(pkgImports), buff.String())
diff --git a/resources/page/permalinks.go b/resources/page/permalinks.go
index 3dfc36937..ab418a20b 100644
--- a/resources/page/permalinks.go
+++ b/resources/page/permalinks.go
@@ -26,6 +26,7 @@ import (
"errors"
"github.com/gohugoio/hugo/helpers"
+
)
// PermalinkExpander holds permalin mappings per section.
@@ -35,7 +36,7 @@ type PermalinkExpander struct {
// to be used to replace that tag.
knownPermalinkAttributes map[string]pageToPermaAttribute
- expanders map[string]func(Page) (string, error)
+ expanders map[string]map[string]func(Page) (string, error)
urlize func(uri string) string
}
@@ -68,7 +69,7 @@ func (p PermalinkExpander) callback(attr string) (pageToPermaAttribute, bool) {
// NewPermalinkExpander creates a new PermalinkExpander configured by the given
// urlize func.
-func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]string) (PermalinkExpander, error) {
+func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]map[string]string) (PermalinkExpander, error) {
p := PermalinkExpander{urlize: urlize}
p.knownPermalinkAttributes = map[string]pageToPermaAttribute{
@@ -87,12 +88,15 @@ func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]st
"filename": p.pageToPermalinkFilename,
}
- e, err := p.parse(patterns)
- if err != nil {
- return p, err
- }
+ p.expanders = make(map[string]map[string]func(Page) (string, error))
- p.expanders = e
+ for kind, patterns := range patterns {
+ e, err := p.parse(patterns)
+ if err != nil {
+ return p, err
+ }
+ p.expanders[kind] = e
+ }
return p, nil
}
@@ -100,7 +104,13 @@ func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]st
// 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, found := l.expanders[key]
+ expanders, found := l.expanders[p.Kind()]
+
+ if !found {
+ return "", nil
+ }
+
+ expand, found := expanders[key]
if !found {
return "", nil
@@ -242,6 +252,10 @@ 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
}
@@ -252,6 +266,8 @@ func (l PermalinkExpander) pageToPermalinkFilename(p Page, _ string) (string, er
// Page bundles; the directory name will hopefully have a better name.
dir := strings.TrimSuffix(p.File().Dir(), helpers.FilePathSeparator)
_, name = filepath.Split(dir)
+ } else if name == "_index" {
+ return "", nil
}
return l.urlize(name), nil
diff --git a/resources/page/permalinks_test.go b/resources/page/permalinks_test.go
index b9c0ca9cb..24eb27f5b 100644
--- a/resources/page/permalinks_test.go
+++ b/resources/page/permalinks_test.go
@@ -69,6 +69,7 @@ func TestPermalinkExpansion(t *testing.T) {
page.date = d
page.section = "blue"
page.slug = "The Slug"
+ page.kind = "page"
for _, item := range testdataPermalinks {
if !item.valid {
@@ -79,8 +80,10 @@ func TestPermalinkExpansion(t *testing.T) {
name := specNameCleaner.ReplaceAllString(item.spec, "")
c.Run(name, func(c *qt.C) {
- patterns := map[string]string{
- "posts": item.spec,
+ patterns := map[string]map[string]string{
+ "page": {
+ "posts": item.spec,
+ },
}
expander, err := NewPermalinkExpander(urlize, patterns)
c.Assert(err, qt.IsNil)
@@ -103,14 +106,18 @@ func TestPermalinkExpansionMultiSection(t *testing.T) {
page.date = d
page.section = "blue"
page.slug = "The Slug"
+ page.kind = "page"
page_slug_fallback := newTestPageWithFile("/page-filename/index.md")
page_slug_fallback.title = "Page Title"
-
- permalinksConfig := map[string]string{
- "posts": "/:slug",
- "blog": "/:section/:year",
- "recipes": "/:slugorfilename",
+ page_slug_fallback.kind = "page"
+
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:slug",
+ "blog": "/:section/:year",
+ "recipes": "/:slugorfilename",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
c.Assert(err, qt.IsNil)
@@ -137,8 +144,10 @@ func TestPermalinkExpansionConcurrent(t *testing.T) {
c := qt.New(t)
- permalinksConfig := map[string]string{
- "posts": "/:slug/",
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:slug/",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
@@ -151,6 +160,7 @@ func TestPermalinkExpansionConcurrent(t *testing.T) {
go func(i int) {
defer wg.Done()
page := newTestPage()
+ page.kind = "page"
for j := 1; j < 20; j++ {
page.slug = fmt.Sprintf("slug%d", i+j)
expanded, err := expander.Expand("posts", page)
@@ -209,9 +219,12 @@ func BenchmarkPermalinkExpand(b *testing.B) {
page.title = "Hugo Rocks"
d, _ := time.Parse("2006-01-02", "2019-02-28")
page.date = d
+ page.kind = "page"
- permalinksConfig := map[string]string{
- "posts": "/:year-:month-:title",
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:year-:month-:title",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
if err != nil {
diff --git a/resources/page/zero_file.autogen.go b/resources/page/zero_file.autogen.go
index 72d98998e..979f92a0f 100644
--- a/resources/page/zero_file.autogen.go
+++ b/resources/page/zero_file.autogen.go
@@ -18,6 +18,7 @@ package page
import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/hugofs"
+ "github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/source"
)
@@ -34,6 +35,11 @@ func (zeroFile) IsZero() bool {
return true
}
+func (z zeroFile) Classifier() files.ContentClass {
+ z.log.Warnln(".File.Classifier on zero object. Wrap it in if or with: {{ with .File }}{{ .Classifier }}{{ end }}")
+ return files.ContentClassZero
+}
+
func (z zeroFile) Path() (o0 string) {
z.log.Warnln(".File.Path on zero object. Wrap it in if or with: {{ with .File }}{{ .Path }}{{ end }}")
return