mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-24 21:56:05 +02:00
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package page
|
||||
package page_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/media"
|
||||
"github.com/gohugoio/hugo/resources/page"
|
||||
|
||||
"github.com/gohugoio/hugo/output"
|
||||
)
|
||||
@@ -27,7 +28,7 @@ import (
|
||||
func TestPageTargetPath(t *testing.T) {
|
||||
pathSpec := newTestPathSpec()
|
||||
|
||||
noExtNoDelimMediaType := media.WithDelimiterAndSuffixes(media.TextType, "", "")
|
||||
noExtNoDelimMediaType := media.WithDelimiterAndSuffixes(media.Builtin.TextType, "", "")
|
||||
noExtNoDelimMediaType.Delimiter = ""
|
||||
|
||||
// Netlify style _redirects
|
||||
@@ -43,152 +44,152 @@ func TestPageTargetPath(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
d TargetPathDescriptor
|
||||
expected TargetPaths
|
||||
d page.TargetPathDescriptor
|
||||
expected page.TargetPaths
|
||||
}{
|
||||
{"JSON home", TargetPathDescriptor{Kind: KindHome, Type: output.JSONFormat}, TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}},
|
||||
{"AMP home", TargetPathDescriptor{Kind: KindHome, Type: output.AMPFormat}, TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}},
|
||||
{"HTML home", TargetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: output.HTMLFormat}, TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}},
|
||||
{"Netlify redirects", TargetPathDescriptor{Kind: KindHome, BaseName: "_index", Type: noExtDelimFormat}, TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}},
|
||||
{"HTML section list", TargetPathDescriptor{
|
||||
Kind: KindSection,
|
||||
{"JSON home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.JSONFormat}, page.TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}},
|
||||
{"AMP home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.AMPFormat}, page.TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}},
|
||||
{"HTML home", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: output.HTMLFormat}, page.TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}},
|
||||
{"Netlify redirects", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: noExtDelimFormat}, page.TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}},
|
||||
{"HTML section list", page.TargetPathDescriptor{
|
||||
Kind: page.KindSection,
|
||||
Sections: []string{"sect1"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
|
||||
{"HTML taxonomy term", TargetPathDescriptor{
|
||||
Kind: KindTerm,
|
||||
}, page.TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
|
||||
{"HTML taxonomy term", page.TargetPathDescriptor{
|
||||
Kind: page.KindTerm,
|
||||
Sections: []string{"tags", "hugo"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
|
||||
{"HTML taxonomy", TargetPathDescriptor{
|
||||
Kind: KindTaxonomy,
|
||||
}, page.TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
|
||||
{"HTML taxonomy", page.TargetPathDescriptor{
|
||||
Kind: page.KindTaxonomy,
|
||||
Sections: []string{"tags"},
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
|
||||
}, page.TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
|
||||
{
|
||||
"HTML page", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
Sections: []string{"a"},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/mypage/index.html", SubResourceBaseTarget: "/a/b/mypage", Link: "/a/b/mypage/"},
|
||||
}, page.TargetPaths{TargetFilename: "/a/b/mypage/index.html", SubResourceBaseTarget: "/a/b/mypage", Link: "/a/b/mypage/"},
|
||||
},
|
||||
|
||||
{
|
||||
"HTML page with index as base", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page with index as base", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "index",
|
||||
Sections: []string{"a"},
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/index.html", SubResourceBaseTarget: "/a/b", Link: "/a/b/"},
|
||||
}, page.TargetPaths{TargetFilename: "/a/b/index.html", SubResourceBaseTarget: "/a/b", Link: "/a/b/"},
|
||||
},
|
||||
|
||||
{
|
||||
"HTML page with special chars", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page with special chars", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "My Page!",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/my-page/index.html", SubResourceBaseTarget: "/a/b/my-page", Link: "/a/b/my-page/"},
|
||||
}, page.TargetPaths{TargetFilename: "/a/b/my-page/index.html", SubResourceBaseTarget: "/a/b/my-page", Link: "/a/b/my-page/"},
|
||||
},
|
||||
{"RSS home", TargetPathDescriptor{Kind: "rss", Type: output.RSSFormat}, TargetPaths{TargetFilename: "/index.xml", SubResourceBaseTarget: "", Link: "/index.xml"}},
|
||||
{"RSS section list", TargetPathDescriptor{
|
||||
{"RSS home", page.TargetPathDescriptor{Kind: "rss", Type: output.RSSFormat}, page.TargetPaths{TargetFilename: "/index.xml", SubResourceBaseTarget: "", Link: "/index.xml"}},
|
||||
{"RSS section list", page.TargetPathDescriptor{
|
||||
Kind: "rss",
|
||||
Sections: []string{"sect1"},
|
||||
Type: output.RSSFormat,
|
||||
}, TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
|
||||
}, page.TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
|
||||
{
|
||||
"AMP page", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"AMP page", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b/c",
|
||||
BaseName: "myamp",
|
||||
Type: output.AMPFormat,
|
||||
}, TargetPaths{TargetFilename: "/amp/a/b/c/myamp/index.html", SubResourceBaseTarget: "/amp/a/b/c/myamp", Link: "/amp/a/b/c/myamp/"},
|
||||
}, page.TargetPaths{TargetFilename: "/amp/a/b/c/myamp/index.html", SubResourceBaseTarget: "/amp/a/b/c/myamp", Link: "/amp/a/b/c/myamp/"},
|
||||
},
|
||||
{
|
||||
"AMP page with URL with suffix", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"AMP page with URL with suffix", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/url.xhtml",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/url.xhtml", SubResourceBaseTarget: "/some/other", Link: "/some/other/url.xhtml"},
|
||||
}, page.TargetPaths{TargetFilename: "/some/other/url.xhtml", SubResourceBaseTarget: "/some/other", Link: "/some/other/url.xhtml"},
|
||||
},
|
||||
{
|
||||
"JSON page with URL without suffix", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"JSON page with URL without suffix", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path/",
|
||||
Type: output.JSONFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
}, page.TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
},
|
||||
{
|
||||
"JSON page with URL without suffix and no trailing slash", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"JSON page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path",
|
||||
Type: output.JSONFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
}, page.TargetPaths{TargetFilename: "/some/other/path/index.json", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/index.json"},
|
||||
},
|
||||
{
|
||||
"HTML page with URL without suffix and no trailing slash", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other/path",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"},
|
||||
}, page.TargetPaths{TargetFilename: "/some/other/path/index.html", SubResourceBaseTarget: "/some/other/path", Link: "/some/other/path/"},
|
||||
},
|
||||
{
|
||||
"HTML page with URL containing double hyphen", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page with URL containing double hyphen", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/sect/",
|
||||
BaseName: "mypage",
|
||||
URL: "/some/other--url/",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/some/other--url/index.html", SubResourceBaseTarget: "/some/other--url", Link: "/some/other--url/"},
|
||||
}, page.TargetPaths{TargetFilename: "/some/other--url/index.html", SubResourceBaseTarget: "/some/other--url", Link: "/some/other--url/"},
|
||||
},
|
||||
{
|
||||
"HTML page with expanded permalink", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"HTML page with expanded permalink", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
ExpandedPermalink: "/2017/10/my-title/",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/2017/10/my-title/index.html", SubResourceBaseTarget: "/2017/10/my-title", Link: "/2017/10/my-title/"},
|
||||
}, page.TargetPaths{TargetFilename: "/2017/10/my-title/index.html", SubResourceBaseTarget: "/2017/10/my-title", Link: "/2017/10/my-title/"},
|
||||
},
|
||||
{
|
||||
"Paginated HTML home", TargetPathDescriptor{
|
||||
Kind: KindHome,
|
||||
"Paginated HTML home", page.TargetPathDescriptor{
|
||||
Kind: page.KindHome,
|
||||
BaseName: "_index",
|
||||
Type: output.HTMLFormat,
|
||||
Addends: "page/3",
|
||||
}, TargetPaths{TargetFilename: "/page/3/index.html", SubResourceBaseTarget: "/page/3", Link: "/page/3/"},
|
||||
}, page.TargetPaths{TargetFilename: "/page/3/index.html", SubResourceBaseTarget: "/page/3", Link: "/page/3/"},
|
||||
},
|
||||
{
|
||||
"Paginated Taxonomy terms list", TargetPathDescriptor{
|
||||
Kind: KindTerm,
|
||||
"Paginated Taxonomy terms list", page.TargetPathDescriptor{
|
||||
Kind: page.KindTerm,
|
||||
BaseName: "_index",
|
||||
Sections: []string{"tags", "hugo"},
|
||||
Type: output.HTMLFormat,
|
||||
Addends: "page/3",
|
||||
}, TargetPaths{TargetFilename: "/tags/hugo/page/3/index.html", SubResourceBaseTarget: "/tags/hugo/page/3", Link: "/tags/hugo/page/3/"},
|
||||
}, page.TargetPaths{TargetFilename: "/tags/hugo/page/3/index.html", SubResourceBaseTarget: "/tags/hugo/page/3", Link: "/tags/hugo/page/3/"},
|
||||
},
|
||||
{
|
||||
"Regular page with addend", TargetPathDescriptor{
|
||||
Kind: KindPage,
|
||||
"Regular page with addend", page.TargetPathDescriptor{
|
||||
Kind: page.KindPage,
|
||||
Dir: "/a/b",
|
||||
BaseName: "mypage",
|
||||
Addends: "c/d/e",
|
||||
Type: output.HTMLFormat,
|
||||
}, TargetPaths{TargetFilename: "/a/b/mypage/c/d/e/index.html", SubResourceBaseTarget: "/a/b/mypage/c/d/e", Link: "/a/b/mypage/c/d/e/"},
|
||||
}, page.TargetPaths{TargetFilename: "/a/b/mypage/c/d/e/index.html", SubResourceBaseTarget: "/a/b/mypage/c/d/e", Link: "/a/b/mypage/c/d/e/"},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -206,8 +207,8 @@ func TestPageTargetPath(t *testing.T) {
|
||||
expected := test.expected
|
||||
|
||||
// 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 != "" {
|
||||
if test.d.Kind == page.KindPage && test.d.BaseName == test.d.Type.BaseName {
|
||||
} else if test.d.Kind == page.KindHome && test.d.Type.Path != "" {
|
||||
} 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.FirstSuffix.Suffix,
|
||||
@@ -228,7 +229,7 @@ func TestPageTargetPath(t *testing.T) {
|
||||
expected.TargetFilename = filepath.FromSlash(expected.TargetFilename)
|
||||
expected.SubResourceBaseTarget = filepath.FromSlash(expected.SubResourceBaseTarget)
|
||||
|
||||
pagePath := CreateTargetPaths(test.d)
|
||||
pagePath := page.CreateTargetPaths(test.d)
|
||||
|
||||
if !eqTargetPaths(pagePath, expected) {
|
||||
t.Fatalf("[%d] [%s] targetPath expected\n%#v, got:\n%#v", i, test.name, expected, pagePath)
|
||||
@@ -244,18 +245,18 @@ func TestPageTargetPathPrefix(t *testing.T) {
|
||||
pathSpec := newTestPathSpec()
|
||||
tests := []struct {
|
||||
name string
|
||||
d TargetPathDescriptor
|
||||
expected TargetPaths
|
||||
d page.TargetPathDescriptor
|
||||
expected page.TargetPaths
|
||||
}{
|
||||
{
|
||||
"URL set, prefix both, no force",
|
||||
TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"},
|
||||
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
page.TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"},
|
||||
},
|
||||
{
|
||||
"URL set, prefix both, force",
|
||||
TargetPathDescriptor{Kind: KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"},
|
||||
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
|
||||
page.TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -267,7 +268,7 @@ func TestPageTargetPathPrefix(t *testing.T) {
|
||||
expected.TargetFilename = filepath.FromSlash(expected.TargetFilename)
|
||||
expected.SubResourceBaseTarget = filepath.FromSlash(expected.SubResourceBaseTarget)
|
||||
|
||||
pagePath := CreateTargetPaths(test.d)
|
||||
pagePath := page.CreateTargetPaths(test.d)
|
||||
|
||||
if pagePath != expected {
|
||||
t.Fatalf("[%d] [%s] targetPath expected\n%#v, got:\n%#v", i, test.name, expected, pagePath)
|
||||
@@ -276,7 +277,7 @@ func TestPageTargetPathPrefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func eqTargetPaths(p1, p2 TargetPaths) bool {
|
||||
func eqTargetPaths(p1, p2 page.TargetPaths) bool {
|
||||
if p1.Link != p2.Link {
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user