summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-07-10 11:55:22 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-07-10 22:13:52 +0200
commitb874a1ba7ab8394dc741c8c70303a30a35b63e43 (patch)
tree756a5869cf623ace8387fcf6166a831c052f0ae7 /hugolib
parent4108705934846f2b7cae2602ce14aeee17139608 (diff)
media: Allow multiple file suffixes per media type
Before this commit, `Suffix` on `MediaType` was used both to set a custom file suffix and as a way to augment the mediatype definition (what you see after the "+", e.g. "image/svg+xml"). This had its limitations. For one, it was only possible with one file extension per MIME type. Now you can specify multiple file suffixes using "suffixes", but you need to specify the full MIME type identifier: [mediaTypes] [mediaTypes."image/svg+xml"] suffixes = ["svg", "abc ] In most cases, it will be enough to just change: [mediaTypes] [mediaTypes."my/custom-mediatype"] suffix = "txt" To: [mediaTypes] [mediaTypes."my/custom-mediatype"] suffixes = ["txt"] Hugo will still respect values set in "suffix" if no value for "suffixes" is provided, but this will be removed in a future release. Note that you can still get the Media Type's suffix from a template: {{ $mediaType.Suffix }}. But this will now map to the MIME type filename. Fixes #4920
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/config_test.go10
-rw-r--r--hugolib/page_output.go2
-rw-r--r--hugolib/page_paths.go2
-rw-r--r--hugolib/page_paths_test.go6
-rw-r--r--hugolib/pagination_test.go2
-rw-r--r--hugolib/shortcode.go4
-rw-r--r--hugolib/site.go5
-rw-r--r--hugolib/site_output_test.go4
-rw-r--r--hugolib/site_render.go4
9 files changed, 23 insertions, 16 deletions
diff --git a/hugolib/config_test.go b/hugolib/config_test.go
index aec673369..2fa26d4f3 100644
--- a/hugolib/config_test.go
+++ b/hugolib/config_test.go
@@ -219,8 +219,11 @@ map[string]interface {}{
"mediatype": Type{
MainType: "text",
SubType: "m1",
- Suffix: "m1main",
+ OldSuffix: "m1main",
Delimiter: ".",
+ Suffixes: []string{
+ "m1main",
+ },
},
},
"o2": map[string]interface {}{
@@ -228,8 +231,11 @@ map[string]interface {}{
"mediatype": Type{
MainType: "text",
SubType: "m2",
- Suffix: "m2theme",
+ OldSuffix: "m2theme",
Delimiter: ".",
+ Suffixes: []string{
+ "m2theme",
+ },
},
},
}`, got["outputformats"])
diff --git a/hugolib/page_output.go b/hugolib/page_output.go
index 6fffbae86..204f5acee 100644
--- a/hugolib/page_output.go
+++ b/hugolib/page_output.go
@@ -218,7 +218,7 @@ func newOutputFormat(p *Page, f output.Format) *OutputFormat {
func (p *PageOutput) AlternativeOutputFormats() (OutputFormats, error) {
var o OutputFormats
for _, of := range p.OutputFormats() {
- if of.f.NotAlternative || of.f == p.outputFormat {
+ if of.f.NotAlternative || of.f.Name == p.outputFormat.Name {
continue
}
o = append(o, of)
diff --git a/hugolib/page_paths.go b/hugolib/page_paths.go
index 1b2d00ad5..550df130d 100644
--- a/hugolib/page_paths.go
+++ b/hugolib/page_paths.go
@@ -239,7 +239,7 @@ func createTargetPath(d targetPathDescriptor) string {
}
if isUgly {
- pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
+ pagePath += d.Type.MediaType.FullSuffix()
} else {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
}
diff --git a/hugolib/page_paths_test.go b/hugolib/page_paths_test.go
index 3ca500f17..8f8df6ec1 100644
--- a/hugolib/page_paths_test.go
+++ b/hugolib/page_paths_test.go
@@ -30,7 +30,7 @@ func TestPageTargetPath(t *testing.T) {
pathSpec := newTestDefaultPathSpec(t)
noExtNoDelimMediaType := media.TextType
- noExtNoDelimMediaType.Suffix = ""
+ noExtNoDelimMediaType.Suffixes = []string{}
noExtNoDelimMediaType.Delimiter = ""
// Netlify style _redirects
@@ -169,8 +169,8 @@ func TestPageTargetPath(t *testing.T) {
} else if test.d.Kind == KindHome && test.d.Type.Path != "" {
} else if (!strings.HasPrefix(expected, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
expected = strings.Replace(expected,
- "/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.Suffix,
- "."+test.d.Type.MediaType.Suffix, -1)
+ "/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.Suffix(),
+ "."+test.d.Type.MediaType.Suffix(), -1)
}
if test.d.LangPrefix != "" && !(test.d.Kind == KindPage && test.d.URL != "") {
diff --git a/hugolib/pagination_test.go b/hugolib/pagination_test.go
index 94f7301bb..5dbef609b 100644
--- a/hugolib/pagination_test.go
+++ b/hugolib/pagination_test.go
@@ -239,7 +239,7 @@ func TestPaginationURLFactory(t *testing.T) {
}
if uglyURLs {
- expected = expected[:len(expected)-1] + "." + test.d.Type.MediaType.Suffix
+ expected = expected[:len(expected)-1] + "." + test.d.Type.MediaType.Suffix()
}
pathSpec := newTestPathSpec(fs, cfg)
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go
index cf5b0ece0..275293771 100644
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -173,11 +173,11 @@ type scKey struct {
}
func newScKey(m media.Type, shortcodeplaceholder string) scKey {
- return scKey{Suffix: m.Suffix, ShortcodePlaceholder: shortcodeplaceholder}
+ return scKey{Suffix: m.Suffix(), ShortcodePlaceholder: shortcodeplaceholder}
}
func newScKeyFromLangAndOutputFormat(lang string, o output.Format, shortcodeplaceholder string) scKey {
- return scKey{Lang: lang, Suffix: o.MediaType.Suffix, OutputFormat: o.Name, ShortcodePlaceholder: shortcodeplaceholder}
+ return scKey{Lang: lang, Suffix: o.MediaType.Suffix(), OutputFormat: o.Name, ShortcodePlaceholder: shortcodeplaceholder}
}
func newDefaultScKey(shortcodeplaceholder string) scKey {
diff --git a/hugolib/site.go b/hugolib/site.go
index a749bafd0..5e300393b 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -586,8 +586,9 @@ type whatChanged struct {
// package, so it will behave correctly with Hugo's built-in server.
func (s *Site) RegisterMediaTypes() {
for _, mt := range s.mediaTypesConfig {
- // The last one will win if there are any duplicates.
- _ = mime.AddExtensionType("."+mt.Suffix, mt.Type()+"; charset=utf-8")
+ for _, suffix := range mt.Suffixes {
+ _ = mime.AddExtensionType(mt.Delimiter+suffix, mt.Type()+"; charset=utf-8")
+ }
}
}
diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go
index 5f864538e..0677dfbfb 100644
--- a/hugolib/site_output_test.go
+++ b/hugolib/site_output_test.go
@@ -179,7 +179,7 @@ Len Pages: {{ .Kind }} {{ len .Site.RegularPages }} Page Number: {{ .Paginator.P
th.assertFileContent("public/index.html",
// The HTML entity is a deliberate part of this test: The HTML templates are
// parsed with html/template.
- `List HTML|JSON Home|<atom:link href=http://example.com/blog/ rel="self" type="text/html&#43;html" />`,
+ `List HTML|JSON Home|<atom:link href=http://example.com/blog/ rel="self" type="text/html" />`,
"en: Elbow",
"ShortHTML",
"OtherShort: <h1>Hi!</h1>",
@@ -195,7 +195,7 @@ Len Pages: {{ .Kind }} {{ len .Site.RegularPages }} Page Number: {{ .Paginator.P
th.assertFileContent("public/index.json",
"Output/Rel: JSON/canonical|",
// JSON is plain text, so no need to safeHTML this and that
- `<atom:link href=http://example.com/blog/index.json rel="self" type="application/json+json" />`,
+ `<atom:link href=http://example.com/blog/index.json rel="self" type="application/json" />`,
"ShortJSON",
"OtherShort: <h1>Hi!</h1>",
)
diff --git a/hugolib/site_render.go b/hugolib/site_render.go
index d6b7a76fc..2da4064b4 100644
--- a/hugolib/site_render.go
+++ b/hugolib/site_render.go
@@ -69,7 +69,7 @@ func headlessPagesPublisher(s *Site, wg *sync.WaitGroup) {
defer wg.Done()
for _, page := range s.headlessPages {
outFormat := page.outputFormats[0] // There is only one
- if outFormat != s.rc.Format {
+ if outFormat.Name != s.rc.Format.Name {
// Avoid double work.
continue
}
@@ -92,7 +92,7 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa
for i, outFormat := range page.outputFormats {
- if outFormat != page.s.rc.Format {
+ if outFormat.Name != page.s.rc.Format.Name {
// Will be rendered ... later.
continue
}