mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
@@ -309,36 +309,6 @@ func (formats Formats) GetByNames(names ...string) (Formats, error) {
|
||||
return types, nil
|
||||
}
|
||||
|
||||
// FromFilename gets a Format given a filename.
|
||||
func (formats Formats) FromFilename(filename string) (f Format, found bool) {
|
||||
// mytemplate.amp.html
|
||||
// mytemplate.html
|
||||
// mytemplate
|
||||
var ext, outFormat string
|
||||
|
||||
parts := strings.Split(filename, ".")
|
||||
if len(parts) > 2 {
|
||||
outFormat = parts[1]
|
||||
ext = parts[2]
|
||||
} else if len(parts) > 1 {
|
||||
ext = parts[1]
|
||||
}
|
||||
|
||||
if outFormat != "" {
|
||||
return formats.GetByName(outFormat)
|
||||
}
|
||||
|
||||
if ext != "" {
|
||||
f, found = formats.GetBySuffix(ext)
|
||||
if !found && len(parts) == 2 {
|
||||
// For extensionless output formats (e.g. Netlify's _redirects)
|
||||
// we must fall back to using the extension as format lookup.
|
||||
f, found = formats.GetByName(ext)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// BaseFilename returns the base filename of f including an extension (ie.
|
||||
// "index.xml").
|
||||
func (f Format) BaseFilename() string {
|
||||
|
||||
@@ -98,45 +98,6 @@ func TestGetFormatByExt(t *testing.T) {
|
||||
c.Assert(found, qt.Equals, false)
|
||||
}
|
||||
|
||||
func TestGetFormatByFilename(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
noExtNoDelimMediaType := media.Builtin.TextType
|
||||
noExtNoDelimMediaType.Delimiter = ""
|
||||
|
||||
noExtMediaType := media.Builtin.TextType
|
||||
|
||||
var (
|
||||
noExtDelimFormat = Format{
|
||||
Name: "NEM",
|
||||
MediaType: noExtNoDelimMediaType,
|
||||
BaseName: "_redirects",
|
||||
}
|
||||
noExt = Format{
|
||||
Name: "NEX",
|
||||
MediaType: noExtMediaType,
|
||||
BaseName: "next",
|
||||
}
|
||||
)
|
||||
|
||||
formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
|
||||
f, found := formats.FromFilename("my.amp.html")
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(f, qt.Equals, AMPFormat)
|
||||
_, found = formats.FromFilename("my.ics")
|
||||
c.Assert(found, qt.Equals, true)
|
||||
f, found = formats.FromFilename("my.html")
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(f, qt.Equals, HTMLFormat)
|
||||
f, found = formats.FromFilename("my.nem")
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(f, qt.Equals, noExtDelimFormat)
|
||||
f, found = formats.FromFilename("my.nex")
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(f, qt.Equals, noExt)
|
||||
_, found = formats.FromFilename("my.css")
|
||||
c.Assert(found, qt.Equals, false)
|
||||
}
|
||||
|
||||
func TestSort(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
c.Assert(DefaultFormats[0].Name, qt.Equals, "html")
|
||||
|
||||
Reference in New Issue
Block a user