mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-01 22:42:45 +02:00
hugolib, output: Incorporate suffix and type in layout resolve
And remove some now superflous and hard to maintain tests.
This commit is contained in:
@@ -37,12 +37,27 @@ func NewLayoutHandler(hasTheme bool) *LayoutHandler {
|
||||
return &LayoutHandler{hasTheme: hasTheme}
|
||||
}
|
||||
|
||||
// TODO(bep) output theme layouts
|
||||
var (
|
||||
layoutsHome = "index.html _default/list.html"
|
||||
layoutsSection = "section/SECTION.html SECTION/list.html _default/section.html _default/list.html indexes/SECTION.html _default/indexes.html"
|
||||
layoutTaxonomy = "taxonomy/SECTION.html indexes/SECTION.html _default/taxonomy.html _default/list.html"
|
||||
layoutTaxonomyTerm = "taxonomy/SECTION.terms.html _default/terms.html indexes/indexes.html"
|
||||
const (
|
||||
layoutsHome = "index.NAME.SUFFIX index.SUFFIX _default/list.NAME.SUFFIX _default/list.SUFFIX"
|
||||
layoutsSection = `
|
||||
section/SECTION.NAME.SUFFIX section/SECTION.SUFFIX
|
||||
SECTION/list.NAME.SUFFIX SECTION/list.SUFFIX
|
||||
_default/section.NAME.SUFFIX _default/section.SUFFIX
|
||||
_default/list.NAME.SUFFIX _default/list.SUFFIX
|
||||
indexes/SECTION.NAME.SUFFIX indexes/SECTION.SUFFIX
|
||||
_default/indexes.NAME.SUFFIX _default/indexes.SUFFIX
|
||||
`
|
||||
layoutTaxonomy = `
|
||||
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 = `
|
||||
taxonomy/SECTION.terms.NAME.SUFFIX taxonomy/SECTION.terms.SUFFIX
|
||||
_default/terms.NAME.SUFFIX _default/terms.SUFFIX
|
||||
indexes/indexes.NAME.SUFFIX indexes/indexes.SUFFIX
|
||||
`
|
||||
)
|
||||
|
||||
func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type) []string {
|
||||
@@ -57,15 +72,15 @@ func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type)
|
||||
switch id.PageKind() {
|
||||
// TODO(bep) move the Kind constants some common place.
|
||||
case "home":
|
||||
layouts = strings.Fields(layoutsHome)
|
||||
layouts = resolveTemplate(layoutsHome, id, tp)
|
||||
case "section":
|
||||
layouts = strings.Fields(strings.Replace(layoutsSection, "SECTION", id.PageSection(), -1))
|
||||
layouts = resolveTemplate(layoutsSection, id, tp)
|
||||
case "taxonomy":
|
||||
layouts = strings.Fields(strings.Replace(layoutTaxonomy, "SECTION", id.PageSection(), -1))
|
||||
layouts = resolveTemplate(layoutTaxonomy, id, tp)
|
||||
case "taxonomyTerm":
|
||||
layouts = strings.Fields(strings.Replace(layoutTaxonomyTerm, "SECTION", id.PageSection(), -1))
|
||||
layouts = resolveTemplate(layoutTaxonomyTerm, id, tp)
|
||||
case "page":
|
||||
layouts = regularPageLayouts(id.PageType(), layout)
|
||||
layouts = regularPageLayouts(id.PageType(), layout, tp)
|
||||
}
|
||||
|
||||
if l.hasTheme {
|
||||
@@ -97,23 +112,41 @@ func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type)
|
||||
return layouts
|
||||
}
|
||||
|
||||
func regularPageLayouts(types string, layout string) (layouts []string) {
|
||||
func resolveTemplate(templ string, id LayoutIdentifier, tp Type) []string {
|
||||
return strings.Fields(replaceKeyValues(templ,
|
||||
"SUFFIX", tp.MediaType.Suffix,
|
||||
"NAME", strings.ToLower(tp.Name),
|
||||
"SECTION", id.PageSection()))
|
||||
}
|
||||
|
||||
func replaceKeyValues(s string, oldNew ...string) string {
|
||||
replacer := strings.NewReplacer(oldNew...)
|
||||
return replacer.Replace(s)
|
||||
}
|
||||
|
||||
func regularPageLayouts(types string, layout string, tp Type) (layouts []string) {
|
||||
if layout == "" {
|
||||
layout = "single"
|
||||
}
|
||||
|
||||
suffix := tp.MediaType.Suffix
|
||||
name := strings.ToLower(tp.Name)
|
||||
|
||||
if types != "" {
|
||||
t := strings.Split(types, "/")
|
||||
|
||||
// Add type/layout.html
|
||||
for i := range t {
|
||||
search := t[:len(t)-i]
|
||||
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
|
||||
layouts = append(layouts, fmt.Sprintf("%s/%s.%s.%s", strings.ToLower(path.Join(search...)), layout, name, suffix))
|
||||
layouts = append(layouts, fmt.Sprintf("%s/%s.%s", strings.ToLower(path.Join(search...)), layout, suffix))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Add _default/layout.html
|
||||
layouts = append(layouts, fmt.Sprintf("_default/%s.html", layout))
|
||||
layouts = append(layouts, fmt.Sprintf("_default/%s.%s.%s", layout, name, suffix))
|
||||
layouts = append(layouts, fmt.Sprintf("_default/%s.%s", layout, suffix))
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -14,9 +14,10 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/hugo/media"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -43,46 +44,56 @@ func (l testLayoutIdentifier) PageSection() string {
|
||||
return l.pageSection
|
||||
}
|
||||
|
||||
var ampType = Type{
|
||||
Name: "AMP",
|
||||
MediaType: media.HTMLType,
|
||||
BaseName: "index",
|
||||
}
|
||||
|
||||
func TestLayout(t *testing.T) {
|
||||
|
||||
for i, this := range []struct {
|
||||
for _, this := range []struct {
|
||||
name string
|
||||
li testLayoutIdentifier
|
||||
hasTheme bool
|
||||
layoutOverride string
|
||||
tp Type
|
||||
expect []string
|
||||
}{
|
||||
{testLayoutIdentifier{"home", "", "", ""}, true, "", HTMLType,
|
||||
[]string{"index.html", "_default/list.html", "theme/index.html", "theme/_default/list.html"}},
|
||||
{testLayoutIdentifier{"section", "sect1", "", ""}, false, "", HTMLType,
|
||||
[]string{"section/sect1.html", "sect1/list.html"}},
|
||||
{testLayoutIdentifier{"taxonomy", "tag", "", ""}, false, "", HTMLType,
|
||||
[]string{"taxonomy/tag.html", "indexes/tag.html"}},
|
||||
{testLayoutIdentifier{"taxonomyTerm", "categories", "", ""}, false, "", HTMLType,
|
||||
[]string{"taxonomy/categories.terms.html", "_default/terms.html"}},
|
||||
{testLayoutIdentifier{"page", "", "", ""}, true, "", HTMLType,
|
||||
[]string{"_default/single.html", "theme/_default/single.html"}},
|
||||
{testLayoutIdentifier{"page", "", "mylayout", ""}, false, "", HTMLType,
|
||||
[]string{"_default/mylayout.html"}},
|
||||
{testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "", HTMLType,
|
||||
[]string{"myttype/mylayout.html", "_default/mylayout.html"}},
|
||||
{testLayoutIdentifier{"page", "", "mylayout", "myttype/mysubtype"}, false, "", HTMLType,
|
||||
[]string{"myttype/mysubtype/mylayout.html", "myttype/mylayout.html", "_default/mylayout.html"}},
|
||||
{testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "myotherlayout", HTMLType,
|
||||
[]string{"myttype/myotherlayout.html", "_default/myotherlayout.html"}},
|
||||
{"Home", testLayoutIdentifier{"home", "", "", ""}, true, "", ampType,
|
||||
[]string{"index.amp.html", "index.html", "_default/list.amp.html", "_default/list.html", "theme/index.amp.html", "theme/index.html"}},
|
||||
{"Section", testLayoutIdentifier{"section", "sect1", "", ""}, false, "", ampType,
|
||||
[]string{"section/sect1.amp.html", "section/sect1.html"}},
|
||||
{"Taxonomy", testLayoutIdentifier{"taxonomy", "tag", "", ""}, false, "", ampType,
|
||||
[]string{"taxonomy/tag.amp.html", "taxonomy/tag.html"}},
|
||||
{"Taxonomy term", testLayoutIdentifier{"taxonomyTerm", "categories", "", ""}, false, "", ampType,
|
||||
[]string{"taxonomy/categories.terms.amp.html", "taxonomy/categories.terms.html", "_default/terms.amp.html"}},
|
||||
{"Page", testLayoutIdentifier{"page", "", "", ""}, true, "", ampType,
|
||||
[]string{"_default/single.amp.html", "_default/single.html", "theme/_default/single.amp.html"}},
|
||||
{"Page with layout", testLayoutIdentifier{"page", "", "mylayout", ""}, false, "", ampType,
|
||||
[]string{"_default/mylayout.amp.html", "_default/mylayout.html"}},
|
||||
{"Page with layout and type", testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "", ampType,
|
||||
[]string{"myttype/mylayout.amp.html", "myttype/mylayout.html", "_default/mylayout.amp.html"}},
|
||||
{"Page with layout and type with subtype", testLayoutIdentifier{"page", "", "mylayout", "myttype/mysubtype"}, false, "", ampType,
|
||||
[]string{"myttype/mysubtype/mylayout.amp.html", "myttype/mysubtype/mylayout.html", "myttype/mylayout.amp.html"}},
|
||||
{"Page with overridden layout", testLayoutIdentifier{"page", "", "mylayout", "myttype"}, false, "myotherlayout", ampType,
|
||||
[]string{"myttype/myotherlayout.amp.html", "myttype/myotherlayout.html"}},
|
||||
} {
|
||||
l := NewLayoutHandler(this.hasTheme)
|
||||
logMsg := fmt.Sprintf("Test %d", i)
|
||||
layouts := l.For(this.li, this.layoutOverride, this.tp)
|
||||
require.NotNil(t, layouts, logMsg)
|
||||
require.True(t, len(layouts) >= len(this.expect), logMsg)
|
||||
// Not checking the complete list for now ...
|
||||
require.Equal(t, this.expect, layouts[:len(this.expect)], logMsg)
|
||||
t.Run(this.name, func(t *testing.T) {
|
||||
l := NewLayoutHandler(this.hasTheme)
|
||||
|
||||
if !this.hasTheme {
|
||||
for _, layout := range layouts {
|
||||
require.NotContains(t, layout, "theme", logMsg)
|
||||
layouts := l.For(this.li, this.layoutOverride, this.tp)
|
||||
|
||||
require.NotNil(t, layouts)
|
||||
require.True(t, len(layouts) >= len(this.expect))
|
||||
// Not checking the complete list for now ...
|
||||
require.Equal(t, this.expect, layouts[:len(this.expect)])
|
||||
|
||||
if !this.hasTheme {
|
||||
for _, layout := range layouts {
|
||||
require.NotContains(t, layout, "theme")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user