Skip to content

Commit

Permalink
Move all Kind constants to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 28, 2023
1 parent 36b5126 commit c91297b
Show file tree
Hide file tree
Showing 37 changed files with 390 additions and 327 deletions.
11 changes: 6 additions & 5 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion create/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ site RegularPages: {{ len site.RegularPages }}
c.Assert(create.NewContent(h, "", "post/my-post2", false), qt.IsNil)
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post2/index.md")), `default archetype index.md`)

// Regular file with bundle kind.
// Regular file with bundle kinds.
c.Assert(create.NewContent(h, "my-bundle", "post/foo.md", false), qt.IsNil)
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/foo.md")), `draft: true`)

Expand Down
7 changes: 4 additions & 3 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
}

Expand Down
12 changes: 7 additions & 5 deletions hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path"
"path/filepath"
"strings"

"sync"

"github.com/gohugoio/hugo/common/maps"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
19 changes: 10 additions & 9 deletions hugolib/disableKinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/gohugoio/hugo/resources/page"
)

Expand Down Expand Up @@ -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{})
Expand All @@ -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{})
Expand All @@ -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{})
Expand All @@ -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{})
Expand All @@ -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{})
Expand All @@ -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{})
Expand All @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 19 additions & 19 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")

Expand Down Expand Up @@ -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*")
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c91297b

Please sign in to comment.