hugolib: Fix output format handling of mix cased page kinds
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 5 Aug 2019 08:19:55 +0000 (10:19 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 5 Aug 2019 09:22:03 +0000 (11:22 +0200)
Fixes #4528

hugolib/page_kinds.go
hugolib/site_output.go
hugolib/site_output_test.go
resources/page/page_kinds.go
resources/page/page_kinds_test.go

index 39de31a169b687d7a47394a98129101791a2214a..6b6a00c5f2a620d89a8934598962a22ef74df024 100644 (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)]
+}
index 9fb2365066ed168770575627ac7fa63d995f568a..b0a5d4f35a5b64091ac2c95474895a4f01090389 100644 (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 {
index 1536817b0919657a4620d1eae19cbff1d398366a..90e023225fbfc3f62ea4ab6a211d2e4d2d97f312 100644 (file)
@@ -327,33 +327,53 @@ baseName = "customdelimbase"
 }
 
 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])
-
-       // 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])
+               cfg := viper.New()
+               cfg.Set("outputs", outputsConfig)
+
+               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])
+
+               // 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])
+
+       })
 
 }
 
index a2e59438ef0fa7797b9407144eb987936ddf8142..1f59ec8699a42c701c634e4fa54de7a19984001a 100644 (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)]
+}
index 8ad7343dcdc495121df74b08b001172a91f125c1..12f1c598efb13cd3196c9202d99c2892f592880b 100644 (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"))
+
 }