mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
node to page: Make Kind a string
Having a custom string type isn't worth it when it doesn't work with `where`, `eq` etc. Fixes #2689 Updates #2297
This commit is contained in:
parent
9fba2a30a9
commit
9347084d61
7 changed files with 57 additions and 61 deletions
|
@ -192,7 +192,7 @@ func (h *HugoSites) renderCrossSitesArtifacts() error {
|
|||
func (h *HugoSites) assignMissingTranslations() error {
|
||||
// This looks heavy, but it should be a small number of nodes by now.
|
||||
allPages := h.findAllPagesByNodeTypeNotIn(KindPage)
|
||||
for _, nodeType := range []Kind{KindHome, KindSection, KindTaxonomy, KindTaxonomyTerm} {
|
||||
for _, nodeType := range []string{KindHome, KindSection, KindTaxonomy, KindTaxonomyTerm} {
|
||||
nodes := h.findPagesByNodeTypeIn(nodeType, allPages)
|
||||
|
||||
// Assign translations
|
||||
|
@ -304,7 +304,7 @@ func (h *HugoSites) createMissingPages() error {
|
|||
|
||||
// TODO(bep) np move
|
||||
// Move the new* methods after cleanup in site.go
|
||||
func (s *Site) newNodePage(typ Kind) *Page {
|
||||
func (s *Site) newNodePage(typ string) *Page {
|
||||
return &Page{
|
||||
Kind: typ,
|
||||
Node: Node{
|
||||
|
@ -566,19 +566,19 @@ func (s *Site) updateBuildStats(page *Page) {
|
|||
}
|
||||
|
||||
// TODO(bep) np remove
|
||||
func (h *HugoSites) findAllPagesByNodeType(n Kind) Pages {
|
||||
func (h *HugoSites) findAllPagesByNodeType(n string) Pages {
|
||||
return h.Sites[0].findAllPagesByNodeType(n)
|
||||
}
|
||||
|
||||
func (h *HugoSites) findPagesByNodeTypeNotIn(n Kind, inPages Pages) Pages {
|
||||
func (h *HugoSites) findPagesByNodeTypeNotIn(n string, inPages Pages) Pages {
|
||||
return h.Sites[0].findPagesByNodeTypeNotIn(n, inPages)
|
||||
}
|
||||
|
||||
func (h *HugoSites) findPagesByNodeTypeIn(n Kind, inPages Pages) Pages {
|
||||
func (h *HugoSites) findPagesByNodeTypeIn(n string, inPages Pages) Pages {
|
||||
return h.Sites[0].findPagesByNodeTypeIn(n, inPages)
|
||||
}
|
||||
|
||||
func (h *HugoSites) findAllPagesByNodeTypeNotIn(n Kind) Pages {
|
||||
func (h *HugoSites) findAllPagesByNodeTypeNotIn(n string) Pages {
|
||||
return h.findPagesByNodeTypeNotIn(n, h.Sites[0].AllPages)
|
||||
}
|
||||
|
||||
|
|
|
@ -291,7 +291,7 @@ func sectionsFromFilename(filename string) []string {
|
|||
}
|
||||
|
||||
// TODO(bep) np node identificator
|
||||
func nodeTypeFromFilename(filename string) Kind {
|
||||
func kindFromFilename(filename string) string {
|
||||
if !strings.Contains(filename, "_index") {
|
||||
return KindPage
|
||||
}
|
||||
|
|
|
@ -49,53 +49,35 @@ var (
|
|||
cjk = regexp.MustCompile(`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`)
|
||||
)
|
||||
|
||||
// Kind is the discriminator that identifies the different page types
|
||||
// in the different page collections. This can, as an example, be used
|
||||
// to to filter regular pages, find sections etc.
|
||||
// NOTE: THe exported constants below are used to filter pages from
|
||||
// templates in the wild, so do not change the values!
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
KindPage Kind = "page"
|
||||
KindPage = "page"
|
||||
|
||||
// The rest are node types; home page, sections etc.
|
||||
KindHome Kind = "home"
|
||||
KindSection Kind = "section"
|
||||
KindTaxonomy Kind = "taxonomy"
|
||||
KindTaxonomyTerm Kind = "taxonomyTerm"
|
||||
KindHome = "home"
|
||||
KindSection = "section"
|
||||
KindTaxonomy = "taxonomy"
|
||||
KindTaxonomyTerm = "taxonomyTerm"
|
||||
|
||||
// Temporary state.
|
||||
kindUnknown Kind = "unknown"
|
||||
kindUnknown = "unknown"
|
||||
|
||||
// The following are (currently) temporary nodes,
|
||||
// i.e. nodes we create just to render in isolation.
|
||||
kindSitemap Kind = "sitemap"
|
||||
kindRobotsTXT Kind = "robotsTXT"
|
||||
kind404 Kind = "404"
|
||||
kindSitemap = "sitemap"
|
||||
kindRobotsTXT = "robotsTXT"
|
||||
kind404 = "404"
|
||||
)
|
||||
|
||||
// IsNode returns whether this is an item of one of the list types in Hugo,
|
||||
// i.e. not a regular content page.
|
||||
func (k Kind) IsNode() bool {
|
||||
return k != KindPage
|
||||
}
|
||||
|
||||
// IsHome returns whether this is the home page.
|
||||
func (k Kind) IsHome() bool {
|
||||
return k == KindHome
|
||||
}
|
||||
|
||||
// IsPage returns whether this is a regular content page.
|
||||
func (k Kind) IsPage() bool {
|
||||
return k == KindPage
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
|
||||
// Kind is the discriminator that identifies the different page types
|
||||
// in the different page collections. This can, as an example, be used
|
||||
// to to filter regular pages, find sections etc.
|
||||
// Kind will, for the pages available to the templates, be one of:
|
||||
// page, home, section, taxonomy and taxonomyTerm.
|
||||
Kind
|
||||
// It is of string type to make it easy to reason about in
|
||||
// the templates.
|
||||
Kind string
|
||||
|
||||
// Since Hugo 0.18 we got rid of the Node type. So now all pages are ...
|
||||
// pages (regular pages, home page, sections etc.).
|
||||
|
@ -171,6 +153,22 @@ type Page struct {
|
|||
site *Site
|
||||
}
|
||||
|
||||
// IsNode returns whether this is an item of one of the list types in Hugo,
|
||||
// i.e. not a regular content page.
|
||||
func (p *Page) IsNode() bool {
|
||||
return p.Kind != KindPage
|
||||
}
|
||||
|
||||
// IsHome returns whether this is the home page.
|
||||
func (p *Page) IsHome() bool {
|
||||
return p.Kind == KindHome
|
||||
}
|
||||
|
||||
// IsPage returns whether this is a regular content page.
|
||||
func (p *Page) IsPage() bool {
|
||||
return p.Kind == KindPage
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
Frontmatter []byte
|
||||
Content []byte
|
||||
|
@ -485,7 +483,7 @@ func (p *Page) getRenderingConfig() *helpers.Blackfriday {
|
|||
|
||||
func newPage(filename string) *Page {
|
||||
page := Page{
|
||||
Kind: nodeTypeFromFilename(filename),
|
||||
Kind: kindFromFilename(filename),
|
||||
contentType: "",
|
||||
Source: Source{File: *source.NewFile(filename)},
|
||||
Node: Node{Keywords: []string{}, Sitemap: Sitemap{Priority: -1}},
|
||||
|
@ -1378,7 +1376,7 @@ func (p *Page) updatePageDates() {
|
|||
// TODO(bep) np there is a potential issue with page sorting for home pages
|
||||
// etc. without front matter dates set, but let us wrap the head around
|
||||
// that in another time.
|
||||
if !p.Kind.IsNode() {
|
||||
if !p.IsNode() {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -61,11 +61,11 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections {
|
|||
|
||||
// TODO(bep) np clean and remove finders
|
||||
|
||||
func (c *PageCollections) findPagesByNodeType(n Kind) Pages {
|
||||
func (c *PageCollections) findPagesByNodeType(n string) Pages {
|
||||
return c.findPagesByNodeTypeIn(n, c.Pages)
|
||||
}
|
||||
|
||||
func (c *PageCollections) getPage(typ Kind, path ...string) *Page {
|
||||
func (c *PageCollections) getPage(typ string, path ...string) *Page {
|
||||
pages := c.findPagesByNodeTypeIn(typ, c.Pages)
|
||||
|
||||
if len(pages) == 0 {
|
||||
|
@ -94,11 +94,11 @@ func (c *PageCollections) getPage(typ Kind, path ...string) *Page {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *PageCollections) findIndexNodesByNodeType(n Kind) Pages {
|
||||
func (c *PageCollections) findIndexNodesByNodeType(n string) Pages {
|
||||
return c.findPagesByNodeTypeIn(n, c.indexPages)
|
||||
}
|
||||
|
||||
func (*PageCollections) findPagesByNodeTypeIn(n Kind, inPages Pages) Pages {
|
||||
func (*PageCollections) findPagesByNodeTypeIn(n string, inPages Pages) Pages {
|
||||
var pages Pages
|
||||
for _, p := range inPages {
|
||||
if p.Kind == n {
|
||||
|
@ -108,7 +108,7 @@ func (*PageCollections) findPagesByNodeTypeIn(n Kind, inPages Pages) Pages {
|
|||
return pages
|
||||
}
|
||||
|
||||
func (*PageCollections) findPagesByNodeTypeNotIn(n Kind, inPages Pages) Pages {
|
||||
func (*PageCollections) findPagesByNodeTypeNotIn(n string, inPages Pages) Pages {
|
||||
var pages Pages
|
||||
for _, p := range inPages {
|
||||
if p.Kind != n {
|
||||
|
@ -118,11 +118,11 @@ func (*PageCollections) findPagesByNodeTypeNotIn(n Kind, inPages Pages) Pages {
|
|||
return pages
|
||||
}
|
||||
|
||||
func (c *PageCollections) findAllPagesByNodeType(n Kind) Pages {
|
||||
func (c *PageCollections) findAllPagesByNodeType(n string) Pages {
|
||||
return c.findPagesByNodeTypeIn(n, c.Pages)
|
||||
}
|
||||
|
||||
func (c *PageCollections) findRawAllPagesByNodeType(n Kind) Pages {
|
||||
func (c *PageCollections) findRawAllPagesByNodeType(n string) Pages {
|
||||
return c.findPagesByNodeTypeIn(n, c.rawAllPages)
|
||||
}
|
||||
|
||||
|
|
|
@ -1278,17 +1278,15 @@ func TestIndexPageSimpleMethods(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPageType(t *testing.T) {
|
||||
func TestKind(t *testing.T) {
|
||||
|
||||
// Add tests for these constants to make sure they don't change
|
||||
require.Equal(t, Kind("page"), KindPage)
|
||||
require.Equal(t, Kind("home"), KindHome)
|
||||
require.Equal(t, Kind("section"), KindSection)
|
||||
require.Equal(t, Kind("taxonomy"), KindTaxonomy)
|
||||
require.Equal(t, Kind("taxonomyTerm"), KindTaxonomyTerm)
|
||||
require.Equal(t, "page", KindPage)
|
||||
require.Equal(t, "home", KindHome)
|
||||
require.Equal(t, "section", KindSection)
|
||||
require.Equal(t, "taxonomy", KindTaxonomy)
|
||||
require.Equal(t, "taxonomyTerm", KindTaxonomyTerm)
|
||||
|
||||
require.False(t, KindPage.IsNode())
|
||||
require.True(t, KindHome.IsNode())
|
||||
}
|
||||
|
||||
func TestChompBOM(t *testing.T) {
|
||||
|
|
|
@ -260,7 +260,7 @@ func splitPageGroups(pageGroups PagesGroup, size int) []paginatedElement {
|
|||
// Paginator gets this Node's paginator if it's already created.
|
||||
// If it's not, one will be created with all pages in Data["Pages"].
|
||||
func (n *Page) Paginator(options ...interface{}) (*Pager, error) {
|
||||
if !n.Kind.IsNode() {
|
||||
if !n.IsNode() {
|
||||
return nil, fmt.Errorf("Paginators not supported for pages of type %q (%q)", n.Kind, n.Title)
|
||||
}
|
||||
pagerSize, err := resolvePagerSize(options...)
|
||||
|
@ -303,7 +303,7 @@ func (n *Page) Paginator(options ...interface{}) (*Pager, error) {
|
|||
// If it's not, one will be created with the qiven sequence.
|
||||
// Note that repeated calls will return the same result, even if the sequence is different.
|
||||
func (n *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error) {
|
||||
if !n.Kind.IsNode() {
|
||||
if !n.IsNode() {
|
||||
return nil, fmt.Errorf("Paginators not supported for pages of type %q (%q)", n.Kind, n.Title)
|
||||
}
|
||||
|
||||
|
|
|
@ -1561,7 +1561,7 @@ func (s *Site) assembleSections() {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Site) nodeTypeFromSections(sections []string) Kind {
|
||||
func (s *Site) nodeTypeFromSections(sections []string) string {
|
||||
if _, isTaxonomy := s.Taxonomies[sections[0]]; isTaxonomy {
|
||||
if len(sections) == 1 {
|
||||
return KindTaxonomyTerm
|
||||
|
@ -1662,7 +1662,7 @@ func (s *Site) Stats() {
|
|||
// This will return nil when no page could be found.
|
||||
//
|
||||
// The valid page types are: home, section, taxonomy and taxonomyTerm
|
||||
func (s *SiteInfo) GetPage(typ Kind, path ...string) *Page {
|
||||
func (s *SiteInfo) GetPage(typ string, path ...string) *Page {
|
||||
return s.getPage(typ, path...)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue