diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-06-20 08:45:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2017-06-20 11:04:14 +0200 |
commit | 0f40e1fadfca2276f65adefa6d7d5d63aef9160a (patch) | |
tree | 7bd0a02e660facfa8200ad3b9adf68ff3c7f3e3d /output | |
parent | 516e6c6dc5733cdaf985317d58eedbc6ec0ef2f7 (diff) | |
download | hugo-0f40e1fadfca2276f65adefa6d7d5d63aef9160a.tar.gz hugo-0f40e1fadfca2276f65adefa6d7d5d63aef9160a.zip |
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
Diffstat (limited to 'output')
-rw-r--r-- | output/layout.go | 43 | ||||
-rw-r--r-- | output/layout_test.go | 38 |
2 files changed, 66 insertions, 15 deletions
diff --git a/output/layout.go b/output/layout.go index 6dba7f3b4..cacb92b80 100644 --- a/output/layout.go +++ b/output/layout.go @@ -181,17 +181,37 @@ func resolveListTemplate(d LayoutDescriptor, f Format, case "taxonomyTerm": layouts = resolveTemplate(taxonomyTermLayouts, d, f) } - return layouts } func resolveTemplate(templ string, d LayoutDescriptor, f Format) []string { + delim := "." + if f.MediaType.Delimiter == "" { + delim = "" + } layouts := strings.Fields(replaceKeyValues(templ, - "SUFFIX", f.MediaType.Suffix, + ".SUFFIX", delim+f.MediaType.Suffix, "NAME", strings.ToLower(f.Name), "SECTION", d.Section)) - return layouts + return filterDotLess(layouts) +} + +func filterDotLess(layouts []string) []string { + var filteredLayouts []string + + for _, l := range layouts { + // This may be constructed, but media types can be suffix-less, but can contain + // a delimiter. + l = strings.TrimSuffix(l, ".") + // If media type has no suffix, we have "index" type of layouts in this list, which + // doesn't make much sense. + if strings.Contains(l, ".") { + filteredLayouts = append(filteredLayouts, l) + } + } + + return filteredLayouts } func prependTextPrefixIfNeeded(f Format, layouts ...string) []string { @@ -220,7 +240,12 @@ func regularPageLayouts(types string, layout string, f Format) []string { layout = "single" } - suffix := f.MediaType.Suffix + delimiter := "." + if f.MediaType.Delimiter == "" { + delimiter = "" + } + + suffix := delimiter + f.MediaType.Suffix name := strings.ToLower(f.Name) if types != "" { @@ -229,15 +254,15 @@ func regularPageLayouts(types string, layout string, f Format) []string { // Add type/layout.html for i := range t { search := t[:len(t)-i] - layouts = append(layouts, fmt.Sprintf("%s/%s.%s.%s", strings.ToLower(path.Join(search...)), layout, name, suffix)) - layouts = append(layouts, fmt.Sprintf("%s/%s.%s", strings.ToLower(path.Join(search...)), layout, suffix)) + layouts = append(layouts, fmt.Sprintf("%s/%s.%s%s", strings.ToLower(path.Join(search...)), layout, name, suffix)) + layouts = append(layouts, fmt.Sprintf("%s/%s%s", strings.ToLower(path.Join(search...)), layout, suffix)) } } // Add _default/layout.html - layouts = append(layouts, fmt.Sprintf("_default/%s.%s.%s", layout, name, suffix)) - layouts = append(layouts, fmt.Sprintf("_default/%s.%s", layout, suffix)) + layouts = append(layouts, fmt.Sprintf("_default/%s.%s%s", layout, name, suffix)) + layouts = append(layouts, fmt.Sprintf("_default/%s%s", layout, suffix)) - return layouts + return filterDotLess(layouts) } diff --git a/output/layout_test.go b/output/layout_test.go index 56aac00d5..9d4d2f6d5 100644 --- a/output/layout_test.go +++ b/output/layout_test.go @@ -21,14 +21,34 @@ import ( "github.com/stretchr/testify/require" ) -var ampType = Format{ - Name: "AMP", - MediaType: media.HTMLType, - BaseName: "index", -} - func TestLayout(t *testing.T) { + noExtNoDelimMediaType := media.TextType + noExtNoDelimMediaType.Suffix = "" + noExtNoDelimMediaType.Delimiter = "" + + noExtMediaType := media.TextType + noExtMediaType.Suffix = "" + + var ( + ampType = Format{ + Name: "AMP", + MediaType: media.HTMLType, + BaseName: "index", + } + + noExtDelimFormat = Format{ + Name: "NEM", + MediaType: noExtNoDelimMediaType, + BaseName: "_redirects", + } + noExt = Format{ + Name: "NEX", + MediaType: noExtMediaType, + BaseName: "next", + } + ) + for _, this := range []struct { name string d LayoutDescriptor @@ -39,6 +59,12 @@ func TestLayout(t *testing.T) { }{ {"Home", LayoutDescriptor{Kind: "home"}, true, "", ampType, []string{"index.amp.html", "index.html", "_default/list.amp.html", "_default/list.html", "theme/index.amp.html", "theme/index.html"}}, + {"Home, no ext or delim", LayoutDescriptor{Kind: "home"}, true, "", noExtDelimFormat, + []string{"index.nem", "_default/list.nem"}}, + {"Home, no ext", LayoutDescriptor{Kind: "home"}, true, "", noExt, + []string{"index.nex", "_default/list.nex"}}, + {"Page, no ext or delim", LayoutDescriptor{Kind: "page"}, true, "", noExtDelimFormat, + []string{"_default/single.nem", "theme/_default/single.nem"}}, {"Section", LayoutDescriptor{Kind: "section", Section: "sect1"}, false, "", ampType, []string{"section/sect1.amp.html", "section/sect1.html"}}, {"Taxonomy", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, false, "", ampType, |