Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-19 11:16:27 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-19 16:52:23 +0300
commitfa520a2d983b982394ad10088393fb303e48980a (patch)
treef545a33ef8de2eccc6fe781281126a5daf90a4c0
parent82029c1ec975bc2173bd5a454aee6c800924035d (diff)
Add Page.GetTerms
Fixes #6905
-rw-r--r--docs/content/en/templates/taxonomy-templates.md12
-rw-r--r--hugolib/content_map_page.go2
-rw-r--r--hugolib/page.go28
-rw-r--r--hugolib/site_benchmark_new_test.go10
-rw-r--r--hugolib/taxonomy_test.go33
-rw-r--r--resources/page/page.go4
-rw-r--r--resources/page/page_nop.go4
-rw-r--r--resources/page/testhelpers_test.go4
8 files changed, 82 insertions, 15 deletions
diff --git a/docs/content/en/templates/taxonomy-templates.md b/docs/content/en/templates/taxonomy-templates.md
index b82a5175c..bef2d3226 100644
--- a/docs/content/en/templates/taxonomy-templates.md
+++ b/docs/content/en/templates/taxonomy-templates.md
@@ -216,6 +216,18 @@ Because we are leveraging the front matter system to define taxonomies for conte
### Example: List Tags in a Single Page Template
+{{< new-in "0.65.0" >}}
+
+```go-html-template
+<ul>
+ {{ range (.GetTerms "tags") }}
+ <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
+ {{ end }}
+</ul>
+```
+
+Before Hugo 0.65.0 you needed to do something like this:
+
```go-html-template
{{ $taxo := "tags" }} <!-- Use the plural form here -->
<ul id="{{ $taxo }}">
diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go
index 06d1310ec..5075095ea 100644
--- a/hugolib/content_map_page.go
+++ b/hugolib/content_map_page.go
@@ -599,7 +599,7 @@ func (m *pageMap) attachPageToViews(s string, b *contentNode) {
if s == "/" {
// To avoid getting an empty key.
- s = "home"
+ s = page.KindHome
}
key := cleanTreeKey(path.Join(viewName.plural, termKey, s))
m.taxonomyEntries.Insert(key, bv)
diff --git a/hugolib/page.go b/hugolib/page.go
index 1384d7293..12129d4ad 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -131,6 +131,34 @@ func (p *pageState) GitInfo() *gitmap.GitInfo {
return p.gitInfo
}
+// GetTerms gets the terms defined on this page in the given taxonomy.
+func (p *pageState) GetTerms(taxonomy string) page.Pages {
+ taxonomy = strings.ToLower(taxonomy)
+ m := p.s.pageMap
+ prefix := cleanTreeKey(taxonomy)
+
+ var self string
+ if p.IsHome() {
+ // TODO(bep) make this less magical, see taxonomyEntries.Insert.
+ self = "/" + page.KindHome
+ } else {
+ self = p.treeRef.key
+ }
+
+ var pas page.Pages
+
+ m.taxonomies.WalkPrefixListable(prefix, func(s string, n *contentNode) bool {
+ if _, found := m.taxonomyEntries.Get(s + self); found {
+ pas = append(pas, n.p)
+ }
+ return false
+ })
+
+ page.SortByDefault(pas)
+
+ return pas
+}
+
func (p *pageState) MarshalJSON() ([]byte, error) {
return page.MarshalPageToJSON(p)
}
diff --git a/hugolib/site_benchmark_new_test.go b/hugolib/site_benchmark_new_test.go
index 4ffeaa8a1..1f16c97e5 100644
--- a/hugolib/site_benchmark_new_test.go
+++ b/hugolib/site_benchmark_new_test.go
@@ -378,14 +378,10 @@ contentDir="content/sv"
{"List terms", func(b testing.TB) *sitesBuilder {
pageTemplateTemplate := `
-{{ $taxo := "categories" }}
<ul>
- {{ range .Param $taxo }}
- {{ $name := . }}
- {{ with $.Site.GetPage (printf "/%s/%s" $taxo ($name | urlize)) }}
- <li><a href="{{ .Permalink }}">{{ $name }}</a></li>
- {{ end }}
- {{ end }}
+ {{ range (.GetTerms "categories") }}
+ <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
+ {{ end }}
</ul>
`
diff --git a/hugolib/taxonomy_test.go b/hugolib/taxonomy_test.go
index d91e7699f..913773da6 100644
--- a/hugolib/taxonomy_test.go
+++ b/hugolib/taxonomy_test.go
@@ -542,25 +542,41 @@ func TestTaxonomiesPageCollections(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t)
- b.WithContent("p1.md", `---
+ b.WithContent(
+ "_index.md", `---
+title: "Home Sweet Home"
+categories: [ "dogs", "gorillas"]
+---
+`,
+ "section/_index.md", `---
+title: "Section"
+categories: [ "cats", "dogs", "birds"]
+---
+`,
+ "section/p1.md", `---
title: "Page1"
categories: ["funny", "cats"]
---
-`, "p2.md", `---
+`, "section/p2.md", `---
title: "Page2"
categories: ["funny"]
---
`)
b.WithTemplatesAdded("index.html", `
+{{ $home := site.Home }}
+{{ $section := site.GetPage "section" }}
{{ $categories := site.GetPage "categories" }}
{{ $funny := site.GetPage "categories/funny" }}
{{ $cats := site.GetPage "categories/cats" }}
+{{ $p1 := site.GetPage "section/p1" }}
Categories Pages: {{ range $categories.Pages}}{{.RelPermalink }}|{{ end }}:END
Funny Pages: {{ range $funny.Pages}}{{.RelPermalink }}|{{ end }}:END
Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
-
+P1 Terms: {{ range $p1.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
+Section Terms: {{ range $section.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
+Home Terms: {{ range $home.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
`)
b.Build(BuildCfg{})
@@ -575,12 +591,15 @@ Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
b.Assert(funny.Parent(), qt.Equals, cat)
b.AssertFileContent("public/index.html", `
-Categories Pages: /categories/cats/|/categories/funny/|:END
-Funny Pages: /p1/|/p2/|:END
-Cats Pages: /p1/|:END
+ Categories Pages: /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END
+ Funny Pages: /section/p1/|/section/p2/|:END
+ Cats Pages: /section/p1/|/section/|:END
+ P1 Terms: /categories/cats/|/categories/funny/|:END
+ Section Terms: /categories/birds/|/categories/cats/|/categories/dogs/|:END
+ Home Terms: /categories/dogs/|/categories/gorillas/|:END
`)
- b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/p1/</link>`)
+ b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/section/p1/</link>`)
b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`)
}
diff --git a/resources/page/page.go b/resources/page/page.go
index c096cb726..1225f43d0 100644
--- a/resources/page/page.go
+++ b/resources/page/page.go
@@ -252,6 +252,10 @@ type PageWithoutContent interface {
maps.Scratcher
RelatedKeywordsProvider
+ // GetTerms gets the terms of a given taxonomy,
+ // e.g. GetTerms("categories")
+ GetTerms(taxonomy string) Pages
+
DeprecatedWarningPageMethods
}
diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go
index 16663ab39..ccfbf525f 100644
--- a/resources/page/page_nop.go
+++ b/resources/page/page_nop.go
@@ -174,6 +174,10 @@ func (p *nopPage) GetParam(key string) interface{} {
return nil
}
+func (p *nopPage) GetTerms(taxonomy string) Pages {
+ return nil
+}
+
func (p *nopPage) GitInfo() *gitmap.GitInfo {
return nil
}
diff --git a/resources/page/testhelpers_test.go b/resources/page/testhelpers_test.go
index dd28fa2cb..9c8605dad 100644
--- a/resources/page/testhelpers_test.go
+++ b/resources/page/testhelpers_test.go
@@ -222,6 +222,10 @@ func (p *testPage) GetParam(key string) interface{} {
panic("not implemented")
}
+func (p *testPage) GetTerms(taxonomy string) Pages {
+ panic("not implemented")
+}
+
func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler {
return relatedDocsHandler
}