shithub: hugo

Download patch

ref: 3cd97951f1cb6d7169a8a84a7b86984c05b2d35c
parent: ee75e2999b66bd9f258a241c487b6677cf2fa071
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Mar 25 13:46:09 EDT 2017

hugolib, layout: Consolidate RSS template handling

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -661,30 +661,6 @@
 	return p.Source.Section()
 }
 
-// TODO(bep) consolidate and test these KindHome switches (see other layouts methods)s
-// rssLayouts returns RSS layouts to use for the RSS version of this page, nil
-// if no RSS should be rendered.
-// TODO(bep) output
-func (p *Page) rssLayouts() []string {
-	switch p.Kind {
-	case KindHome:
-		return []string{"rss.xml", "_default/rss.xml", "_internal/_default/rss.xml"}
-	case KindSection:
-		section := p.sections[0]
-		return []string{"section/" + section + ".rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}
-	case KindTaxonomy:
-		singular := p.s.taxonomiesPluralSingular[p.sections[0]]
-		return []string{"taxonomy/" + singular + ".rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}
-	case KindTaxonomyTerm:
-		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
-	}
-
-	return nil
-}
-
 func (s *Site) NewPageFrom(buf io.Reader, name string) (*Page, error) {
 	p, err := s.NewPage(name)
 	if err != nil {
@@ -856,7 +832,6 @@
 }
 
 func (p *Page) initURLs() error {
-	// TODO(bep) output
 	if len(p.outputFormats) == 0 {
 		p.outputFormats = p.s.outputFormats[p.Kind]
 	}
--- a/hugolib/page_output.go
+++ b/hugolib/page_output.go
@@ -148,7 +148,6 @@
 	return o.f.MediaType
 }
 
-// TODO(bep) outputs consider just save this wrapper on Page.
 // OutputFormats gives the output formats for this Page.
 func (p *Page) OutputFormats() OutputFormats {
 	var o OutputFormats
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1706,7 +1706,6 @@
 	close(errs)
 }
 
-// TODO(bep) output remove
 func (s *Site) appendThemeTemplates(in []string) []string {
 	if !s.PathSpec.ThemeSet() {
 		return in
--- a/hugolib/site_render.go
+++ b/hugolib/site_render.go
@@ -173,13 +173,6 @@
 		return nil
 	}
 
-	layouts := p.rssLayouts()
-
-	if layouts == nil {
-		// No RSS for this Kind of page.
-		return nil
-	}
-
 	p.Kind = kindRSS
 
 	// TODO(bep) we zero the date here to get the number of diffs down in
@@ -196,6 +189,11 @@
 		p.Data["Pages"] = p.Pages
 	}
 
+	layouts := s.layoutHandler.For(
+		p.layoutDescriptor,
+		"",
+		p.outputFormat)
+
 	// TODO(bep) output deprecate/handle rssURI
 	targetPath, err := p.targetPath()
 	if err != nil {
@@ -203,7 +201,7 @@
 	}
 
 	return s.renderAndWriteXML(p.Title,
-		targetPath, p, s.appendThemeTemplates(layouts)...)
+		targetPath, p, layouts...)
 }
 
 func (s *Site) render404() error {
--- a/output/layout.go
+++ b/output/layout.go
@@ -48,7 +48,20 @@
 	return &LayoutHandler{hasTheme: hasTheme, cache: make(map[layoutCacheKey][]string)}
 }
 
+// RSS:
+// Home:"rss.xml", "_default/rss.xml", "_internal/_default/rss.xml"
+// Section: "section/" + section + ".rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"
+// Taxonomy "taxonomy/" + singular + ".rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"
+// Tax term: taxonomy/" + singular + ".terms.rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"
+
 const (
+
+	// The RSS templates doesn't map easily into the regular pages.
+	layoutsRSSHome         = `NAME.SUFFIX _default/NAME.SUFFIX _internal/_default/rss.xml`
+	layoutsRSSSection      = `section/SECTION.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml`
+	layoutsRSSTaxonomy     = `taxonomy/SECTION.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml`
+	layoutsRSSTaxonomyTerm = `taxonomy/SECTION.terms.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml`
+
 	layoutsHome    = "index.NAME.SUFFIX index.SUFFIX _default/list.NAME.SUFFIX _default/list.SUFFIX"
 	layoutsSection = `
 section/SECTION.NAME.SUFFIX section/SECTION.SUFFIX
@@ -58,13 +71,13 @@
 indexes/SECTION.NAME.SUFFIX indexes/SECTION.SUFFIX
 _default/indexes.NAME.SUFFIX _default/indexes.SUFFIX
 `
-	layoutTaxonomy = `
+	layoutsTaxonomy = `
 taxonomy/SECTION.NAME.SUFFIX taxonomy/SECTION.SUFFIX
 indexes/SECTION.NAME.SUFFIX indexes/SECTION.SUFFIX 
 _default/taxonomy.NAME.SUFFIX _default/taxonomy.SUFFIX
 _default/list.NAME.SUFFIX _default/list.SUFFIX
 `
-	layoutTaxonomyTerm = `
+	layoutsTaxonomyTerm = `
 taxonomy/SECTION.terms.NAME.SUFFIX taxonomy/SECTION.terms.SUFFIX
 _default/terms.NAME.SUFFIX _default/terms.SUFFIX
 indexes/indexes.NAME.SUFFIX indexes/indexes.SUFFIX
@@ -90,18 +103,27 @@
 		layout = layoutOverride
 	}
 
-	switch d.Kind {
-	// TODO(bep) move the Kind constants some common place.
-	case "home":
-		layouts = resolveTemplate(layoutsHome, d, f)
-	case "section":
-		layouts = resolveTemplate(layoutsSection, d, f)
-	case "taxonomy":
-		layouts = resolveTemplate(layoutTaxonomy, d, f)
-	case "taxonomyTerm":
-		layouts = resolveTemplate(layoutTaxonomyTerm, d, f)
-	case "page":
+	isRSS := f.Name == RSSType.Name
+
+	if d.Kind == "page" {
+		if isRSS {
+			return []string{}
+		}
 		layouts = regularPageLayouts(d.Type, layout, f)
+	} else {
+		if isRSS {
+			layouts = resolveListTemplate(d, f,
+				layoutsRSSHome,
+				layoutsRSSSection,
+				layoutsRSSTaxonomy,
+				layoutsRSSTaxonomyTerm)
+		} else {
+			layouts = resolveListTemplate(d, f,
+				layoutsHome,
+				layoutsSection,
+				layoutsTaxonomy,
+				layoutsTaxonomyTerm)
+		}
 	}
 
 	if l.hasTheme {
@@ -133,6 +155,27 @@
 	l.mu.Lock()
 	l.cache[key] = layouts
 	l.mu.Unlock()
+
+	return layouts
+}
+
+func resolveListTemplate(d LayoutDescriptor, f Format,
+	homeLayouts,
+	sectionLayouts,
+	taxonomyLayouts,
+	taxonomyTermLayouts string) []string {
+	var layouts []string
+
+	switch d.Kind {
+	case "home":
+		layouts = resolveTemplate(homeLayouts, d, f)
+	case "section":
+		layouts = resolveTemplate(sectionLayouts, d, f)
+	case "taxonomy":
+		layouts = resolveTemplate(taxonomyLayouts, d, f)
+	case "taxonomyTerm":
+		layouts = resolveTemplate(taxonomyTermLayouts, d, f)
+	}
 
 	return layouts
 }
--- a/output/layout_test.go
+++ b/output/layout_test.go
@@ -55,6 +55,15 @@
 			[]string{"myttype/mysubtype/mylayout.amp.html", "myttype/mysubtype/mylayout.html", "myttype/mylayout.amp.html"}},
 		{"Page with overridden layout", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype"}, false, "myotherlayout", ampType,
 			[]string{"myttype/myotherlayout.amp.html", "myttype/myotherlayout.html"}},
+		// RSS
+		{"RSS Home with theme", LayoutDescriptor{Kind: "home"}, true, "", RSSType,
+			[]string{"rss.xml", "_default/rss.xml", "theme/rss.xml", "theme/_default/rss.xml", "_internal/_default/rss.xml"}},
+		{"RSS Section", LayoutDescriptor{Kind: "section", Section: "sect1"}, false, "", RSSType,
+			[]string{"section/sect1.rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}},
+		{"RSS Taxonomy", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, false, "", RSSType,
+			[]string{"taxonomy/tag.rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}},
+		{"RSS Taxonomy term", LayoutDescriptor{Kind: "taxonomyTerm", Section: "tag"}, false, "", RSSType,
+			[]string{"taxonomy/tag.terms.rss.xml", "_default/rss.xml", "rss.xml", "_internal/_default/rss.xml"}},
 	} {
 		t.Run(this.name, func(t *testing.T) {
 			l := NewLayoutHandler(this.hasTheme)