hugolib: Populate the taxonomy term Pages list
authorAnton Staaf <github@socialhacker.com>
Sun, 5 Mar 2017 20:24:14 +0000 (12:24 -0800)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 5 Mar 2017 20:24:14 +0000 (21:24 +0100)
Previously this was left empty, but it is very handy to have a list
of term pages for a given taxonomy.  This list can now be paginated
like other page lists.  It makes it possible to render summary
content from each terms index page for instance.  It also makes it
possible to sort the term pages in the same way that other page
lists can be sorted.  An RSS feed is now generated for
KindTaxonomyTerm pages as well.

This also fixes a bug in HugoSites.createMissingPages.  Previously
it would only check for existing KindTaxonomyTerm pages if the
taxonomy had any terms defined.  So for a taxonomy with no terms
but a taxonomy terms page it would generate a second empty terms
page.

docs/content/templates/rss.md
docs/content/templates/terms.md
hugolib/node_as_page_test.go
hugolib/page.go
hugolib/site.go
hugolib/site_render.go
hugolib/taxonomy_test.go

index d390b43060a30467ed4c5f7a5e472d1719a3f708..fe445469a4e15c3bc46abdc75c4e143d533dbb16 100644 (file)
@@ -48,6 +48,14 @@ Hugo will use the following prioritized list. If a file isn’t present, then th
 * /themes/`THEME`/layouts/\_default/rss.xml
 * [Embedded rss.xml](#the-embedded-rss-xml:eceb479b7b3b2077408a2878a29e1320)
 
+### Taxonomy Terms RSS
+
+* /layouts/taxonomy/`SINGULAR`.terms.rss.xml
+* /layouts/\_default/rss.xml
+* /themes/`THEME`/layouts/taxonomy/`SINGULAR`.terms.rss.xml
+* /themes/`THEME`/layouts/\_default/rss.xml
+* [Embedded rss.xml](#the-embedded-rss-xml:eceb479b7b3b2077408a2878a29e1320)
+
 
 ## Configuring RSS
 
index 378bd4df84b95eb48e33546e4406e5957e0d1fa3..f81fc8f71aa4da62276c102cba43e495b2ef0aff 100644 (file)
@@ -57,6 +57,7 @@ Taxonomy Terms pages will additionally have:
 
 * **.Data.Singular** The singular name of the taxonomy
 * **.Data.Plural** The plural name of the taxonomy
+* **.Data.Pages** (or as **.Pages**) The taxonomy Terms index pages
 * **.Data.Terms** The taxonomy itself
 * **.Data.Terms.Alphabetical** The Terms alphabetized
 * **.Data.Terms.ByCount** The Terms ordered by popularity
@@ -121,7 +122,7 @@ Another example listing the content for each term (ordered by Date):
 
 ## Ordering
 
-Hugo can order the meta data in two different ways. It can be ordered:
+Hugo can order the term meta data in two different ways. It can be ordered:
 
 * by the number of contents assigned to that key, or
 * alphabetically.
@@ -162,3 +163,14 @@ Hugo can order the meta data in two different ways. It can be ordered:
     </section>
 
     {{ partial "footer.html" . }}
+
+Hugo can also order and paginate the term index pages in all the normal ways.
+
+### Example terms.html snippet (paginated and ordered by date)
+
+    <h1 id="title">{{ .Title }}</h1>
+    <ul>
+      {{ range .Paginator.Pages.ByDate.Reverse }}
+        <li><a href="{{ .Permalink }}">{{ .Title }}</a> {{ $.Data.Terms.Count .Data.Term }}</li>
+      {{ end }}
+    </ul>
index f063faf9ab9afb6b31df0c35dc286b8b9498fcbd..57a51f40a8d754e12bda69692a1d7657d8cff07d 100644 (file)
@@ -176,7 +176,10 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) {
                "Lastmod: 2009-01-15",
        )
 
-       // There are no pages to paginate over in the taxonomy terms.
+       // Check taxonomy terms paginator
+       th.assertFileContent(expectedFilePath(ugly, "public", "categories", "page", "2"),
+               "Taxonomy Terms Title: Taxonomy Term Categories",
+               "Pag: Taxonomy Web")
 
        // RSS
        th.assertFileContent(filepath.Join("public", "customrss.xml"), "Recent content in Home Sweet Home! on Hugo Rocks", "<rss")
@@ -184,6 +187,7 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) {
        th.assertFileContent(filepath.Join("public", "sect2", "customrss.xml"), "Recent content in Section2 on Hugo Rocks", "<rss")
        th.assertFileContent(filepath.Join("public", "categories", "hugo", "customrss.xml"), "Recent content in Taxonomy Hugo on Hugo Rocks", "<rss")
        th.assertFileContent(filepath.Join("public", "categories", "web", "customrss.xml"), "Recent content in Taxonomy Web on Hugo Rocks", "<rss")
+       th.assertFileContent(filepath.Join("public", "categories", "customrss.xml"), "Recent content in Taxonomy Term Categories on Hugo Rocks", "<rss")
 
 }
 
