diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-20 08:45:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-20 11:04:14 +0200 |
commit | 0f40e1fadfca2276f65adefa6d7d5d63aef9160a (patch) | |
tree | 7bd0a02e660facfa8200ad3b9adf68ff3c7f3e3d /output/layout.go | |
parent | 516e6c6dc5733cdaf985317d58eedbc6ec0ef2f7 (diff) |
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/layout.go')
-rw-r--r-- | output/layout.go | 43 |
1 files changed, 34 insertions, 9 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) } |