mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
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
This commit is contained in:
+34
-9
@@ -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)
|
||||
}
|
||||
|
||||
+32
-6
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user