mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
media: Make Type comparable
So we can use it and output.Format as map key etc. This commit also fixes the media.Type implementation so it does not need to mutate itself to handle different suffixes for the same MIME type, e.g. jpg vs. jpeg. This means that there are no Suffix or FullSuffix on media.Type anymore. Fixes #8317 Fixes #8324
This commit is contained in:
@@ -265,7 +265,7 @@ func (f Format) SupportsTransparency() bool {
|
||||
// DefaultExtension returns the default file extension of this format, starting with a dot.
|
||||
// For example: .jpg for JPEG
|
||||
func (f Format) DefaultExtension() string {
|
||||
return f.MediaType().FullSuffix()
|
||||
return f.MediaType().FirstSuffix.FullSuffix
|
||||
}
|
||||
|
||||
// MediaType returns the media type of this image, e.g. image/jpeg for JPEG
|
||||
|
@@ -128,6 +128,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
|
||||
}
|
||||
|
||||
pagePath := slash
|
||||
fullSuffix := d.Type.MediaType.FirstSuffix.FullSuffix
|
||||
|
||||
var (
|
||||
pagePathDir string
|
||||
@@ -172,7 +173,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
|
||||
hasSlash := strings.HasSuffix(d.URL, slash)
|
||||
|
||||
if hasSlash || !hasDot {
|
||||
pagePath = pjoin(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
|
||||
pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
|
||||
} else if hasDot {
|
||||
pagePathDir = path.Dir(pagePathDir)
|
||||
}
|
||||
@@ -229,9 +230,9 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
|
||||
linkDir = pagePathDir
|
||||
|
||||
if isUgly {
|
||||
pagePath = addSuffix(pagePath, d.Type.MediaType.FullSuffix())
|
||||
pagePath = addSuffix(pagePath, fullSuffix)
|
||||
} else {
|
||||
pagePath = pjoin(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
|
||||
pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
|
||||
}
|
||||
|
||||
if !isHtmlIndex(pagePath) {
|
||||
@@ -267,9 +268,9 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
|
||||
linkDir = pagePathDir
|
||||
|
||||
if base != "" {
|
||||
pagePath = path.Join(pagePath, addSuffix(base, d.Type.MediaType.FullSuffix()))
|
||||
pagePath = path.Join(pagePath, addSuffix(base, fullSuffix))
|
||||
} else {
|
||||
pagePath = addSuffix(pagePath, d.Type.MediaType.FullSuffix())
|
||||
pagePath = addSuffix(pagePath, fullSuffix)
|
||||
}
|
||||
|
||||
if !isHtmlIndex(pagePath) {
|
||||
|
@@ -27,8 +27,7 @@ import (
|
||||
func TestPageTargetPath(t *testing.T) {
|
||||
pathSpec := newTestPathSpec()
|
||||
|
||||
noExtNoDelimMediaType := media.TextType
|
||||
noExtNoDelimMediaType.Suffixes = []string{}
|
||||
noExtNoDelimMediaType := media.WithDelimiterAndSuffixes(media.TextType, "", "")
|
||||
noExtNoDelimMediaType.Delimiter = ""
|
||||
|
||||
// Netlify style _redirects
|
||||
@@ -209,11 +208,11 @@ func TestPageTargetPath(t *testing.T) {
|
||||
// TODO(bep) simplify
|
||||
if test.d.Kind == KindPage && test.d.BaseName == test.d.Type.BaseName {
|
||||
} else if test.d.Kind == KindHome && test.d.Type.Path != "" {
|
||||
} else if test.d.Type.MediaType.Suffix() != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
|
||||
} else if test.d.Type.MediaType.FirstSuffix.Suffix != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
|
||||
expected.TargetFilename = strings.Replace(expected.TargetFilename,
|
||||
"/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.Suffix(),
|
||||
"."+test.d.Type.MediaType.Suffix(), 1)
|
||||
expected.Link = strings.TrimSuffix(expected.Link, "/") + "." + test.d.Type.MediaType.Suffix()
|
||||
"/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.FirstSuffix.Suffix,
|
||||
"."+test.d.Type.MediaType.FirstSuffix.Suffix, 1)
|
||||
expected.Link = strings.TrimSuffix(expected.Link, "/") + "." + test.d.Type.MediaType.FirstSuffix.Suffix
|
||||
|
||||
}
|
||||
|
||||
|
@@ -31,15 +31,14 @@ func TestCreatePlaceholders(t *testing.T) {
|
||||
})
|
||||
|
||||
c.Assert(m, qt.DeepEquals, map[string]interface{}{
|
||||
"FullSuffix": "pre_foo.FullSuffix_post",
|
||||
"IsZero": "pre_foo.IsZero_post",
|
||||
"MarshalJSON": "pre_foo.MarshalJSON_post",
|
||||
"Suffixes": "pre_foo.Suffixes_post",
|
||||
"Delimiter": "pre_foo.Delimiter_post",
|
||||
"FirstSuffix": "pre_foo.FirstSuffix_post",
|
||||
"String": "pre_foo.String_post",
|
||||
"Type": "pre_foo.Type_post",
|
||||
"MainType": "pre_foo.MainType_post",
|
||||
"Delimiter": "pre_foo.Delimiter_post",
|
||||
"MarshalJSON": "pre_foo.MarshalJSON_post",
|
||||
"String": "pre_foo.String_post",
|
||||
"Suffix": "pre_foo.Suffix_post",
|
||||
"SubType": "pre_foo.SubType_post",
|
||||
"Suffixes": "pre_foo.Suffixes_post",
|
||||
})
|
||||
}
|
||||
|
@@ -268,10 +268,10 @@ func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (reso
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(fd.RelTargetFilename))
|
||||
mimeType, found := r.MediaTypes.GetFirstBySuffix(strings.TrimPrefix(ext, "."))
|
||||
mimeType, suffixInfo, found := r.MediaTypes.GetFirstBySuffix(strings.TrimPrefix(ext, "."))
|
||||
// TODO(bep) we need to handle these ambiguous types better, but in this context
|
||||
// we most likely want the application/xml type.
|
||||
if mimeType.Suffix() == "xml" && mimeType.SubType == "rss" {
|
||||
if suffixInfo.Suffix == "xml" && mimeType.SubType == "rss" {
|
||||
mimeType, found = r.MediaTypes.GetByType("application/xml")
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user