hugolib: Fix output format handling of mix cased page kinds

Fixes #4528
This commit is contained in:
Bjørn Erik Pedersen 2019-08-05 10:19:55 +02:00
parent 9ef4dca361
commit de87624241
5 changed files with 85 additions and 23 deletions

View file

@ -14,6 +14,8 @@
package hugolib
import (
"strings"
"github.com/gohugoio/hugo/resources/page"
)
@ -38,3 +40,17 @@ const (
pageResourceType = "page"
)
var kindMap = map[string]string{
strings.ToLower(kindRSS): kindRSS,
strings.ToLower(kindSitemap): kindSitemap,
strings.ToLower(kindRobotsTXT): kindRobotsTXT,
strings.ToLower(kind404): kind404,
}
func getKind(s string) string {
if pkind := page.GetKind(s); pkind != "" {
return pkind
}
return kindMap[strings.ToLower(s)]
}

View file

@ -34,7 +34,7 @@ func createDefaultOutputFormats(allFormats output.Formats, cfg config.Provider)
page.KindSection: {htmlOut, rssOut},
page.KindTaxonomy: {htmlOut, rssOut},
page.KindTaxonomyTerm: {htmlOut, rssOut},
// Below are for conistency. They are currently not used during rendering.
// Below are for consistency. They are currently not used during rendering.
kindRSS: {rssOut},
kindSitemap: {sitemapOut},
kindRobotsTXT: {robotsOut},
@ -61,6 +61,11 @@ func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (ma
seen := make(map[string]bool)
for k, v := range outputs {
k = getKind(k)
if k == "" {
// Invalid kind
continue
}
var formats output.Formats
vals := cast.ToStringSlice(v)
for _, format := range vals {

View file

@ -327,6 +327,8 @@ baseName = "customdelimbase"
}
func TestCreateSiteOutputFormats(t *testing.T) {
t.Run("Basic", func(t *testing.T) {
assert := require.New(t)
outputsConfig := map[string]interface{}{
@ -355,6 +357,24 @@ func TestCreateSiteOutputFormats(t *testing.T) {
assert.Equal(output.Formats{output.RobotsTxtFormat}, outputs[kindRobotsTXT])
assert.Equal(output.Formats{output.HTMLFormat}, outputs[kind404])
})
// Issue #4528
t.Run("Mixed case", func(t *testing.T) {
assert := require.New(t)
cfg := viper.New()
outputsConfig := map[string]interface{}{
"taxonomyterm": []string{"JSON"},
}
cfg.Set("outputs", outputsConfig)
outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg)
assert.NoError(err)
assert.Equal(output.Formats{output.JSONFormat}, outputs[page.KindTaxonomyTerm])
})
}
func TestCreateSiteOutputFormatsInvalidConfig(t *testing.T) {

View file

@ -13,6 +13,8 @@
package page
import "strings"
const (
KindPage = "page"
@ -23,3 +25,16 @@ const (
KindTaxonomy = "taxonomy"
KindTaxonomyTerm = "taxonomyTerm"
)
var kindMap = map[string]string{
strings.ToLower(KindPage): KindPage,
strings.ToLower(KindHome): KindHome,
strings.ToLower(KindSection): KindSection,
strings.ToLower(KindTaxonomy): KindTaxonomy,
strings.ToLower(KindTaxonomyTerm): KindTaxonomyTerm,
}
// GetKind gets the page kind given a string, empty if not found.
func GetKind(s string) string {
return kindMap[strings.ToLower(s)]
}

View file

@ -28,4 +28,10 @@ func TestKind(t *testing.T) {
require.Equal(t, "taxonomy", KindTaxonomy)
require.Equal(t, "taxonomyTerm", KindTaxonomyTerm)
require.Equal(t, KindTaxonomyTerm, GetKind("TAXONOMYTERM"))
require.Equal(t, KindTaxonomy, GetKind("Taxonomy"))
require.Equal(t, KindPage, GetKind("Page"))
require.Equal(t, KindHome, GetKind("Home"))
require.Equal(t, KindSection, GetKind("SEction"))
}