mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
hugolib, layout: Consolidate RSS template handling
This commit is contained in:
parent
ee75e2999b
commit
3cd97951f1
6 changed files with 71 additions and 48 deletions
|
@ -661,30 +661,6 @@ func (p *Page) Section() string {
|
|||
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) RelPermalink() string {
|
|||
}
|
||||
|
||||
func (p *Page) initURLs() error {
|
||||
// TODO(bep) output
|
||||
if len(p.outputFormats) == 0 {
|
||||
p.outputFormats = p.s.outputFormats[p.Kind]
|
||||
}
|
||||
|
|
|
@ -148,7 +148,6 @@ func (o OutputFormat) MediaType() media.Type {
|
|||
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
|
||||
|
|
|
@ -1706,7 +1706,6 @@ func errorCollator(results <-chan error, errs chan<- error) {
|
|||
close(errs)
|
||||
}
|
||||
|
||||
// TODO(bep) output remove
|
||||
func (s *Site) appendThemeTemplates(in []string) []string {
|
||||
if !s.PathSpec.ThemeSet() {
|
||||
return in
|
||||
|
|
|
@ -173,13 +173,6 @@ func (s *Site) renderRSS(p *PageOutput) error {
|
|||
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 @@ func (s *Site) renderRSS(p *PageOutput) error {
|
|||
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 @@ func (s *Site) renderRSS(p *PageOutput) error {
|
|||
}
|
||||
|
||||
return s.renderAndWriteXML(p.Title,
|
||||
targetPath, p, s.appendThemeTemplates(layouts)...)
|
||||
targetPath, p, layouts...)
|
||||
}
|
||||
|
||||
func (s *Site) render404() error {
|
||||
|
|
|
@ -48,7 +48,20 @@ func NewLayoutHandler(hasTheme bool) *LayoutHandler {
|
|||
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 @@ _default/list.NAME.SUFFIX _default/list.SUFFIX
|
|||
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 @@ func (l *LayoutHandler) For(d LayoutDescriptor, layoutOverride string, f Format)
|
|||
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 {
|
||||
|
@ -137,6 +159,27 @@ func (l *LayoutHandler) For(d LayoutDescriptor, layoutOverride string, f Format)
|
|||
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
|
||||
}
|
||||
|
||||
func resolveTemplate(templ string, d LayoutDescriptor, f Format) []string {
|
||||
return strings.Fields(replaceKeyValues(templ,
|
||||
"SUFFIX", f.MediaType.Suffix,
|
||||
|
|
|
@ -55,6 +55,15 @@ func TestLayout(t *testing.T) {
|
|||
[]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)
|
||||
|
|
Loading…
Reference in a new issue