From b3cb6788b2634a89ae774895f345f082020b52d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 28 Jul 2023 10:53:47 +0200 Subject: [PATCH] Move all Kind constants to its own package See #11256 --- config/allconfig/allconfig.go | 11 +- hugolib/content_map.go | 7 +- hugolib/content_map_page.go | 12 +- hugolib/disableKinds_test.go | 19 ++-- hugolib/hugo_sites.go | 7 +- hugolib/hugo_sites_build_test.go | 38 +++---- hugolib/hugo_sites_multihost_test.go | 12 +- hugolib/language_content_dir_test.go | 5 +- hugolib/page.go | 21 ++-- hugolib/page__data.go | 7 +- hugolib/page__meta.go | 17 +-- hugolib/page__paginator.go | 5 +- hugolib/page__paths.go | 3 +- hugolib/page__tree.go | 3 +- hugolib/page_kinds.go | 34 ------ hugolib/pagebundler_test.go | 37 +++--- hugolib/pagecollections.go | 7 +- hugolib/pagecollections_test.go | 85 +++++++------- hugolib/shortcode_test.go | 4 +- hugolib/site.go | 18 ++- hugolib/site_new.go | 7 +- hugolib/site_output.go | 22 ++-- hugolib/site_output_test.go | 40 +++---- hugolib/site_render.go | 11 +- hugolib/site_sections_test.go | 9 +- hugolib/site_test.go | 5 +- hugolib/site_url_test.go | 9 +- hugolib/taxonomy_test.go | 21 ++-- resources/kinds/kinds.go | 107 ++++++++++++++++++ .../kinds_test.go} | 17 +-- resources/page/page_kinds.go | 33 ------ resources/page/page_matcher.go | 3 +- resources/page/page_paths.go | 9 +- resources/page/page_paths_test.go | 49 ++++---- resources/page/permalinks.go | 15 +-- 35 files changed, 386 insertions(+), 323 deletions(-) create mode 100644 resources/kinds/kinds.go rename resources/{page/page_kinds_test.go => kinds/kinds_test.go} (66%) diff --git a/config/allconfig/allconfig.go b/config/allconfig/allconfig.go index ec95f5eba..6d206d7a4 100644 --- a/config/allconfig/allconfig.go +++ b/config/allconfig/allconfig.go @@ -45,6 +45,7 @@ import ( "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/related" "github.com/gohugoio/hugo/resources/images" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page/pagemeta" "github.com/spf13/afero" @@ -940,11 +941,11 @@ func createDefaultOutputFormats(allFormats output.Formats) map[string][]string { } m := map[string][]string{ - page.KindPage: {htmlOut.Name}, - page.KindHome: defaultListTypes, - page.KindSection: defaultListTypes, - page.KindTerm: defaultListTypes, - page.KindTaxonomy: defaultListTypes, + kinds.KindPage: {htmlOut.Name}, + kinds.KindHome: defaultListTypes, + kinds.KindSection: defaultListTypes, + kinds.KindTerm: defaultListTypes, + kinds.KindTaxonomy: defaultListTypes, } // May be disabled diff --git a/hugolib/content_map.go b/hugolib/content_map.go index 9abf0d5b0..8cb307691 100644 --- a/hugolib/content_map.go +++ b/hugolib/content_map.go @@ -22,6 +22,7 @@ import ( "github.com/gohugoio/hugo/helpers" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/hugofs/files" @@ -275,13 +276,13 @@ type contentBundleViewInfo struct { func (c *contentBundleViewInfo) kind() string { if c.termKey != "" { - return page.KindTerm + return kinds.KindTerm } - return page.KindTaxonomy + return kinds.KindTaxonomy } func (c *contentBundleViewInfo) sections() []string { - if c.kind() == page.KindTaxonomy { + if c.kind() == kinds.KindTaxonomy { return []string{c.name.plural} } diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go index 6f02e2e29..e7f6c5aed 100644 --- a/hugolib/content_map_page.go +++ b/hugolib/content_map_page.go @@ -19,6 +19,7 @@ import ( "path" "path/filepath" "strings" + "sync" "github.com/gohugoio/hugo/common/maps" @@ -30,6 +31,7 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/parser/pageparser" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/resource" "github.com/spf13/cast" @@ -105,7 +107,7 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB sections := s.sectionsFromFile(f) kind := s.kindFromFileInfoOrSections(f, sections) - if kind == page.KindTerm { + if kind == kinds.KindTerm { s.PathSpec.MakePathsSanitized(sections) } @@ -366,7 +368,7 @@ func (m *pageMap) assemblePages() error { return true } - shouldBuild = !(n.p.Kind() == page.KindPage && m.cfg.pageDisabled) && m.s.shouldBuild(n.p) + shouldBuild = !(n.p.Kind() == kinds.KindPage && m.cfg.pageDisabled) && m.s.shouldBuild(n.p) if !shouldBuild { m.deletePage(s) return false @@ -469,9 +471,9 @@ func (m *pageMap) assembleSections() error { parentBucket = m.s.siteBucket } - kind := page.KindSection + kind := kinds.KindSection if s == "/" { - kind = page.KindHome + kind = kinds.KindHome } if n.fi != nil { @@ -537,7 +539,7 @@ func (m *pageMap) assembleTaxonomies() error { } } else { title := "" - if kind == page.KindTerm { + if kind == kinds.KindTerm { title = n.viewInfo.term() } n.p = m.s.newPage(n, parent.p.bucket, kind, title, sections...) diff --git a/hugolib/disableKinds_test.go b/hugolib/disableKinds_test.go index 87a60d636..c24f6b10e 100644 --- a/hugolib/disableKinds_test.go +++ b/hugolib/disableKinds_test.go @@ -17,6 +17,7 @@ import ( "testing" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -134,7 +135,7 @@ title: Headless Local Lists Sub return nil } - disableKind := page.KindPage + disableKind := kinds.KindPage c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -149,7 +150,7 @@ title: Headless Local Lists Sub b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 0) }) - disableKind = page.KindTerm + disableKind = kinds.KindTerm c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -161,7 +162,7 @@ title: Headless Local Lists Sub b.Assert(getPage(b, "/categories/mycat"), qt.IsNil) }) - disableKind = page.KindTaxonomy + disableKind = kinds.KindTaxonomy c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -177,7 +178,7 @@ title: Headless Local Lists Sub b.Assert(getPageInPagePages(getPage(b, "/"), "/categories"), qt.IsNil) }) - disableKind = page.KindHome + disableKind = kinds.KindHome c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -190,7 +191,7 @@ title: Headless Local Lists Sub b.Assert(getPage(b, "/sect/page.md"), qt.Not(qt.IsNil)) }) - disableKind = page.KindSection + disableKind = kinds.KindSection c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -210,7 +211,7 @@ title: Headless Local Lists Sub b.AssertFileContent("public/index.xml", "rss") }) - disableKind = kindRSS + disableKind = kinds.KindRSS c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) @@ -219,21 +220,21 @@ title: Headless Local Lists Sub b.Assert(home.OutputFormats(), qt.HasLen, 1) }) - disableKind = kindSitemap + disableKind = kinds.KindSitemap c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) b.Assert(b.CheckExists("public/sitemap.xml"), qt.Equals, false) }) - disableKind = kind404 + disableKind = kinds.Kind404 c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.Build(BuildCfg{}) b.Assert(b.CheckExists("public/404.html"), qt.Equals, false) }) - disableKind = kindRobotsTXT + disableKind = kinds.KindRobotsTXT c.Run("Disable "+disableKind, func(c *qt.C) { b := newSitesBuilder(c, disableKind) b.WithTemplatesAdded("robots.txt", "myrobots") diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 3b5efa2e5..eb0385cda 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -47,6 +47,7 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/lazy" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page/pagemeta" "github.com/gohugoio/hugo/tpl" @@ -443,7 +444,7 @@ func (h *HugoSites) renderCrossSitesSitemap() error { sitemapEnabled := false for _, s := range h.Sites { - if s.conf.IsKindEnabled(kindSitemap) { + if s.conf.IsKindEnabled(kinds.KindSitemap) { sitemapEnabled = true break } @@ -474,7 +475,7 @@ func (h *HugoSites) renderCrossSitesRobotsTXT() error { p, err := newPageStandalone(&pageMeta{ s: s, - kind: kindRobotsTXT, + kind: kinds.KindRobotsTXT, urlPaths: pagemeta.URLPath{ URL: "robots.txt", }, @@ -523,7 +524,7 @@ func (h *HugoSites) createPageCollections() error { }) allRegularPages := newLazyPagesFactory(func() page.Pages { - return h.findPagesByKindIn(page.KindPage, allPages.get()) + return h.findPagesByKindIn(kinds.KindPage, allPages.get()) }) for _, s := range h.Sites { diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 3c82a60b6..bdbf4270e 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -10,7 +10,7 @@ import ( qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/htesting" - "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/resources/kinds" "github.com/fortytw2/leaktest" "github.com/fsnotify/fsnotify" @@ -182,12 +182,12 @@ p1 = "p1en" c.Assert(len(sites), qt.Equals, 2) nnSite := sites[0] - nnHome := nnSite.getPage(page.KindHome) + nnHome := nnSite.getPage(kinds.KindHome) c.Assert(len(nnHome.AllTranslations()), qt.Equals, 2) c.Assert(len(nnHome.Translations()), qt.Equals, 1) c.Assert(nnHome.IsTranslated(), qt.Equals, true) - enHome := sites[1].getPage(page.KindHome) + enHome := sites[1].getPage(kinds.KindHome) p1, err := enHome.Param("p1") c.Assert(err, qt.IsNil) @@ -239,7 +239,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { c.Assert(gp2, qt.IsNil) enSite := sites[0] - enSiteHome := enSite.getPage(page.KindHome) + enSiteHome := enSite.getPage(kinds.KindHome) c.Assert(enSiteHome.IsTranslated(), qt.Equals, true) c.Assert(enSite.language.Lang, qt.Equals, "en") @@ -300,10 +300,10 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { // isn't ideal in a multilingual setup. You want a way to get the current language version if available. // Now you can do lookups with translation base name to get that behaviour. // Let us test all the regular page variants: - getPageDoc1En := enSite.getPage(page.KindPage, filepath.ToSlash(doc1en.File().Path())) - getPageDoc1EnBase := enSite.getPage(page.KindPage, "sect/doc1") - getPageDoc1Fr := frSite.getPage(page.KindPage, filepath.ToSlash(doc1fr.File().Path())) - getPageDoc1FrBase := frSite.getPage(page.KindPage, "sect/doc1") + getPageDoc1En := enSite.getPage(kinds.KindPage, filepath.ToSlash(doc1en.File().Path())) + getPageDoc1EnBase := enSite.getPage(kinds.KindPage, "sect/doc1") + getPageDoc1Fr := frSite.getPage(kinds.KindPage, filepath.ToSlash(doc1fr.File().Path())) + getPageDoc1FrBase := frSite.getPage(kinds.KindPage, "sect/doc1") c.Assert(getPageDoc1En, qt.Equals, doc1en) c.Assert(getPageDoc1Fr, qt.Equals, doc1fr) c.Assert(getPageDoc1EnBase, qt.Equals, doc1en) @@ -321,7 +321,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello", "LingoDefault") // Check node translations - homeEn := enSite.getPage(page.KindHome) + homeEn := enSite.getPage(kinds.KindHome) c.Assert(homeEn, qt.Not(qt.IsNil)) c.Assert(len(homeEn.Translations()), qt.Equals, 3) c.Assert(homeEn.Translations()[0].Language().Lang, qt.Equals, "fr") @@ -331,7 +331,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { c.Assert(homeEn.Translations()[2].Title(), qt.Equals, "På bokmål") c.Assert(homeEn.Translations()[2].Language().LanguageName, qt.Equals, "Bokmål") - sectFr := frSite.getPage(page.KindSection, "sect") + sectFr := frSite.getPage(kinds.KindSection, "sect") c.Assert(sectFr, qt.Not(qt.IsNil)) c.Assert(sectFr.Language().Lang, qt.Equals, "fr") @@ -341,14 +341,14 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { nnSite := sites[2] c.Assert(nnSite.language.Lang, qt.Equals, "nn") - taxNn := nnSite.getPage(page.KindTaxonomy, "lag") + taxNn := nnSite.getPage(kinds.KindTaxonomy, "lag") c.Assert(taxNn, qt.Not(qt.IsNil)) c.Assert(len(taxNn.Translations()), qt.Equals, 1) c.Assert(taxNn.Translations()[0].Language().Lang, qt.Equals, "nb") - taxTermNn := nnSite.getPage(page.KindTerm, "lag", "sogndal") + taxTermNn := nnSite.getPage(kinds.KindTerm, "lag", "sogndal") c.Assert(taxTermNn, qt.Not(qt.IsNil)) - c.Assert(nnSite.getPage(page.KindTerm, "LAG", "SOGNDAL"), qt.Equals, taxTermNn) + c.Assert(nnSite.getPage(kinds.KindTerm, "LAG", "SOGNDAL"), qt.Equals, taxTermNn) c.Assert(len(taxTermNn.Translations()), qt.Equals, 1) c.Assert(taxTermNn.Translations()[0].Language().Lang, qt.Equals, "nb") @@ -379,19 +379,19 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { // Issue #3108 prevPage := enSite.RegularPages()[0].Prev() c.Assert(prevPage, qt.Not(qt.IsNil)) - c.Assert(prevPage.Kind(), qt.Equals, page.KindPage) + c.Assert(prevPage.Kind(), qt.Equals, kinds.KindPage) for { if prevPage == nil { break } - c.Assert(prevPage.Kind(), qt.Equals, page.KindPage) + c.Assert(prevPage.Kind(), qt.Equals, kinds.KindPage) prevPage = prevPage.Prev() } // Check bundles b.AssertFileContent("public/fr/bundles/b1/index.html", "RelPermalink: /blog/fr/bundles/b1/|") - bundleFr := frSite.getPage(page.KindPage, "bundles/b1/index.md") + bundleFr := frSite.getPage(kinds.KindPage, "bundles/b1/index.md") c.Assert(bundleFr, qt.Not(qt.IsNil)) c.Assert(len(bundleFr.Resources()), qt.Equals, 1) logoFr := bundleFr.Resources().GetMatch("logo*") @@ -401,7 +401,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { b.AssertFileContent("public/fr/bundles/b1/index.html", "Resources: image/png: /blog/fr/bundles/b1/logo.png") b.AssertFileContent("public/fr/bundles/b1/logo.png", "PNG Data") - bundleEn := enSite.getPage(page.KindPage, "bundles/b1/index.en.md") + bundleEn := enSite.getPage(kinds.KindPage, "bundles/b1/index.en.md") c.Assert(bundleEn, qt.Not(qt.IsNil)) b.AssertFileContent("public/en/bundles/b1/index.html", "RelPermalink: /blog/en/bundles/b1/|") c.Assert(len(bundleEn.Resources()), qt.Equals, 1) @@ -441,7 +441,7 @@ func TestMultiSitesRebuild(t *testing.T) { b.AssertFileContent("public/fr/sect/doc1/index.html", "Single", "Shortcode: Bonjour") b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello") - homeEn := enSite.getPage(page.KindHome) + homeEn := enSite.getPage(kinds.KindHome) c.Assert(homeEn, qt.Not(qt.IsNil)) c.Assert(len(homeEn.Translations()), qt.Equals, 3) @@ -558,7 +558,7 @@ func TestMultiSitesRebuild(t *testing.T) { docFr := readWorkingDir(t, fs, "public/fr/sect/doc1/index.html") c.Assert(strings.Contains(docFr, "Salut"), qt.Equals, true) - homeEn := enSite.getPage(page.KindHome) + homeEn := enSite.getPage(kinds.KindHome) c.Assert(homeEn, qt.Not(qt.IsNil)) c.Assert(len(homeEn.Translations()), qt.Equals, 3) c.Assert(homeEn.Translations()[0].Language().Lang, qt.Equals, "fr") diff --git a/hugolib/hugo_sites_multihost_test.go b/hugolib/hugo_sites_multihost_test.go index 2aba5b593..a37310987 100644 --- a/hugolib/hugo_sites_multihost_test.go +++ b/hugolib/hugo_sites_multihost_test.go @@ -3,7 +3,7 @@ package hugolib import ( "testing" - "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/resources/kinds" qt "github.com/frankban/quicktest" ) @@ -58,7 +58,7 @@ languageName = "Nynorsk" s1 := b.H.Sites[0] - s1h := s1.getPage(page.KindHome) + s1h := s1.getPage(kinds.KindHome) c.Assert(s1h.IsTranslated(), qt.Equals, true) c.Assert(len(s1h.Translations()), qt.Equals, 2) c.Assert(s1h.Permalink(), qt.Equals, "https://example.com/docs/") @@ -69,7 +69,7 @@ languageName = "Nynorsk" // For multihost, we never want any content in the root. // // check url in front matter: - pageWithURLInFrontMatter := s1.getPage(page.KindPage, "sect/doc3.en.md") + pageWithURLInFrontMatter := s1.getPage(kinds.KindPage, "sect/doc3.en.md") c.Assert(pageWithURLInFrontMatter, qt.Not(qt.IsNil)) c.Assert(pageWithURLInFrontMatter.RelPermalink(), qt.Equals, "/docs/superbob/") b.AssertFileContent("public/en/superbob/index.html", "doc3|Hello|en") @@ -86,7 +86,7 @@ languageName = "Nynorsk" s2 := b.H.Sites[1] - s2h := s2.getPage(page.KindHome) + s2h := s2.getPage(kinds.KindHome) c.Assert(s2h.Permalink(), qt.Equals, "https://example.fr/") // See https://github.com/gohugoio/hugo/issues/10912 @@ -104,7 +104,7 @@ languageName = "Nynorsk" // Check bundles - bundleEn := s1.getPage(page.KindPage, "bundles/b1/index.en.md") + bundleEn := s1.getPage(kinds.KindPage, "bundles/b1/index.en.md") c.Assert(bundleEn, qt.Not(qt.IsNil)) c.Assert(bundleEn.RelPermalink(), qt.Equals, "/docs/bundles/b1/") c.Assert(len(bundleEn.Resources()), qt.Equals, 1) @@ -112,7 +112,7 @@ languageName = "Nynorsk" b.AssertFileContent("public/en/bundles/b1/logo.png", "PNG Data") b.AssertFileContent("public/en/bundles/b1/index.html", " image/png: /docs/bundles/b1/logo.png") - bundleFr := s2.getPage(page.KindPage, "bundles/b1/index.md") + bundleFr := s2.getPage(kinds.KindPage, "bundles/b1/index.md") c.Assert(bundleFr, qt.Not(qt.IsNil)) c.Assert(bundleFr.RelPermalink(), qt.Equals, "/bundles/b1/") c.Assert(len(bundleFr.Resources()), qt.Equals, 1) diff --git a/hugolib/language_content_dir_test.go b/hugolib/language_content_dir_test.go index 05e207bbc..a22201475 100644 --- a/hugolib/language_content_dir_test.go +++ b/hugolib/language_content_dir_test.go @@ -20,10 +20,9 @@ import ( "path/filepath" "testing" + "github.com/gohugoio/hugo/resources/kinds" "github.com/spf13/cast" - "github.com/gohugoio/hugo/resources/page" - qt "github.com/frankban/quicktest" ) @@ -312,7 +311,7 @@ Content. b.AssertFileContent("public/sv/sect/mybundle/logo.png", "PNG Data") b.AssertFileContent("public/nn/sect/mybundle/logo.png", "PNG Data") - nnSect := nnSite.getPage(page.KindSection, "sect") + nnSect := nnSite.getPage(kinds.KindSection, "sect") c.Assert(nnSect, qt.Not(qt.IsNil)) c.Assert(len(nnSect.Pages()), qt.Equals, 12) nnHome := nnSite.Home() diff --git a/hugolib/page.go b/hugolib/page.go index 2644c8e4e..f8f966156 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -49,6 +49,7 @@ import ( "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/resources" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/resource" ) @@ -249,7 +250,7 @@ func (p *pageState) RegularPagesRecursive() page.Pages { p.regularPagesRecursiveInit.Do(func() { var pages page.Pages switch p.Kind() { - case page.KindSection: + case kinds.KindSection: pages = p.getPagesRecursive() default: pages = p.RegularPages() @@ -268,10 +269,10 @@ func (p *pageState) RegularPages() page.Pages { var pages page.Pages switch p.Kind() { - case page.KindPage: - case page.KindSection, page.KindHome, page.KindTaxonomy: + case kinds.KindPage: + case kinds.KindSection, kinds.KindHome, kinds.KindTaxonomy: pages = p.getPages() - case page.KindTerm: + case kinds.KindTerm: all := p.Pages() for _, p := range all { if p.IsPage() { @@ -293,15 +294,15 @@ func (p *pageState) Pages() page.Pages { var pages page.Pages switch p.Kind() { - case page.KindPage: - case page.KindSection, page.KindHome: + case kinds.KindPage: + case kinds.KindSection, kinds.KindHome: pages = p.getPagesAndSections() - case page.KindTerm: + case kinds.KindTerm: b := p.treeRef.n viewInfo := b.viewInfo taxonomy := p.s.Taxonomies()[viewInfo.name.plural].Get(viewInfo.termKey) pages = taxonomy.Pages() - case page.KindTaxonomy: + case kinds.KindTaxonomy: pages = p.bucket.getTaxonomies() default: pages = p.s.Pages() @@ -439,11 +440,11 @@ func (p *pageState) getLayoutDescriptor() layouts.LayoutDescriptor { sections := p.SectionsEntries() switch p.Kind() { - case page.KindSection: + case kinds.KindSection: if len(sections) > 0 { section = sections[0] } - case page.KindTaxonomy, page.KindTerm: + case kinds.KindTaxonomy, kinds.KindTerm: b := p.getTreeRef().n section = b.viewInfo.name.singular default: diff --git a/hugolib/page__data.go b/hugolib/page__data.go index 19b0154ad..ad6ba126e 100644 --- a/hugolib/page__data.go +++ b/hugolib/page__data.go @@ -16,6 +16,7 @@ package hugolib import ( "sync" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -30,12 +31,12 @@ func (p *pageData) Data() any { p.dataInit.Do(func() { p.data = make(page.Data) - if p.Kind() == page.KindPage { + if p.Kind() == kinds.KindPage { return } switch p.Kind() { - case page.KindTerm: + case kinds.KindTerm: b := p.treeRef.n name := b.viewInfo.name termKey := b.viewInfo.termKey @@ -46,7 +47,7 @@ func (p *pageData) Data() any { p.data["Singular"] = name.singular p.data["Plural"] = name.plural p.data["Term"] = b.viewInfo.term() - case page.KindTaxonomy: + case kinds.KindTaxonomy: b := p.treeRef.n name := b.viewInfo.name diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 7ad2a8d51..1c4f13d3b 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -40,6 +40,7 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/output" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page/pagemeta" "github.com/gohugoio/hugo/resources/resource" @@ -164,7 +165,7 @@ func (p *pageMeta) File() source.File { } func (p *pageMeta) IsHome() bool { - return p.Kind() == page.KindHome + return p.Kind() == kinds.KindHome } func (p *pageMeta) Keywords() []string { @@ -199,7 +200,7 @@ func (p *pageMeta) IsNode() bool { } func (p *pageMeta) IsPage() bool { - return p.Kind() == page.KindPage + return p.Kind() == kinds.KindPage } // Param is a convenience method to do lookups in Page's and Site's Params map, @@ -251,7 +252,7 @@ func (p *pageMeta) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, } func (p *pageMeta) IsSection() bool { - return p.Kind() == page.KindSection + return p.Kind() == kinds.KindSection } func (p *pageMeta) Section() string { @@ -680,9 +681,9 @@ func (p *pageMeta) applyDefaultValues(n *contentNode) error { if p.title == "" && p.f.IsZero() { switch p.Kind() { - case page.KindHome: + case kinds.KindHome: p.title = p.s.Title() - case page.KindSection: + case kinds.KindSection: var sectionName string if n != nil { sectionName = n.rootSection() @@ -696,13 +697,13 @@ func (p *pageMeta) applyDefaultValues(n *contentNode) error { } else { p.title = sectionName } - case page.KindTerm: + case kinds.KindTerm: // TODO(bep) improve key := p.sections[len(p.sections)-1] p.title = strings.Replace(p.s.conf.C.CreateTitle(key), "-", " ", -1) - case page.KindTaxonomy: + case kinds.KindTaxonomy: p.title = p.s.conf.C.CreateTitle(p.sections[0]) - case kind404: + case kinds.Kind404: p.title = "404 Page not found" } diff --git a/hugolib/page__paginator.go b/hugolib/page__paginator.go index 2ec89561c..6a1b4bfab 100644 --- a/hugolib/page__paginator.go +++ b/hugolib/page__paginator.go @@ -17,6 +17,7 @@ import ( "sync" "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -86,12 +87,12 @@ func (p *pagePaginator) Paginator(options ...any) (*page.Pager, error) { var pages page.Pages switch p.source.Kind() { - case page.KindHome: + case kinds.KindHome: // From Hugo 0.57 we made home.Pages() work like any other // section. To avoid the default paginators for the home page // changing in the wild, we make this a special case. pages = p.source.s.RegularPages() - case page.KindTerm, page.KindTaxonomy: + case kinds.KindTerm, kinds.KindTaxonomy: pages = p.source.Pages() default: pages = p.source.RegularPages() diff --git a/hugolib/page__paths.go b/hugolib/page__paths.go index a834a1d9d..9a6caa05e 100644 --- a/hugolib/page__paths.go +++ b/hugolib/page__paths.go @@ -20,6 +20,7 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugofs/files" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -125,7 +126,7 @@ func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.Target baseName = contentBaseName } - alwaysInSubDir := p.Kind() == kindSitemap + alwaysInSubDir := p.Kind() == kinds.KindSitemap desc := page.TargetPathDescriptor{ PathSpec: d.PathSpec, diff --git a/hugolib/page__tree.go b/hugolib/page__tree.go index 2f9fd2e46..8b02667f1 100644 --- a/hugolib/page__tree.go +++ b/hugolib/page__tree.go @@ -18,6 +18,7 @@ import ( "strings" "github.com/gohugoio/hugo/common/types" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -166,7 +167,7 @@ func (pt pageTree) Parent() page.Page { tree := p.getTreeRef() - if tree == nil || pt.p.Kind() == page.KindTaxonomy { + if tree == nil || pt.p.Kind() == kinds.KindTaxonomy { return pt.p.s.home } diff --git a/hugolib/page_kinds.go b/hugolib/page_kinds.go index 6536ad6bb..9bdf689d7 100644 --- a/hugolib/page_kinds.go +++ b/hugolib/page_kinds.go @@ -13,40 +13,6 @@ package hugolib -import ( - "strings" - - "github.com/gohugoio/hugo/resources/page" -) - -// This is all the kinds we can expect to find in .Site.Pages. -var allKindsInPages = []string{page.KindPage, page.KindHome, page.KindSection, page.KindTerm, page.KindTaxonomy} - const ( - - // Temporary state. - kindUnknown = "unknown" - - // The following are (currently) temporary nodes, - // i.e. nodes we create just to render in isolation. - kindRSS = "rss" - kindSitemap = "sitemap" - kindRobotsTXT = "robotstxt" - kind404 = "404" - pageResourceType = "page" ) - -var kindMap = map[string]string{ - strings.ToLower(kindRSS): kindRSS, - strings.ToLower(kindSitemap): kindSitemap, - strings.ToLower(kindRobotsTXT): kindRobotsTXT, - strings.ToLower(kind404): kind404, -} - -func getKind(s string) string { - if pkind := page.GetKind(s); pkind != "" { - return pkind - } - return kindMap[strings.ToLower(s)] -} diff --git a/hugolib/pagebundler_test.go b/hugolib/pagebundler_test.go index ebf5d628b..64d329832 100644 --- a/hugolib/pagebundler_test.go +++ b/hugolib/pagebundler_test.go @@ -34,6 +34,7 @@ import ( "github.com/gohugoio/hugo/hugofs" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/htesting" @@ -104,7 +105,7 @@ func TestPageBundlerSiteRegular(t *testing.T) { c.Assert(len(s.RegularPages()), qt.Equals, 8) - singlePage := s.getPage(page.KindPage, "a/1.md") + singlePage := s.getPage(kinds.KindPage, "a/1.md") c.Assert(singlePage.BundleType(), qt.Equals, files.ContentClass("")) c.Assert(singlePage, qt.Not(qt.IsNil)) @@ -150,18 +151,18 @@ func TestPageBundlerSiteRegular(t *testing.T) { // This should be just copied to destination. b.AssertFileContent(filepath.FromSlash("public/assets/pic1.png"), "content") - leafBundle1 := s.getPage(page.KindPage, "b/my-bundle/index.md") + leafBundle1 := s.getPage(kinds.KindPage, "b/my-bundle/index.md") c.Assert(leafBundle1, qt.Not(qt.IsNil)) c.Assert(leafBundle1.BundleType(), qt.Equals, files.ContentClassLeaf) c.Assert(leafBundle1.Section(), qt.Equals, "b") - sectionB := s.getPage(page.KindSection, "b") + sectionB := s.getPage(kinds.KindSection, "b") c.Assert(sectionB, qt.Not(qt.IsNil)) home := s.Home() c.Assert(home.BundleType(), qt.Equals, files.ContentClassBranch) // This is a root bundle and should live in the "home section" // See https://github.com/gohugoio/hugo/issues/4332 - rootBundle := s.getPage(page.KindPage, "root") + rootBundle := s.getPage(kinds.KindPage, "root") c.Assert(rootBundle, qt.Not(qt.IsNil)) c.Assert(rootBundle.Parent().IsHome(), qt.Equals, true) if !ugly { @@ -169,9 +170,9 @@ func TestPageBundlerSiteRegular(t *testing.T) { b.AssertFileContent(filepath.FromSlash("public/cpath/root/cindex.html"), "Single RelPermalink: "+relURLBase+"/cpath/root/") } - leafBundle2 := s.getPage(page.KindPage, "a/b/index.md") + leafBundle2 := s.getPage(kinds.KindPage, "a/b/index.md") c.Assert(leafBundle2, qt.Not(qt.IsNil)) - unicodeBundle := s.getPage(page.KindPage, "c/bundle/index.md") + unicodeBundle := s.getPage(kinds.KindPage, "c/bundle/index.md") c.Assert(unicodeBundle, qt.Not(qt.IsNil)) pageResources := leafBundle1.Resources().ByType(pageResourceType) @@ -302,7 +303,7 @@ func TestPageBundlerSiteMultilingual(t *testing.T) { c.Assert(len(s.AllPages()), qt.Equals, 31) - bundleWithSubPath := s.getPage(page.KindPage, "lb/index") + bundleWithSubPath := s.getPage(kinds.KindPage, "lb/index") c.Assert(bundleWithSubPath, qt.Not(qt.IsNil)) // See https://github.com/gohugoio/hugo/issues/4312 @@ -316,22 +317,22 @@ func TestPageBundlerSiteMultilingual(t *testing.T) { // and probably also just b (aka "my-bundle") // These may also be translated, so we also need to test that. // "bf", "my-bf-bundle", "index.md + nn - bfBundle := s.getPage(page.KindPage, "bf/my-bf-bundle/index") + bfBundle := s.getPage(kinds.KindPage, "bf/my-bf-bundle/index") c.Assert(bfBundle, qt.Not(qt.IsNil)) c.Assert(bfBundle.Language().Lang, qt.Equals, "en") - c.Assert(s.getPage(page.KindPage, "bf/my-bf-bundle/index.md"), qt.Equals, bfBundle) - c.Assert(s.getPage(page.KindPage, "bf/my-bf-bundle"), qt.Equals, bfBundle) - c.Assert(s.getPage(page.KindPage, "my-bf-bundle"), qt.Equals, bfBundle) + c.Assert(s.getPage(kinds.KindPage, "bf/my-bf-bundle/index.md"), qt.Equals, bfBundle) + c.Assert(s.getPage(kinds.KindPage, "bf/my-bf-bundle"), qt.Equals, bfBundle) + c.Assert(s.getPage(kinds.KindPage, "my-bf-bundle"), qt.Equals, bfBundle) nnSite := sites.Sites[1] c.Assert(len(nnSite.RegularPages()), qt.Equals, 7) - bfBundleNN := nnSite.getPage(page.KindPage, "bf/my-bf-bundle/index") + bfBundleNN := nnSite.getPage(kinds.KindPage, "bf/my-bf-bundle/index") c.Assert(bfBundleNN, qt.Not(qt.IsNil)) c.Assert(bfBundleNN.Language().Lang, qt.Equals, "nn") - c.Assert(nnSite.getPage(page.KindPage, "bf/my-bf-bundle/index.nn.md"), qt.Equals, bfBundleNN) - c.Assert(nnSite.getPage(page.KindPage, "bf/my-bf-bundle"), qt.Equals, bfBundleNN) - c.Assert(nnSite.getPage(page.KindPage, "my-bf-bundle"), qt.Equals, bfBundleNN) + c.Assert(nnSite.getPage(kinds.KindPage, "bf/my-bf-bundle/index.nn.md"), qt.Equals, bfBundleNN) + c.Assert(nnSite.getPage(kinds.KindPage, "bf/my-bf-bundle"), qt.Equals, bfBundleNN) + c.Assert(nnSite.getPage(kinds.KindPage, "my-bf-bundle"), qt.Equals, bfBundleNN) // See https://github.com/gohugoio/hugo/issues/4295 // Every resource should have its Name prefixed with its base folder. @@ -483,7 +484,7 @@ TheContent. s := b.H.Sites[0] c.Assert(len(s.RegularPages()), qt.Equals, 7) - a1Bundle := s.getPage(page.KindPage, "symbolic2/a1/index.md") + a1Bundle := s.getPage(kinds.KindPage, "symbolic2/a1/index.md") c.Assert(a1Bundle, qt.Not(qt.IsNil)) c.Assert(len(a1Bundle.Resources()), qt.Equals, 2) c.Assert(len(a1Bundle.Resources().ByType(pageResourceType)), qt.Equals, 1) @@ -543,10 +544,10 @@ HEADLESS {{< myShort >}} c.Assert(len(s.RegularPages()), qt.Equals, 1) - regular := s.getPage(page.KindPage, "a/index") + regular := s.getPage(kinds.KindPage, "a/index") c.Assert(regular.RelPermalink(), qt.Equals, "/s1/") - headless := s.getPage(page.KindPage, "b/index") + headless := s.getPage(kinds.KindPage, "b/index") c.Assert(headless, qt.Not(qt.IsNil)) c.Assert(headless.Title(), qt.Equals, "Headless Bundle in Topless Bar") c.Assert(headless.RelPermalink(), qt.Equals, "") diff --git a/hugolib/pagecollections.go b/hugolib/pagecollections.go index b49669bb6..26da4905e 100644 --- a/hugolib/pagecollections.go +++ b/hugolib/pagecollections.go @@ -26,6 +26,7 @@ import ( "github.com/gohugoio/hugo/helpers" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -92,7 +93,7 @@ func newPageCollections(m *pageMap) *PageCollections { }) c.regularPages = newLazyPagesFactory(func() page.Pages { - return c.findPagesByKindIn(page.KindPage, c.pages.get()) + return c.findPagesByKindIn(kinds.KindPage, c.pages.get()) }) return c @@ -120,10 +121,10 @@ func (c *PageCollections) getPageOldVersion(ref ...string) (page.Page, error) { return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref) } - if len(refs) == 0 || refs[0] == page.KindHome { + if len(refs) == 0 || refs[0] == kinds.KindHome { key = "/" } else if len(refs) == 1 { - if len(ref) == 2 && refs[0] == page.KindSection { + if len(ref) == 2 && refs[0] == kinds.KindSection { // This is an old style reference to the "Home Page section". // Typically fetched via {{ .Site.GetPage "section" .Section }} // See https://github.com/gohugoio/hugo/issues/4989 diff --git a/hugolib/pagecollections_test.go b/hugolib/pagecollections_test.go index 66f157781..1be296262 100644 --- a/hugolib/pagecollections_test.go +++ b/hugolib/pagecollections_test.go @@ -22,6 +22,7 @@ import ( "time" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/deps" @@ -231,72 +232,72 @@ func TestGetPage(t *testing.T) { tests := []getPageTest{ // legacy content root relative paths - {"Root relative, no slash, home", page.KindHome, nil, []string{""}, "home page"}, - {"Root relative, no slash, root page", page.KindPage, nil, []string{"about.md", "ABOUT.md"}, "about page"}, - {"Root relative, no slash, section", page.KindSection, nil, []string{"sect3"}, "section 3"}, - {"Root relative, no slash, section page", page.KindPage, nil, []string{"sect3/page1.md"}, "Title3_1"}, - {"Root relative, no slash, sub section", page.KindSection, nil, []string{"sect3/sect7"}, "another sect7"}, - {"Root relative, no slash, nested page", page.KindPage, nil, []string{"sect3/subsect/deep.md"}, "deep page"}, - {"Root relative, no slash, OS slashes", page.KindPage, nil, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"}, + {"Root relative, no slash, home", kinds.KindHome, nil, []string{""}, "home page"}, + {"Root relative, no slash, root page", kinds.KindPage, nil, []string{"about.md", "ABOUT.md"}, "about page"}, + {"Root relative, no slash, section", kinds.KindSection, nil, []string{"sect3"}, "section 3"}, + {"Root relative, no slash, section page", kinds.KindPage, nil, []string{"sect3/page1.md"}, "Title3_1"}, + {"Root relative, no slash, sub section", kinds.KindSection, nil, []string{"sect3/sect7"}, "another sect7"}, + {"Root relative, no slash, nested page", kinds.KindPage, nil, []string{"sect3/subsect/deep.md"}, "deep page"}, + {"Root relative, no slash, OS slashes", kinds.KindPage, nil, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"}, - {"Short ref, unique", page.KindPage, nil, []string{"unique.md", "unique"}, "UniqueBase"}, - {"Short ref, unique, upper case", page.KindPage, nil, []string{"Unique2.md", "unique2.md", "unique2"}, "UniqueBase2"}, + {"Short ref, unique", kinds.KindPage, nil, []string{"unique.md", "unique"}, "UniqueBase"}, + {"Short ref, unique, upper case", kinds.KindPage, nil, []string{"Unique2.md", "unique2.md", "unique2"}, "UniqueBase2"}, {"Short ref, ambiguous", "Ambiguous", nil, []string{"page1.md"}, ""}, // ISSUE: This is an ambiguous ref, but because we have to support the legacy // content root relative paths without a leading slash, the lookup // returns /sect7. This undermines ambiguity detection, but we have no choice. //{"Ambiguous", nil, []string{"sect7"}, ""}, - {"Section, ambiguous", page.KindSection, nil, []string{"sect7"}, "Sect7s"}, + {"Section, ambiguous", kinds.KindSection, nil, []string{"sect7"}, "Sect7s"}, - {"Absolute, home", page.KindHome, nil, []string{"/", ""}, "home page"}, - {"Absolute, page", page.KindPage, nil, []string{"/about.md", "/about"}, "about page"}, - {"Absolute, sect", page.KindSection, nil, []string{"/sect3"}, "section 3"}, - {"Absolute, page in subsection", page.KindPage, nil, []string{"/sect3/page1.md", "/Sect3/Page1.md"}, "Title3_1"}, - {"Absolute, section, subsection with same name", page.KindSection, nil, []string{"/sect3/sect7"}, "another sect7"}, - {"Absolute, page, deep", page.KindPage, nil, []string{"/sect3/subsect/deep.md"}, "deep page"}, - {"Absolute, page, OS slashes", page.KindPage, nil, []string{filepath.FromSlash("/sect5/page3.md")}, "Title5_3"}, // test OS-specific path - {"Absolute, unique", page.KindPage, nil, []string{"/sect3/unique.md"}, "UniqueBase"}, - {"Absolute, unique, case", page.KindPage, nil, []string{"/sect3/Unique2.md", "/sect3/unique2.md", "/sect3/unique2", "/sect3/Unique2"}, "UniqueBase2"}, + {"Absolute, home", kinds.KindHome, nil, []string{"/", ""}, "home page"}, + {"Absolute, page", kinds.KindPage, nil, []string{"/about.md", "/about"}, "about page"}, + {"Absolute, sect", kinds.KindSection, nil, []string{"/sect3"}, "section 3"}, + {"Absolute, page in subsection", kinds.KindPage, nil, []string{"/sect3/page1.md", "/Sect3/Page1.md"}, "Title3_1"}, + {"Absolute, section, subsection with same name", kinds.KindSection, nil, []string{"/sect3/sect7"}, "another sect7"}, + {"Absolute, page, deep", kinds.KindPage, nil, []string{"/sect3/subsect/deep.md"}, "deep page"}, + {"Absolute, page, OS slashes", kinds.KindPage, nil, []string{filepath.FromSlash("/sect5/page3.md")}, "Title5_3"}, // test OS-specific path + {"Absolute, unique", kinds.KindPage, nil, []string{"/sect3/unique.md"}, "UniqueBase"}, + {"Absolute, unique, case", kinds.KindPage, nil, []string{"/sect3/Unique2.md", "/sect3/unique2.md", "/sect3/unique2", "/sect3/Unique2"}, "UniqueBase2"}, // next test depends on this page existing // {"NoPage", nil, []string{"/unique.md"}, ""}, // ISSUE #4969: this is resolving to /sect3/unique.md {"Absolute, missing page", "NoPage", nil, []string{"/missing-page.md"}, ""}, {"Absolute, missing section", "NoPage", nil, []string{"/missing-section"}, ""}, // relative paths - {"Dot relative, home", page.KindHome, sec3, []string{".."}, "home page"}, - {"Dot relative, home, slash", page.KindHome, sec3, []string{"../"}, "home page"}, - {"Dot relative about", page.KindPage, sec3, []string{"../about.md"}, "about page"}, - {"Dot", page.KindSection, sec3, []string{"."}, "section 3"}, - {"Dot slash", page.KindSection, sec3, []string{"./"}, "section 3"}, - {"Page relative, no dot", page.KindPage, sec3, []string{"page1.md"}, "Title3_1"}, - {"Page relative, dot", page.KindPage, sec3, []string{"./page1.md"}, "Title3_1"}, - {"Up and down another section", page.KindPage, sec3, []string{"../sect4/page2.md"}, "Title4_2"}, - {"Rel sect7", page.KindSection, sec3, []string{"sect7"}, "another sect7"}, - {"Rel sect7 dot", page.KindSection, sec3, []string{"./sect7"}, "another sect7"}, - {"Dot deep", page.KindPage, sec3, []string{"./subsect/deep.md"}, "deep page"}, - {"Dot dot inner", page.KindPage, sec3, []string{"./subsect/../../sect7/page9.md"}, "Title7_9"}, - {"Dot OS slash", page.KindPage, sec3, []string{filepath.FromSlash("../sect5/page3.md")}, "Title5_3"}, // test OS-specific path - {"Dot unique", page.KindPage, sec3, []string{"./unique.md"}, "UniqueBase"}, + {"Dot relative, home", kinds.KindHome, sec3, []string{".."}, "home page"}, + {"Dot relative, home, slash", kinds.KindHome, sec3, []string{"../"}, "home page"}, + {"Dot relative about", kinds.KindPage, sec3, []string{"../about.md"}, "about page"}, + {"Dot", kinds.KindSection, sec3, []string{"."}, "section 3"}, + {"Dot slash", kinds.KindSection, sec3, []string{"./"}, "section 3"}, + {"Page relative, no dot", kinds.KindPage, sec3, []string{"page1.md"}, "Title3_1"}, + {"Page relative, dot", kinds.KindPage, sec3, []string{"./page1.md"}, "Title3_1"}, + {"Up and down another section", kinds.KindPage, sec3, []string{"../sect4/page2.md"}, "Title4_2"}, + {"Rel sect7", kinds.KindSection, sec3, []string{"sect7"}, "another sect7"}, + {"Rel sect7 dot", kinds.KindSection, sec3, []string{"./sect7"}, "another sect7"}, + {"Dot deep", kinds.KindPage, sec3, []string{"./subsect/deep.md"}, "deep page"}, + {"Dot dot inner", kinds.KindPage, sec3, []string{"./subsect/../../sect7/page9.md"}, "Title7_9"}, + {"Dot OS slash", kinds.KindPage, sec3, []string{filepath.FromSlash("../sect5/page3.md")}, "Title5_3"}, // test OS-specific path + {"Dot unique", kinds.KindPage, sec3, []string{"./unique.md"}, "UniqueBase"}, {"Dot sect", "NoPage", sec3, []string{"./sect2"}, ""}, //{"NoPage", sec3, []string{"sect2"}, ""}, // ISSUE: /sect3 page relative query is resolving to /sect2 - {"Abs, ignore context, home", page.KindHome, sec3, []string{"/"}, "home page"}, - {"Abs, ignore context, about", page.KindPage, sec3, []string{"/about.md"}, "about page"}, - {"Abs, ignore context, page in section", page.KindPage, sec3, []string{"/sect4/page2.md"}, "Title4_2"}, - {"Abs, ignore context, page subsect deep", page.KindPage, sec3, []string{"/sect3/subsect/deep.md"}, "deep page"}, // next test depends on this page existing + {"Abs, ignore context, home", kinds.KindHome, sec3, []string{"/"}, "home page"}, + {"Abs, ignore context, about", kinds.KindPage, sec3, []string{"/about.md"}, "about page"}, + {"Abs, ignore context, page in section", kinds.KindPage, sec3, []string{"/sect4/page2.md"}, "Title4_2"}, + {"Abs, ignore context, page subsect deep", kinds.KindPage, sec3, []string{"/sect3/subsect/deep.md"}, "deep page"}, // next test depends on this page existing {"Abs, ignore context, page deep", "NoPage", sec3, []string{"/subsect/deep.md"}, ""}, // Taxonomies - {"Taxonomy term", page.KindTaxonomy, nil, []string{"categories"}, "Categories"}, - {"Taxonomy", page.KindTerm, nil, []string{"categories/hugo", "categories/Hugo"}, "Hugo"}, + {"Taxonomy term", kinds.KindTaxonomy, nil, []string{"categories"}, "Categories"}, + {"Taxonomy", kinds.KindTerm, nil, []string{"categories/hugo", "categories/Hugo"}, "Hugo"}, // Bundle variants - {"Bundle regular", page.KindPage, nil, []string{"sect3/b1", "sect3/b1/index.md", "sect3/b1/index.en.md"}, "b1 bundle"}, - {"Bundle index name", page.KindPage, nil, []string{"sect3/index/index.md", "sect3/index"}, "index bundle"}, + {"Bundle regular", kinds.KindPage, nil, []string{"sect3/b1", "sect3/b1/index.md", "sect3/b1/index.en.md"}, "b1 bundle"}, + {"Bundle index name", kinds.KindPage, nil, []string{"sect3/index/index.md", "sect3/index"}, "index bundle"}, // https://github.com/gohugoio/hugo/issues/7301 - {"Section and bundle overlap", page.KindPage, nil, []string{"section_bundle_overlap_bundle"}, "index overlap bundle"}, + {"Section and bundle overlap", kinds.KindPage, nil, []string{"section_bundle_overlap_bundle"}, "index overlap bundle"}, } for _, test := range tests { diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index a95b38967..774794c56 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -22,9 +22,9 @@ import ( "testing" "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/parser/pageparser" - "github.com/gohugoio/hugo/resources/page" qt "github.com/frankban/quicktest" ) @@ -186,7 +186,7 @@ CSV: {{< myShort >}} b.Assert(len(h.Sites), qt.Equals, 1) s := h.Sites[0] - home := s.getPage(page.KindHome) + home := s.getPage(kinds.KindHome) b.Assert(home, qt.Not(qt.IsNil)) b.Assert(len(home.OutputFormats()), qt.Equals, 3) diff --git a/hugolib/site.go b/hugolib/site.go index b5c43fba3..863b5c4b7 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -52,6 +52,7 @@ import ( "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/config" @@ -248,7 +249,7 @@ func (s *Site) initRenderFormats() { }) // Add the per kind configured output formats - for _, kind := range allKindsInPages { + for _, kind := range kinds.AllKindsInPages { if siteFormats, found := s.conf.C.KindOutputFormats[kind]; found { for _, f := range siteFormats { if !formatSet[f.Name] { @@ -276,9 +277,6 @@ func (s *Site) Languages() langs.Languages { } func (s *Site) isEnabled(kind string) bool { - if kind == kindUnknown { - panic("Unknown kind") - } return s.conf.IsKindEnabled(kind) } @@ -1141,19 +1139,19 @@ func (s *Site) publish(statCounter *uint64, path string, r io.Reader, fs afero.F func (s *Site) kindFromFileInfoOrSections(fi *fileInfo, sections []string) string { if fi.TranslationBaseName() == "_index" { if fi.Dir() == "" { - return page.KindHome + return kinds.KindHome } return s.kindFromSections(sections) } - return page.KindPage + return kinds.KindPage } func (s *Site) kindFromSections(sections []string) string { if len(sections) == 0 { - return page.KindHome + return kinds.KindHome } return s.kindFromSectionPath(path.Join(sections...)) @@ -1163,16 +1161,16 @@ func (s *Site) kindFromSectionPath(sectionPath string) string { var taxonomiesConfig taxonomiesConfig = s.conf.Taxonomies for _, plural := range taxonomiesConfig { if plural == sectionPath { - return page.KindTaxonomy + return kinds.KindTaxonomy } if strings.HasPrefix(sectionPath, plural) { - return page.KindTerm + return kinds.KindTerm } } - return page.KindSection + return kinds.KindSection } func (s *Site) newPage( diff --git a/hugolib/site_new.go b/hugolib/site_new.go index 61273d3c8..13c845376 100644 --- a/hugolib/site_new.go +++ b/hugolib/site_new.go @@ -40,6 +40,7 @@ import ( "github.com/gohugoio/hugo/navigation" "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/publisher" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page/pagemeta" "github.com/gohugoio/hugo/resources/resource" @@ -185,9 +186,9 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { contentMap: newContentMap(contentMapConfig{ lang: k, taxonomyConfig: taxonomiesConfig.Values(), - taxonomyDisabled: !conf.IsKindEnabled(page.KindTerm), - taxonomyTermDisabled: !conf.IsKindEnabled(page.KindTaxonomy), - pageDisabled: !conf.IsKindEnabled(page.KindPage), + taxonomyDisabled: !conf.IsKindEnabled(kinds.KindTerm), + taxonomyTermDisabled: !conf.IsKindEnabled(kinds.KindTaxonomy), + pageDisabled: !conf.IsKindEnabled(kinds.KindPage), }), s: s, } diff --git a/hugolib/site_output.go b/hugolib/site_output.go index 1e248baff..d6f55cbdd 100644 --- a/hugolib/site_output.go +++ b/hugolib/site_output.go @@ -18,7 +18,7 @@ import ( "strings" "github.com/gohugoio/hugo/output" - "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/resources/kinds" "github.com/spf13/cast" ) @@ -34,20 +34,20 @@ func createDefaultOutputFormats(allFormats output.Formats) map[string]output.For } m := map[string]output.Formats{ - page.KindPage: {htmlOut}, - page.KindHome: defaultListTypes, - page.KindSection: defaultListTypes, - page.KindTerm: defaultListTypes, - page.KindTaxonomy: defaultListTypes, + kinds.KindPage: {htmlOut}, + kinds.KindHome: defaultListTypes, + kinds.KindSection: defaultListTypes, + kinds.KindTerm: defaultListTypes, + kinds.KindTaxonomy: defaultListTypes, // Below are for consistency. They are currently not used during rendering. - kindSitemap: {sitemapOut}, - kindRobotsTXT: {robotsOut}, - kind404: {htmlOut}, + kinds.KindSitemap: {sitemapOut}, + kinds.KindRobotsTXT: {robotsOut}, + kinds.Kind404: {htmlOut}, } // May be disabled if rssFound { - m[kindRSS] = output.Formats{rssOut} + m[kinds.KindRSS] = output.Formats{rssOut} } return m @@ -69,7 +69,7 @@ func createSiteOutputFormats(allFormats output.Formats, outputs map[string]any, seen := make(map[string]bool) for k, v := range outputs { - k = getKind(k) + k = kinds.GetKindAny(k) if k == "" { // Invalid kind continue diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go index b598e23d6..ce415a824 100644 --- a/hugolib/site_output_test.go +++ b/hugolib/site_output_test.go @@ -21,7 +21,7 @@ import ( qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/resources/kinds" "github.com/spf13/afero" @@ -142,7 +142,7 @@ Len Pages: {{ .Kind }} {{ len .Site.RegularPages }} Page Number: {{ .Paginator.P s := b.H.Sites[0] b.Assert(s.language.Lang, qt.Equals, "en") - home := s.getPage(page.KindHome) + home := s.getPage(kinds.KindHome) b.Assert(home, qt.Not(qt.IsNil)) @@ -314,7 +314,7 @@ baseName = "customdelimbase" th.assertFileContent("public/nosuffixbase", "no suffix") th.assertFileContent("public/customdelimbase_del", "custom delim") - home := s.getPage(page.KindHome) + home := s.getPage(kinds.KindHome) c.Assert(home, qt.Not(qt.IsNil)) outputs := home.OutputFormats() @@ -360,8 +360,8 @@ func TestCreateSiteOutputFormats(t *testing.T) { c := qt.New(t) outputsConfig := map[string]any{ - page.KindHome: []string{"HTML", "JSON"}, - page.KindSection: []string{"JSON"}, + kinds.KindHome: []string{"HTML", "JSON"}, + kinds.KindSection: []string{"JSON"}, } cfg := config.New() @@ -369,21 +369,21 @@ func TestCreateSiteOutputFormats(t *testing.T) { outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg.GetStringMap("outputs"), false) c.Assert(err, qt.IsNil) - c.Assert(outputs[page.KindSection], deepEqualsOutputFormats, output.Formats{output.JSONFormat}) - c.Assert(outputs[page.KindHome], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.JSONFormat}) + c.Assert(outputs[kinds.KindSection], deepEqualsOutputFormats, output.Formats{output.JSONFormat}) + c.Assert(outputs[kinds.KindHome], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.JSONFormat}) // Defaults - c.Assert(outputs[page.KindTerm], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) - c.Assert(outputs[page.KindTaxonomy], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) - c.Assert(outputs[page.KindPage], deepEqualsOutputFormats, output.Formats{output.HTMLFormat}) + c.Assert(outputs[kinds.KindTerm], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) + c.Assert(outputs[kinds.KindTaxonomy], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) + c.Assert(outputs[kinds.KindPage], deepEqualsOutputFormats, output.Formats{output.HTMLFormat}) // These aren't (currently) in use when rendering in Hugo, // but the pages needs to be assigned an output format, // so these should also be correct/sensible. - c.Assert(outputs[kindRSS], deepEqualsOutputFormats, output.Formats{output.RSSFormat}) - c.Assert(outputs[kindSitemap], deepEqualsOutputFormats, output.Formats{output.SitemapFormat}) - c.Assert(outputs[kindRobotsTXT], deepEqualsOutputFormats, output.Formats{output.RobotsTxtFormat}) - c.Assert(outputs[kind404], deepEqualsOutputFormats, output.Formats{output.HTMLFormat}) + c.Assert(outputs[kinds.KindRSS], deepEqualsOutputFormats, output.Formats{output.RSSFormat}) + c.Assert(outputs[kinds.KindSitemap], deepEqualsOutputFormats, output.Formats{output.SitemapFormat}) + c.Assert(outputs[kinds.KindRobotsTXT], deepEqualsOutputFormats, output.Formats{output.RobotsTxtFormat}) + c.Assert(outputs[kinds.Kind404], deepEqualsOutputFormats, output.Formats{output.HTMLFormat}) }) // Issue #4528 @@ -400,7 +400,7 @@ func TestCreateSiteOutputFormats(t *testing.T) { outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg.GetStringMap("outputs"), false) c.Assert(err, qt.IsNil) - c.Assert(outputs[page.KindTaxonomy], deepEqualsOutputFormats, output.Formats{output.JSONFormat}) + c.Assert(outputs[kinds.KindTaxonomy], deepEqualsOutputFormats, output.Formats{output.JSONFormat}) }) } @@ -408,7 +408,7 @@ func TestCreateSiteOutputFormatsInvalidConfig(t *testing.T) { c := qt.New(t) outputsConfig := map[string]any{ - page.KindHome: []string{"FOO", "JSON"}, + kinds.KindHome: []string{"FOO", "JSON"}, } cfg := config.New() @@ -422,7 +422,7 @@ func TestCreateSiteOutputFormatsEmptyConfig(t *testing.T) { c := qt.New(t) outputsConfig := map[string]any{ - page.KindHome: []string{}, + kinds.KindHome: []string{}, } cfg := config.New() @@ -430,14 +430,14 @@ func TestCreateSiteOutputFormatsEmptyConfig(t *testing.T) { outputs, err := createSiteOutputFormats(output.DefaultFormats, cfg.GetStringMap("outputs"), false) c.Assert(err, qt.IsNil) - c.Assert(outputs[page.KindHome], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) + c.Assert(outputs[kinds.KindHome], deepEqualsOutputFormats, output.Formats{output.HTMLFormat, output.RSSFormat}) } func TestCreateSiteOutputFormatsCustomFormats(t *testing.T) { c := qt.New(t) outputsConfig := map[string]any{ - page.KindHome: []string{}, + kinds.KindHome: []string{}, } cfg := config.New() @@ -450,7 +450,7 @@ func TestCreateSiteOutputFormatsCustomFormats(t *testing.T) { outputs, err := createSiteOutputFormats(output.Formats{customRSS, customHTML}, cfg.GetStringMap("outputs"), false) c.Assert(err, qt.IsNil) - c.Assert(outputs[page.KindHome], deepEqualsOutputFormats, output.Formats{customHTML, customRSS}) + c.Assert(outputs[kinds.KindHome], deepEqualsOutputFormats, output.Formats{customHTML, customRSS}) } // https://github.com/gohugoio/hugo/issues/5849 diff --git a/hugolib/site_render.go b/hugolib/site_render.go index f9c1dd498..43371b44d 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -29,6 +29,7 @@ import ( "github.com/gohugoio/hugo/output" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page/pagemeta" ) @@ -228,7 +229,7 @@ func (s *Site) renderPaginator(p *pageState, templ tpl.Template) error { func (s *Site) render404() error { p, err := newPageStandalone(&pageMeta{ s: s, - kind: kind404, + kind: kinds.Kind404, urlPaths: pagemeta.URLPath{ URL: "404.html", }, @@ -244,7 +245,7 @@ func (s *Site) render404() error { } var d layouts.LayoutDescriptor - d.Kind = kind404 + d.Kind = kinds.Kind404 templ, found, err := s.Tmpl().LookupLayout(d, output.HTMLFormat) if err != nil { @@ -266,7 +267,7 @@ func (s *Site) render404() error { func (s *Site) renderSitemap() error { p, err := newPageStandalone(&pageMeta{ s: s, - kind: kindSitemap, + kind: kinds.KindSitemap, urlPaths: pagemeta.URLPath{ URL: s.conf.Sitemap.Filename, }, @@ -294,13 +295,13 @@ func (s *Site) renderSitemap() error { } func (s *Site) renderRobotsTXT() error { - if !s.conf.EnableRobotsTXT && s.isEnabled(kindRobotsTXT) { + if !s.conf.EnableRobotsTXT && s.isEnabled(kinds.KindRobotsTXT) { return nil } p, err := newPageStandalone(&pageMeta{ s: s, - kind: kindRobotsTXT, + kind: kinds.KindRobotsTXT, urlPaths: pagemeta.URLPath{ URL: "robots.txt", }, diff --git a/hugolib/site_sections_test.go b/hugolib/site_sections_test.go index 5c97163cb..f5cb41d28 100644 --- a/hugolib/site_sections_test.go +++ b/hugolib/site_sections_test.go @@ -21,6 +21,7 @@ import ( qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/deps" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -293,7 +294,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }} }}, } - home := s.getPage(page.KindHome) + home := s.getPage(kinds.KindHome) for _, test := range tests { test := test @@ -301,7 +302,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }} t.Parallel() c := qt.New(t) sections := strings.Split(test.sections, ",") - p := s.getPage(page.KindSection, sections...) + p := s.getPage(kinds.KindSection, sections...) c.Assert(p, qt.Not(qt.IsNil), qt.Commentf(fmt.Sprint(sections))) if p.Pages() != nil { @@ -318,7 +319,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }} c.Assert(len(home.Sections()), qt.Equals, 9) c.Assert(s.Sections(), deepEqualsPages, home.Sections()) - rootPage := s.getPage(page.KindPage, "mypage.md") + rootPage := s.getPage(kinds.KindPage, "mypage.md") c.Assert(rootPage, qt.Not(qt.IsNil)) c.Assert(rootPage.Parent().IsHome(), qt.Equals, true) // https://github.com/gohugoio/hugo/issues/6365 @@ -330,7 +331,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }} // If we later decide to do something about this, we will have to do some normalization in // getPage. // TODO(bep) - sectionWithSpace := s.getPage(page.KindSection, "Spaces in Section") + sectionWithSpace := s.getPage(kinds.KindSection, "Spaces in Section") c.Assert(sectionWithSpace, qt.Not(qt.IsNil)) c.Assert(sectionWithSpace.RelPermalink(), qt.Equals, "/spaces-in-section/") diff --git a/hugolib/site_test.go b/hugolib/site_test.go index effdac6df..f4d63943f 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -28,6 +28,7 @@ import ( qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/deps" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" ) @@ -717,7 +718,7 @@ func TestOrderedPages(t *testing.T) { s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true}) - if s.getPage(page.KindSection, "sect").Pages()[1].Title() != "Three" || s.getPage(page.KindSection, "sect").Pages()[2].Title() != "Four" { + if s.getPage(kinds.KindSection, "sect").Pages()[1].Title() != "Three" || s.getPage(kinds.KindSection, "sect").Pages()[2].Title() != "Four" { t.Error("Pages in unexpected order.") } @@ -1010,7 +1011,7 @@ func TestRefLinking(t *testing.T) { t.Parallel() site := setupLinkingMockSite(t) - currentPage := site.getPage(page.KindPage, "level2/level3/start.md") + currentPage := site.getPage(kinds.KindPage, "level2/level3/start.md") if currentPage == nil { t.Fatalf("failed to find current page in site") } diff --git a/hugolib/site_url_test.go b/hugolib/site_url_test.go index 821c84c95..fd15eb5d3 100644 --- a/hugolib/site_url_test.go +++ b/hugolib/site_url_test.go @@ -18,10 +18,9 @@ import ( "path/filepath" "testing" - "github.com/gohugoio/hugo/resources/page" - qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/deps" + "github.com/gohugoio/hugo/resources/kinds" ) const slugDoc1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - /sd1/foo/\n - /sd2\n - /sd3/\n - /sd4.html\n---\nslug doc 1 content\n" @@ -68,12 +67,12 @@ Do not go gentle into that good night. c.Assert(len(s.RegularPages()), qt.Equals, 2) - notUgly := s.getPage(page.KindPage, "sect1/p1.md") + notUgly := s.getPage(kinds.KindPage, "sect1/p1.md") c.Assert(notUgly, qt.Not(qt.IsNil)) c.Assert(notUgly.Section(), qt.Equals, "sect1") c.Assert(notUgly.RelPermalink(), qt.Equals, "/sect1/p1/") - ugly := s.getPage(page.KindPage, "sect2/p2.md") + ugly := s.getPage(kinds.KindPage, "sect2/p2.md") c.Assert(ugly, qt.Not(qt.IsNil)) c.Assert(ugly.Section(), qt.Equals, "sect2") c.Assert(ugly.RelPermalink(), qt.Equals, "/sect2/p2.html") @@ -125,7 +124,7 @@ Do not go gentle into that good night. c.Assert(len(s.RegularPages()), qt.Equals, 10) - sect1 := s.getPage(page.KindSection, "sect1") + sect1 := s.getPage(kinds.KindSection, "sect1") c.Assert(sect1, qt.Not(qt.IsNil)) c.Assert(sect1.RelPermalink(), qt.Equals, "/ss1/") th.assertFileContent(filepath.Join("public", "ss1", "index.html"), "P1|URL: /ss1/|Next: /ss1/page/2/") diff --git a/hugolib/taxonomy_test.go b/hugolib/taxonomy_test.go index 94e937c81..d4ded2058 100644 --- a/hugolib/taxonomy_test.go +++ b/hugolib/taxonomy_test.go @@ -20,6 +20,7 @@ import ( "strings" "testing" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" qt "github.com/frankban/quicktest" @@ -155,8 +156,8 @@ permalinkeds: s := b.H.Sites[0] - // Make sure that each page.KindTaxonomyTerm page has an appropriate number - // of page.KindTaxonomy pages in its Pages slice. + // Make sure that each kinds.KindTaxonomyTerm page has an appropriate number + // of kinds.KindTaxonomy pages in its Pages slice. taxonomyTermPageCounts := map[string]int{ "tags": 3, "categories": 2, @@ -167,16 +168,16 @@ permalinkeds: for taxonomy, count := range taxonomyTermPageCounts { msg := qt.Commentf(taxonomy) - term := s.getPage(page.KindTaxonomy, taxonomy) + term := s.getPage(kinds.KindTaxonomy, taxonomy) b.Assert(term, qt.Not(qt.IsNil), msg) b.Assert(len(term.Pages()), qt.Equals, count, msg) for _, p := range term.Pages() { - b.Assert(p.Kind(), qt.Equals, page.KindTerm) + b.Assert(p.Kind(), qt.Equals, kinds.KindTerm) } } - cat1 := s.getPage(page.KindTerm, "categories", "cat1") + cat1 := s.getPage(kinds.KindTerm, "categories", "cat1") b.Assert(cat1, qt.Not(qt.IsNil)) if uglyURLs { b.Assert(cat1.RelPermalink(), qt.Equals, "/blog/categories/cat1.html") @@ -184,8 +185,8 @@ permalinkeds: b.Assert(cat1.RelPermalink(), qt.Equals, "/blog/categories/cat1/") } - pl1 := s.getPage(page.KindTerm, "permalinkeds", "pl1") - permalinkeds := s.getPage(page.KindTaxonomy, "permalinkeds") + pl1 := s.getPage(kinds.KindTerm, "permalinkeds", "pl1") + permalinkeds := s.getPage(kinds.KindTaxonomy, "permalinkeds") b.Assert(pl1, qt.Not(qt.IsNil)) b.Assert(permalinkeds, qt.Not(qt.IsNil)) if uglyURLs { @@ -196,7 +197,7 @@ permalinkeds: b.Assert(permalinkeds.RelPermalink(), qt.Equals, "/blog/permalinkeds/") } - helloWorld := s.getPage(page.KindTerm, "others", "hello-hugo-world") + helloWorld := s.getPage(kinds.KindTerm, "others", "hello-hugo-world") b.Assert(helloWorld, qt.Not(qt.IsNil)) b.Assert(helloWorld.Title(), qt.Equals, "Hello Hugo world") @@ -268,8 +269,8 @@ title: "This is S3s" return pages } - ta := filterbyKind(page.KindTerm) - te := filterbyKind(page.KindTaxonomy) + ta := filterbyKind(kinds.KindTerm) + te := filterbyKind(kinds.KindTaxonomy) b.Assert(len(te), qt.Equals, 4) b.Assert(len(ta), qt.Equals, 7) diff --git a/resources/kinds/kinds.go b/resources/kinds/kinds.go new file mode 100644 index 000000000..b1d1c18f4 --- /dev/null +++ b/resources/kinds/kinds.go @@ -0,0 +1,107 @@ +// Copyright 2023 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package kinds + +import ( + "sort" + "strings" +) + +const ( + KindPage = "page" + + // The rest are node types; home page, sections etc. + + KindHome = "home" + KindSection = "section" + + // Note that before Hugo 0.73 these were confusingly named + // taxonomy (now: term) + // taxonomyTerm (now: taxonomy) + KindTaxonomy = "taxonomy" + KindTerm = "term" + + // The following are (currently) temporary nodes, + // i.e. nodes we create just to render in isolation. + KindRSS = "rss" + KindSitemap = "sitemap" + KindRobotsTXT = "robotstxt" + Kind404 = "404" +) + +var ( + // This is all the kinds we can expect to find in .Site.Pages. + AllKindsInPages []string + // This is all the kinds, including the temporary ones. + AllKinds []string +) + +func init() { + for k := range kindMapMain { + AllKindsInPages = append(AllKindsInPages, k) + AllKinds = append(AllKinds, k) + } + + for k := range kindMapTemporary { + AllKinds = append(AllKinds, k) + } + + // Sort the slices for determinism. + sort.Strings(AllKindsInPages) + sort.Strings(AllKinds) +} + +var kindMapMain = map[string]string{ + KindPage: KindPage, + KindHome: KindHome, + KindSection: KindSection, + KindTaxonomy: KindTaxonomy, + KindTerm: KindTerm, + + // Legacy, pre v0.53.0. + "taxonomyterm": KindTaxonomy, +} + +var kindMapTemporary = map[string]string{ + KindRSS: KindRSS, + KindSitemap: KindSitemap, + KindRobotsTXT: KindRobotsTXT, + Kind404: Kind404, +} + +// GetKindMain gets the page kind given a string, empty if not found. +// Note that this will not return any temporary kinds (e.g. robotstxt). +func GetKindMain(s string) string { + return kindMapMain[strings.ToLower(s)] +} + +// GetKindAny gets the page kind given a string, empty if not found. +func GetKindAny(s string) string { + if pkind := GetKindMain(s); pkind != "" { + return pkind + } + return kindMapTemporary[strings.ToLower(s)] +} + +// IsDeprecated returns whether the given kind is deprecated. +func IsDeprecated(s string) bool { + s = strings.ToLower(s) + + switch s { + case "taxonomyterm": + return true + default: + return false + } +} diff --git a/resources/page/page_kinds_test.go b/resources/kinds/kinds_test.go similarity index 66% rename from resources/page/page_kinds_test.go rename to resources/kinds/kinds_test.go index 357be6739..c2868d617 100644 --- a/resources/page/page_kinds_test.go +++ b/resources/kinds/kinds_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. +// Copyright 2023 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package page +package kinds import ( "testing" @@ -29,9 +29,12 @@ func TestKind(t *testing.T) { c.Assert(KindTaxonomy, qt.Equals, "taxonomy") c.Assert(KindTerm, qt.Equals, "term") - c.Assert(GetKind("TAXONOMYTERM"), qt.Equals, KindTaxonomy) - c.Assert(GetKind("Taxonomy"), qt.Equals, KindTaxonomy) - c.Assert(GetKind("Page"), qt.Equals, KindPage) - c.Assert(GetKind("Home"), qt.Equals, KindHome) - c.Assert(GetKind("SEction"), qt.Equals, KindSection) + c.Assert(GetKindMain("TAXONOMYTERM"), qt.Equals, KindTaxonomy) + c.Assert(GetKindMain("Taxonomy"), qt.Equals, KindTaxonomy) + c.Assert(GetKindMain("Page"), qt.Equals, KindPage) + c.Assert(GetKindMain("Home"), qt.Equals, KindHome) + c.Assert(GetKindMain("SEction"), qt.Equals, KindSection) + + c.Assert(GetKindAny("Page"), qt.Equals, KindPage) + c.Assert(GetKindAny("Robotstxt"), qt.Equals, KindRobotsTXT) } diff --git a/resources/page/page_kinds.go b/resources/page/page_kinds.go index 479859841..bb846ca5b 100644 --- a/resources/page/page_kinds.go +++ b/resources/page/page_kinds.go @@ -12,36 +12,3 @@ // limitations under the License. package page - -import "strings" - -const ( - KindPage = "page" - - // The rest are node types; home page, sections etc. - - KindHome = "home" - KindSection = "section" - - // Note that before Hugo 0.73 these were confusingly named - // taxonomy (now: term) - // taxonomyTerm (now: taxonomy) - KindTaxonomy = "taxonomy" - KindTerm = "term" -) - -var kindMap = map[string]string{ - strings.ToLower(KindPage): KindPage, - strings.ToLower(KindHome): KindHome, - strings.ToLower(KindSection): KindSection, - strings.ToLower(KindTaxonomy): KindTaxonomy, - strings.ToLower(KindTerm): KindTerm, - - // Legacy, pre v0.53.0. - "taxonomyterm": KindTaxonomy, -} - -// GetKind gets the page kind given a string, empty if not found. -func GetKind(s string) string { - return kindMap[strings.ToLower(s)] -} diff --git a/resources/page/page_matcher.go b/resources/page/page_matcher.go index a2b327f61..4c861cbd7 100644 --- a/resources/page/page_matcher.go +++ b/resources/page/page_matcher.go @@ -21,6 +21,7 @@ import ( "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/hugofs/glob" + "github.com/gohugoio/hugo/resources/kinds" "github.com/mitchellh/mapstructure" ) @@ -173,7 +174,7 @@ func decodePageMatcher(m any, v *PageMatcher) error { if v.Kind != "" { g, _ := glob.GetGlob(v.Kind) found := false - for _, k := range kindMap { + for _, k := range kinds.AllKindsInPages { if g.Match(k) { found = true break diff --git a/resources/page/page_paths.go b/resources/page/page_paths.go index 0ed758ca1..1bc16fe35 100644 --- a/resources/page/page_paths.go +++ b/resources/page/page_paths.go @@ -21,6 +21,7 @@ import ( "github.com/gohugoio/hugo/common/urls" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/output" + "github.com/gohugoio/hugo/resources/kinds" ) const slash = "/" @@ -147,7 +148,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) { isUgly = true } - if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 { + if d.Kind != kinds.KindPage && d.URL == "" && len(d.Sections) > 0 { if d.ExpandedPermalink != "" { pagePath = pjoin(pagePath, d.ExpandedPermalink) } else { @@ -160,7 +161,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) { pagePath = pjoin(pagePath, d.Type.Path) } - if d.Kind != KindHome && d.URL != "" { + if d.Kind != kinds.KindHome && d.URL != "" { pagePath = pjoin(pagePath, d.URL) if d.Addends != "" { @@ -200,7 +201,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) { } } - } else if d.Kind == KindPage { + } else if d.Kind == kinds.KindPage { if d.ExpandedPermalink != "" { pagePath = pjoin(pagePath, d.ExpandedPermalink) @@ -307,7 +308,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) { // if page URL is explicitly set in frontmatter, // preserve its value without sanitization - if d.Kind != KindPage || d.URL == "" { + if d.Kind != kinds.KindPage || d.URL == "" { // Note: MakePathSanitized will lower case the path if // disablePathToLower isn't set. pagePath = d.PathSpec.MakePathSanitized(pagePath) diff --git a/resources/page/page_paths_test.go b/resources/page/page_paths_test.go index 137f3bec3..dd6457f77 100644 --- a/resources/page/page_paths_test.go +++ b/resources/page/page_paths_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/gohugoio/hugo/media" + "github.com/gohugoio/hugo/resources/kinds" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/output" @@ -47,31 +48,31 @@ func TestPageTargetPath(t *testing.T) { d page.TargetPathDescriptor expected page.TargetPaths }{ - {"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"}}, + {"JSON home", page.TargetPathDescriptor{Kind: kinds.KindHome, Type: output.JSONFormat}, page.TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}}, + {"AMP home", page.TargetPathDescriptor{Kind: kinds.KindHome, Type: output.AMPFormat}, page.TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}}, + {"HTML home", page.TargetPathDescriptor{Kind: kinds.KindHome, BaseName: "_index", Type: output.HTMLFormat}, page.TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}}, + {"Netlify redirects", page.TargetPathDescriptor{Kind: kinds.KindHome, BaseName: "_index", Type: noExtDelimFormat}, page.TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}}, {"HTML section list", page.TargetPathDescriptor{ - Kind: page.KindSection, + Kind: kinds.KindSection, Sections: []string{"sect1"}, BaseName: "_index", Type: output.HTMLFormat, }, page.TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}}, {"HTML taxonomy term", page.TargetPathDescriptor{ - Kind: page.KindTerm, + Kind: kinds.KindTerm, Sections: []string{"tags", "hugo"}, BaseName: "_index", Type: output.HTMLFormat, }, page.TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}}, {"HTML taxonomy", page.TargetPathDescriptor{ - Kind: page.KindTaxonomy, + Kind: kinds.KindTaxonomy, Sections: []string{"tags"}, BaseName: "_index", Type: output.HTMLFormat, }, page.TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}}, { "HTML page", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b", BaseName: "mypage", Sections: []string{"a"}, @@ -81,7 +82,7 @@ func TestPageTargetPath(t *testing.T) { { "HTML page with index as base", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b", BaseName: "index", Sections: []string{"a"}, @@ -91,7 +92,7 @@ func TestPageTargetPath(t *testing.T) { { "HTML page with special chars", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b", BaseName: "My Page!", Type: output.HTMLFormat, @@ -105,7 +106,7 @@ func TestPageTargetPath(t *testing.T) { }, page.TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}}, { "AMP page", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b/c", BaseName: "myamp", Type: output.AMPFormat, @@ -113,7 +114,7 @@ func TestPageTargetPath(t *testing.T) { }, { "AMP page with URL with suffix", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/sect/", BaseName: "mypage", URL: "/some/other/url.xhtml", @@ -122,7 +123,7 @@ func TestPageTargetPath(t *testing.T) { }, { "JSON page with URL without suffix", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/sect/", BaseName: "mypage", URL: "/some/other/path/", @@ -131,7 +132,7 @@ func TestPageTargetPath(t *testing.T) { }, { "JSON page with URL without suffix and no trailing slash", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/sect/", BaseName: "mypage", URL: "/some/other/path", @@ -140,7 +141,7 @@ func TestPageTargetPath(t *testing.T) { }, { "HTML page with URL without suffix and no trailing slash", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/sect/", BaseName: "mypage", URL: "/some/other/path", @@ -149,7 +150,7 @@ func TestPageTargetPath(t *testing.T) { }, { "HTML page with URL containing double hyphen", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/sect/", BaseName: "mypage", URL: "/some/other--url/", @@ -158,7 +159,7 @@ func TestPageTargetPath(t *testing.T) { }, { "HTML page with expanded permalink", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b", BaseName: "mypage", ExpandedPermalink: "/2017/10/my-title/", @@ -167,7 +168,7 @@ func TestPageTargetPath(t *testing.T) { }, { "Paginated HTML home", page.TargetPathDescriptor{ - Kind: page.KindHome, + Kind: kinds.KindHome, BaseName: "_index", Type: output.HTMLFormat, Addends: "page/3", @@ -175,7 +176,7 @@ func TestPageTargetPath(t *testing.T) { }, { "Paginated Taxonomy terms list", page.TargetPathDescriptor{ - Kind: page.KindTerm, + Kind: kinds.KindTerm, BaseName: "_index", Sections: []string{"tags", "hugo"}, Type: output.HTMLFormat, @@ -184,7 +185,7 @@ func TestPageTargetPath(t *testing.T) { }, { "Regular page with addend", page.TargetPathDescriptor{ - Kind: page.KindPage, + Kind: kinds.KindPage, Dir: "/a/b", BaseName: "mypage", Addends: "c/d/e", @@ -207,8 +208,8 @@ func TestPageTargetPath(t *testing.T) { expected := test.expected // TODO(bep) simplify - if test.d.Kind == page.KindPage && test.d.BaseName == test.d.Type.BaseName { - } else if test.d.Kind == page.KindHome && test.d.Type.Path != "" { + if test.d.Kind == kinds.KindPage && test.d.BaseName == test.d.Type.BaseName { + } else if test.d.Kind == kinds.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, @@ -250,12 +251,12 @@ func TestPageTargetPathPrefix(t *testing.T) { }{ { "URL set, prefix both, no force", - page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"}, + page.TargetPathDescriptor{Kind: kinds.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", - page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"}, + page.TargetPathDescriptor{Kind: kinds.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"}, }, } diff --git a/resources/page/permalinks.go b/resources/page/permalinks.go index 3b12e1154..7fc55eccb 100644 --- a/resources/page/permalinks.go +++ b/resources/page/permalinks.go @@ -27,6 +27,7 @@ import ( "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/helpers" + "github.com/gohugoio/hugo/resources/kinds" ) // PermalinkExpander holds permalin mappings per section. @@ -398,16 +399,16 @@ func (l PermalinkExpander) toSliceFunc(cut string) func(s []string) []string { } -var permalinksKindsSuppurt = []string{KindPage, KindSection, KindTaxonomy, KindTerm} +var permalinksKindsSuppurt = []string{kinds.KindPage, kinds.KindSection, kinds.KindTaxonomy, kinds.KindTerm} // DecodePermalinksConfig decodes the permalinks configuration in the given map func DecodePermalinksConfig(m map[string]any) (map[string]map[string]string, error) { permalinksConfig := make(map[string]map[string]string) - permalinksConfig[KindPage] = make(map[string]string) - permalinksConfig[KindSection] = make(map[string]string) - permalinksConfig[KindTaxonomy] = make(map[string]string) - permalinksConfig[KindTerm] = make(map[string]string) + permalinksConfig[kinds.KindPage] = make(map[string]string) + permalinksConfig[kinds.KindSection] = make(map[string]string) + permalinksConfig[kinds.KindTaxonomy] = make(map[string]string) + permalinksConfig[kinds.KindTerm] = make(map[string]string) config := maps.CleanConfigStringMap(m) for k, v := range config { @@ -417,8 +418,8 @@ func DecodePermalinksConfig(m map[string]any) (map[string]map[string]string, err // key = '...' // To sucessfully be backward compatible, "default" patterns need to be set for both page and term - permalinksConfig[KindPage][k] = v - permalinksConfig[KindTerm][k] = v + permalinksConfig[kinds.KindPage][k] = v + permalinksConfig[kinds.KindTerm][k] = v case maps.Params: // [permalinks.key]