From 0f40e1fadfca2276f65adefa6d7d5d63aef9160a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 20 Jun 2017 08:45:52 +0200 Subject: media, hugolib: Support extension-less media types This change is motivated by Netlify's `_redirects` files, which is currently not possible to generate with Hugo. This commit adds a `Delimiter` field to media type, which defaults to ".", but can be blanked out. Fixes #3614 --- hugolib/page_paths.go | 8 ++--- hugolib/page_paths_test.go | 14 +++++++++ hugolib/site_output_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) (limited to 'hugolib') diff --git a/hugolib/page_paths.go b/hugolib/page_paths.go index 8aa70b95b..73fd62278 100644 --- a/hugolib/page_paths.go +++ b/hugolib/page_paths.go @@ -164,7 +164,7 @@ func createTargetPath(d targetPathDescriptor) string { if d.URL != "" { pagePath = filepath.Join(pagePath, d.URL) if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") { - pagePath = filepath.Join(pagePath, d.Type.BaseName+"."+d.Type.MediaType.Suffix) + pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) } } else { if d.ExpandedPermalink != "" { @@ -184,9 +184,9 @@ func createTargetPath(d targetPathDescriptor) string { } if isUgly { - pagePath += "." + d.Type.MediaType.Suffix + pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix } else { - pagePath = filepath.Join(pagePath, d.Type.BaseName+"."+d.Type.MediaType.Suffix) + pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) } if d.LangPrefix != "" { @@ -207,7 +207,7 @@ func createTargetPath(d targetPathDescriptor) string { base = helpers.FilePathSeparator + d.Type.BaseName } - pagePath += base + "." + d.Type.MediaType.Suffix + pagePath += base + d.Type.MediaType.FullSuffix() if d.LangPrefix != "" { pagePath = filepath.Join(d.LangPrefix, pagePath) diff --git a/hugolib/page_paths_test.go b/hugolib/page_paths_test.go index 9a2db1192..80dc390cc 100644 --- a/hugolib/page_paths_test.go +++ b/hugolib/page_paths_test.go @@ -18,6 +18,8 @@ import ( "strings" "testing" + "github.com/gohugoio/hugo/media" + "fmt" "github.com/gohugoio/hugo/output" @@ -27,6 +29,17 @@ func TestPageTargetPath(t *testing.T) { pathSpec := newTestDefaultPathSpec() + noExtNoDelimMediaType := media.TextType + noExtNoDelimMediaType.Suffix = "" + noExtNoDelimMediaType.Delimiter = "" + + // Netlify style _redirects + noExtDelimFormat := output.Format{ + Name: "NER", + MediaType: noExtNoDelimMediaType, + BaseName: "_redirects", + } + for _, langPrefix := range []string{"", "no"} { for _, uglyURLs := range []bool{false, true} { t.Run(fmt.Sprintf("langPrefix=%q,uglyURLs=%t", langPrefix, uglyURLs), @@ -40,6 +53,7 @@ func TestPageTargetPath(t *testing.T) { {"JSON home", targetPathDescriptor{Kind: KindHome, Type: output.JSONFormat}, "/index.json"}, {"AMP home", targetPathDescriptor{Kind: KindHome, Type: output.AMPFormat}, "/amp/index.html"}, {"HTML home", targetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: output.HTMLFormat}, "/index.html"}, + {"Netlify redirects", targetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: noExtDelimFormat}, "/_redirects"}, {"HTML section list", targetPathDescriptor{ Kind: KindSection, Sections: []string{"sect1"}, diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go index 6aff84397..8455a13f7 100644 --- a/hugolib/site_output_test.go +++ b/hugolib/site_output_test.go @@ -290,3 +290,76 @@ baseName = "feed" require.Equal(t, "http://example.com/blog/feed.xml", s.Info.RSSLink) } + +// Issue #3614 +func TestDotLessOutputFormat(t *testing.T) { + siteConfig := ` +baseURL = "http://example.com/blog" + +paginate = 1 +defaultContentLanguage = "en" + +disableKinds = ["page", "section", "taxonomy", "taxonomyTerm", "sitemap", "robotsTXT", "404"] + +[mediaTypes] +[mediaTypes."text/nodot"] +suffix = "" +delimiter = "" +[mediaTypes."text/defaultdelim"] +suffix = "defd" +[mediaTypes."text/nosuffix"] +suffix = "" +[mediaTypes."text/customdelim"] +suffix = "del" +delimiter = "_" + +[outputs] +home = [ "DOTLESS", "DEF", "NOS", "CUS" ] + +[outputFormats] +[outputFormats.DOTLESS] +mediatype = "text/nodot" +baseName = "_redirects" # This is how Netlify names their redirect files. +[outputFormats.DEF] +mediatype = "text/defaultdelim" +baseName = "defaultdelimbase" +[outputFormats.NOS] +mediatype = "text/nosuffix" +baseName = "nosuffixbase" +[outputFormats.CUS] +mediatype = "text/customdelim" +baseName = "customdelimbase" + +` + + mf := afero.NewMemMapFs() + writeToFs(t, mf, "content/foo.html", `foo`) + writeToFs(t, mf, "layouts/_default/list.dotless", `a dotless`) + writeToFs(t, mf, "layouts/_default/list.def.defd", `default delimim`) + writeToFs(t, mf, "layouts/_default/list.nos", `no suffix`) + writeToFs(t, mf, "layouts/_default/list.cus.del", `custom delim`) + + th, h := newTestSitesFromConfig(t, mf, siteConfig) + + err := h.Build(BuildCfg{}) + + require.NoError(t, err) + + th.assertFileContent("public/_redirects", "a dotless") + th.assertFileContent("public/defaultdelimbase.defd", "default delimim") + // This looks weird, but the user has chosen this definition. + th.assertFileContent("public/nosuffixbase.", "no suffix") + th.assertFileContent("public/customdelimbase_del", "custom delim") + + s := h.Sites[0] + home := s.getPage(KindHome) + require.NotNil(t, home) + + outputs := home.OutputFormats() + + require.Equal(t, "/blog/_redirects", outputs.Get("DOTLESS").RelPermalink()) + require.Equal(t, "/blog/defaultdelimbase.defd", outputs.Get("DEF").RelPermalink()) + require.Equal(t, "/blog/nosuffixbase.", outputs.Get("NOS").RelPermalink()) + require.Equal(t, "/blog/customdelimbase_del", outputs.Get("CUS").RelPermalink()) + +} -- cgit v1.2.3