shithub: hugo

Download patch

ref: 6e2f2dd8d3ca61c92a2ee8824fbf05cadef08425
parent: 94b5be67fc73b87d114d94a7bb1a33ab997f30f1
author: Bjørn Erik Pedersen <[email protected]>
date: Wed May 17 14:04:07 EDT 2017

hugolib: Fix output formats override when no outputs definition given

A common use case for this is to redefine the built-in output format `RSS` to give it a different URL.

Before this commit, that was not possible without also providing an `outputs` definition.

Fixes #3447

--- a/hugolib/site_output.go
+++ b/hugolib/site_output.go
@@ -26,7 +26,7 @@
 
 func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
 	if !cfg.IsSet("outputs") {
-		return createDefaultOutputFormats(cfg)
+		return createDefaultOutputFormats(allFormats, cfg)
 	}
 
 	outFormats := make(map[string]output.Formats)
@@ -64,20 +64,22 @@
 
 }
 
-func createDefaultOutputFormats(cfg config.Provider) (map[string]output.Formats, error) {
+func createDefaultOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
 	outFormats := make(map[string]output.Formats)
+	rssOut, _ := allFormats.GetByName(output.RSSFormat.Name)
+	htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
+
 	for _, kind := range allKinds {
 		var formats output.Formats
 		// All have HTML
-		formats = append(formats, output.HTMLFormat)
+		formats = append(formats, htmlOut)
 
 		// All but page have RSS
 		if kind != KindPage {
-			rssType := output.RSSFormat
 
 			rssBase := cfg.GetString("rssURI")
 			if rssBase == "" || rssBase == "index.xml" {
-				rssBase = rssType.BaseName
+				rssBase = rssOut.BaseName
 			} else {
 				// Remove in Hugo 0.22.
 				helpers.Deprecated("Site config", "rssURI", "Set baseName in outputFormats.RSS", false)
@@ -85,8 +87,8 @@
 				rssBase = strings.TrimSuffix(rssBase, path.Ext(rssBase))
 			}
 
-			rssType.BaseName = rssBase
-			formats = append(formats, rssType)
+			rssOut.BaseName = rssBase
+			formats = append(formats, rssOut)
 
 		}
 
--- a/hugolib/site_output_test.go
+++ b/hugolib/site_output_test.go
@@ -31,7 +31,7 @@
 
 func TestDefaultOutputFormats(t *testing.T) {
 	t.Parallel()
-	defs, err := createDefaultOutputFormats(viper.New())
+	defs, err := createDefaultOutputFormats(output.DefaultFormats, viper.New())
 
 	require.NoError(t, err)
 
@@ -53,6 +53,30 @@
 	}
 }
 
+func TestDefaultOutputFormatsWithOverrides(t *testing.T) {
+	t.Parallel()
+
+	htmlOut := output.HTMLFormat
+	htmlOut.BaseName = "htmlindex"
+	rssOut := output.RSSFormat
+	rssOut.BaseName = "feed"
+
+	defs, err := createDefaultOutputFormats(output.Formats{htmlOut, rssOut}, viper.New())
+
+	homeDefs := defs[KindHome]
+
+	rss, found := homeDefs.GetByName("RSS")
+	require.True(t, found)
+	require.Equal(t, rss.BaseName, "feed")
+
+	html, found := homeDefs.GetByName("HTML")
+	require.True(t, found)
+	require.Equal(t, html.BaseName, "htmlindex")
+
+	require.NoError(t, err)
+
+}
+
 func TestSiteWithPageOutputs(t *testing.T) {
 	for _, outputs := range [][]string{{"html", "json", "calendar"}, {"json"}} {
 		t.Run(fmt.Sprintf("%v", outputs), func(t *testing.T) {
@@ -229,5 +253,35 @@
 		require.Equal(t, "/blog/index.ics", cal.RelPermalink())
 		require.Equal(t, "webcal://example.com/blog/index.ics", cal.Permalink())
 	}
+
+}
+
+// Issue #3447
+func TestRedefineRSSOutputFormat(t *testing.T) {
+	siteConfig := `
+baseURL = "http://example.com/blog"
+
+paginate = 1
+defaultContentLanguage = "en"
+
+disableKinds = ["page", "section", "taxonomy", "taxonomyTerm", "sitemap", "robotsTXT", "404"]
+
+[outputFormats]
+[outputFormats.RSS]
+mediatype = "application/rss"
+baseName = "feed"
+
+`
+
+	mf := afero.NewMemMapFs()
+	writeToFs(t, mf, "content/foo.html", `foo`)
+
+	th, h := newTestSitesFromConfig(t, mf, siteConfig)
+
+	err := h.Build(BuildCfg{})
+
+	require.NoError(t, err)
+
+	th.assertFileContent("public/feed.xml", "Recent content on")
 
 }