shithub: hugo

Download patch

ref: de87624241daa86660f205cc72a745409b9c9238
parent: 9ef4dca361727a78e0f66f8f4e54c64e4c4781cb
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Mon Aug 5 06:19:55 EDT 2019

hugolib: Fix output format handling of mix cased page kinds

Fixes #4528

--- a/hugolib/page_kinds.go
+++ b/hugolib/page_kinds.go
@@ -14,6 +14,8 @@
 package hugolib
 
 import (
+	"strings"
+
 	"github.com/gohugoio/hugo/resources/page"
 )
 
@@ -38,3 +40,17 @@
 
 	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)]
+}
--- a/hugolib/site_output.go
+++ b/hugolib/site_output.go
@@ -34,7 +34,7 @@
 		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 @@
 	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 {
--- a/hugolib/site_output_test.go
+++ b/hugolib/site_output_test.go
@@ -327,33 +327,53 @@
 }
 
 func TestCreateSiteOutputFormats(t *testing.T) {
-	assert := require.New(t)
 
-	outputsConfig := map[string]interface{}{
-		page.KindHome:    []string{"HTML", "JSON"},
-		page.KindSection: []string{"JSON"},
-	}
+	t.Run("Basic", func(t *testing.T) {
+		assert := require.New(t)
 
-	cfg := viper.New()
-	cfg.Set("outputs", outputsConfig)
+		outputsConfig := map[string]interface{}{
+			page.KindHome:    []string{"HTML", "JSON"},
+			page.KindSection: []string{"JSON"},
+		}
 
-	outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg)
-	assert.NoError(err)
-	assert.Equal(output.Formats{output.JSONFormat}, outputs[page.KindSection])
-	assert.Equal(output.Formats{output.HTMLFormat, output.JSONFormat}, outputs[page.KindHome])
+		cfg := viper.New()
+		cfg.Set("outputs", outputsConfig)
 
-	// Defaults
-	assert.Equal(output.Formats{output.HTMLFormat, output.RSSFormat}, outputs[page.KindTaxonomy])
-	assert.Equal(output.Formats{output.HTMLFormat, output.RSSFormat}, outputs[page.KindTaxonomyTerm])
-	assert.Equal(output.Formats{output.HTMLFormat}, outputs[page.KindPage])
+		outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg)
+		assert.NoError(err)
+		assert.Equal(output.Formats{output.JSONFormat}, outputs[page.KindSection])
+		assert.Equal(output.Formats{output.HTMLFormat, output.JSONFormat}, outputs[page.KindHome])
 
-	// These aren't (currently) in use when rendering in Hugo,
-	// but the pages needs to be assigned an output format,
-	// so these should also be correct/sensible.
-	assert.Equal(output.Formats{output.RSSFormat}, outputs[kindRSS])
-	assert.Equal(output.Formats{output.SitemapFormat}, outputs[kindSitemap])
-	assert.Equal(output.Formats{output.RobotsTxtFormat}, outputs[kindRobotsTXT])
-	assert.Equal(output.Formats{output.HTMLFormat}, outputs[kind404])
+		// Defaults
+		assert.Equal(output.Formats{output.HTMLFormat, output.RSSFormat}, outputs[page.KindTaxonomy])
+		assert.Equal(output.Formats{output.HTMLFormat, output.RSSFormat}, outputs[page.KindTaxonomyTerm])
+		assert.Equal(output.Formats{output.HTMLFormat}, outputs[page.KindPage])
+
+		// These aren't (currently) in use when rendering in Hugo,
+		// but the pages needs to be assigned an output format,
+		// so these should also be correct/sensible.
+		assert.Equal(output.Formats{output.RSSFormat}, outputs[kindRSS])
+		assert.Equal(output.Formats{output.SitemapFormat}, outputs[kindSitemap])
+		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])
+
+	})
 
 }
 
--- a/resources/page/page_kinds.go
+++ b/resources/page/page_kinds.go
@@ -13,6 +13,8 @@
 
 package page
 
+import "strings"
+
 const (
 	KindPage = "page"
 
@@ -23,3 +25,16 @@
 	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)]
+}
--- a/resources/page/page_kinds_test.go
+++ b/resources/page/page_kinds_test.go
@@ -28,4 +28,10 @@
 	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"))
+
 }