@@ -792,6 +796,10 @@ Lastmod: {{ .Lastmod.Format "2006-01-02" }}
        writeSource(t, fs, filepath.Join("layouts", "_default", "terms.html"), `
 Taxonomy Terms Title: {{ .Title }}
 Taxonomy Terms Content: {{ .Content }}
+# Pages: {{ len .Data.Pages }}
+{{ range .Paginator.Pages }}
+       Pag: {{ .Title }}
+{{ end }}
 {{ range $key, $value := .Data.Terms }}
        k/v: {{ $key | lower }} / {{ printf "%s" $value }}
 {{ end }}
index 8e09012e9befd02580a89d37377b3f147e563893..3169e123a79c2ef3368fe74c9e72fa4064978264 100644 (file)
@@ -681,7 +681,8 @@ func (p *Page) rssLayouts() []string {
                singular := p.s.taxonomiesPluralSingular[p.sections[0]]
                return []string{"taxonomy/" + singular + ".rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}
        case KindTaxonomyTerm:
-       // No RSS for taxonomy terms
+               singular := p.s.taxonomiesPluralSingular[p.sections[0]]
+               return []string{"taxonomy/" + singular + ".terms.rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}
        case KindPage:
                // No RSS for regular pages
        }
@@ -1591,6 +1592,7 @@ func (p *Page) prepareData(s *Site) error {
                p.Data[singular] = taxonomy
                p.Data["Singular"] = singular
                p.Data["Plural"] = plural
+               p.Data["Term"] = term
                pages = taxonomy.Pages()
        case KindTaxonomyTerm:
                plural := p.sections[0]
@@ -1602,6 +1604,13 @@ func (p *Page) prepareData(s *Site) error {
                // keep the following just for legacy reasons
                p.Data["OrderedIndex"] = p.Data["Terms"]
                p.Data["Index"] = p.Data["Terms"]
+
+               // A list of all KindTaxonomy pages with matching plural
+               for _, p := range s.findPagesByKind(KindTaxonomy) {
+                       if p.sections[0] == plural {
+                               pages = append(pages, p)
+                       }
+               }
        }
 
        p.Data["Pages"] = pages
index 1512819d5676e20ab95ea44060c9f94a5ce91e59..93e3ee740fec7282fe1d4ae31897ffca7b3a9d4a 100644 (file)
@@ -2037,7 +2037,7 @@ func (s *Site) newHomePage() *Page {
 func (s *Site) setPageURLs(p *Page, in string) {
        p.URLPath.URL = s.PathSpec.URLizeAndPrep(in)
        p.URLPath.Permalink = s.Info.permalink(p.URLPath.URL)
-       if p.Kind != KindPage && p.Kind != KindTaxonomyTerm {
+       if p.Kind != KindPage {
                p.RSSLink = template.URL(s.Info.permalink(in + ".xml"))
        }
 }
index 81cad525c25907f3265a85cde495977df17e9530..37ee21fd450db1d376f527e030132648c50a1358 100644 (file)
@@ -72,7 +72,7 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa
                }
 
                // Taxonomy terms have no page set to paginate, so skip that for now.
-               if p.IsNode() && p.Kind != KindTaxonomyTerm {
+               if p.IsNode() {
                        if err := s.renderPaginator(p); err != nil {
                                results <- err
                        }
index 5687cc032df39ac9b1768a17872e6d11d68de3fd..6843838b9a9abfec4001ea09243af52aa563db55 100644 (file)
@@ -15,7 +15,6 @@ package hugolib
 
 import (
        "fmt"
-       "html/template"
        "path/filepath"
        "reflect"
        "testing"
@@ -127,9 +126,24 @@ others:
 
        s := h.Sites[0]
 
-       // Issue #1302
-       term := s.getPage(KindTaxonomyTerm, "others")
-       require.Equal(t, template.URL(""), term.RSSLink)
+       // Make sure that each KindTaxonomyTerm page has an appropriate number
+       // of KindTaxonomy pages in its Pages slice.
+       taxonomyTermPageCounts := map[string]int{
+               "tags":       2,
+               "categories": 2,
+               "others":     2,
+               "empties":    0,
+       }
+
+       for taxonomy, count := range taxonomyTermPageCounts {
+               term := s.getPage(KindTaxonomyTerm, taxonomy)
+               require.NotNil(t, term)
+               require.Len(t, term.Pages, count)
+
+               for _, page := range term.Pages {
+                       require.Equal(t, KindTaxonomy, page.Kind)
+               }
+       }
 
        // Issue #3070 preserveTaxonomyNames
        if preserveTaxonomyNames {