Skip to content

Commit

Permalink
Implement cascade support in front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 2, 2019
1 parent 9ef4dca commit 05b7f0d
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 284 deletions.
1 change: 1 addition & 0 deletions hugolib/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ tags_weight: %d
b.CreateSites().Build(BuildCfg{})

assert.Equal(1, len(b.H.Sites))

require.Len(t, b.H.Sites[0].RegularPages(), 2)

b.AssertFileContent("public/index.html",
Expand Down
183 changes: 2 additions & 181 deletions hugolib/disableKinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,190 +13,11 @@
package hugolib

import (
"strings"
"testing"

"fmt"

"github.com/gohugoio/hugo/resources/page"

"github.com/gohugoio/hugo/helpers"
"github.com/stretchr/testify/require"
)

func TestDisableKindsNoneDisabled(t *testing.T) {
t.Parallel()
doTestDisableKinds(t)
}

func TestDisableKindsSomeDisabled(t *testing.T) {
t.Parallel()
doTestDisableKinds(t, page.KindSection, kind404)
}

func TestDisableKindsOneDisabled(t *testing.T) {
t.Parallel()
for _, kind := range allKinds {
if kind == page.KindPage {
// Turning off regular page generation have some side-effects
// not handled by the assertions below (no sections), so
// skip that for now.
continue
}
doTestDisableKinds(t, kind)
}
}

func TestDisableKindsAllDisabled(t *testing.T) {
// TODO(bep) cascade add some simpler tests.
func TestDisableKinds(t *testing.T) {
t.Parallel()
doTestDisableKinds(t, allKinds...)
}

func doTestDisableKinds(t *testing.T, disabled ...string) {
siteConfigTemplate := `
baseURL = "http://example.com/blog"
enableRobotsTXT = true
disableKinds = %s
paginate = 1
defaultContentLanguage = "en"
[Taxonomies]
tag = "tags"
category = "categories"
`

pageTemplate := `---
title: "%s"
tags:
%s
categories:
- Hugo
---
# Doc
`

disabledStr := "[]"

if len(disabled) > 0 {
disabledStr = strings.Replace(fmt.Sprintf("%#v", disabled), "[]string{", "[", -1)
disabledStr = strings.Replace(disabledStr, "}", "]", -1)
}

siteConfig := fmt.Sprintf(siteConfigTemplate, disabledStr)

b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig)

b.WithTemplates(
"index.html", "Home|{{ .Title }}|{{ .Content }}",
"_default/single.html", "Single|{{ .Title }}|{{ .Content }}",
"_default/list.html", "List|{{ .Title }}|{{ .Content }}",
"_default/terms.html", "Terms List|{{ .Title }}|{{ .Content }}",
"layouts/404.html", "Page Not Found",
)

b.WithContent(
"sect/p1.md", fmt.Sprintf(pageTemplate, "P1", "- tag1"),
"categories/_index.md", newTestPage("Category Terms", "2017-01-01", 10),
"tags/tag1/_index.md", newTestPage("Tag1 List", "2017-01-01", 10),
)

b.Build(BuildCfg{})
h := b.H

require.Len(t, h.Sites, 1)

assertDisabledKinds(b, h.Sites[0], disabled...)

}

func assertDisabledKinds(b *sitesBuilder, s *Site, disabled ...string) {
assertDisabledKind(b,
func(isDisabled bool) bool {
if isDisabled {
return len(s.RegularPages()) == 0
}
return len(s.RegularPages()) > 0
}, disabled, page.KindPage, "public/sect/p1/index.html", "Single|P1")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindHome)
if isDisabled {
return p == nil
}
return p != nil
}, disabled, page.KindHome, "public/index.html", "Home")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindSection, "sect")
if isDisabled {
return p == nil
}
return p != nil
}, disabled, page.KindSection, "public/sect/index.html", "Sects")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindTaxonomy, "tags", "tag1")

if isDisabled {
return p == nil
}
return p != nil

}, disabled, page.KindTaxonomy, "public/tags/tag1/index.html", "Tag1")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindTaxonomyTerm, "tags")
if isDisabled {
return p == nil
}
return p != nil

}, disabled, page.KindTaxonomyTerm, "public/tags/index.html", "Tags")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindTaxonomyTerm, "categories")

if isDisabled {
return p == nil
}
return p != nil

}, disabled, page.KindTaxonomyTerm, "public/categories/index.html", "Category Terms")
assertDisabledKind(b,
func(isDisabled bool) bool {
p := s.getPage(page.KindTaxonomy, "categories", "hugo")
if isDisabled {
return p == nil
}
return p != nil

}, disabled, page.KindTaxonomy, "public/categories/hugo/index.html", "Hugo")
// The below have no page in any collection.
assertDisabledKind(b, func(isDisabled bool) bool { return true }, disabled, kindRSS, "public/index.xml", "<link>")
assertDisabledKind(b, func(isDisabled bool) bool { return true }, disabled, kindSitemap, "public/sitemap.xml", "sitemap")
assertDisabledKind(b, func(isDisabled bool) bool { return true }, disabled, kindRobotsTXT, "public/robots.txt", "User-agent")
assertDisabledKind(b, func(isDisabled bool) bool { return true }, disabled, kind404, "public/404.html", "Page Not Found")
}

func assertDisabledKind(b *sitesBuilder, kindAssert func(bool) bool, disabled []string, kind, path, matcher string) {
isDisabled := stringSliceContains(kind, disabled...)
require.True(b.T, kindAssert(isDisabled), fmt.Sprintf("%s: %t", kind, isDisabled))

if kind == kindRSS && !isDisabled {
// If the home page is also disabled, there is not RSS to look for.
if stringSliceContains(page.KindHome, disabled...) {
isDisabled = true
}
}

if isDisabled {
// Path should not exist
fileExists, err := helpers.Exists(path, b.Fs.Destination)
require.False(b.T, fileExists)
require.NoError(b.T, err)

} else {
b.AssertFileContent(path, matcher)
}
}
122 changes: 91 additions & 31 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/langs/i18n"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/tpl"
"github.com/gohugoio/hugo/tpl/tplimpl"
)
Expand Down Expand Up @@ -623,31 +624,69 @@ func (h *HugoSites) renderCrossSitesArtifacts() error {
s.siteCfg.sitemap.Filename, h.toSiteInfos(), smLayouts...)
}

// createMissingPages creates home page, taxonomies etc. that isnt't created as an
// effect of having a content file.
func (h *HugoSites) createMissingPages() error {

// assignMetaData parses any front matter stored away and merges it with any
// cascading data available.
// TODO(bep) cascade workers
func (h *HugoSites) assignMetaData() error {
for _, s := range h.Sites {
if s.isEnabled(page.KindHome) {
// home pages
homes := s.findWorkPagesByKind(page.KindHome)
if len(homes) > 1 {
panic("Too many homes")
}
var home *pageState
if len(homes) == 0 {
home = s.newPage(page.KindHome)
s.workAllPages = append(s.workAllPages, home)
} else {
home = homes[0]
section := s.home
if section == nil {
panic("no home set")
}

ma := &pagesMetadataHandler{dates: &resource.Dates{}}

if err := ma.handleSection(nil, section); err != nil {
return err
}

if ma.dates != nil {
section.m.Dates = *ma.dates
}

// The headless pages currently lives outside the main tree, which
// is unfortunate.
// TODO(bep) improve
for _, p := range s.headlessPages {
if _, err := ma.parseAndAssignMeta(false, nil, p); err != nil {
return err
}
}
}

return nil
}

s.home = home
// Will create sections (including home) with not content page.
func (h *HugoSites) createMissingSectionPages() error {
for _, s := range h.Sites {
// home pages
homes := s.findWorkPagesByKind(page.KindHome)
if len(homes) > 1 {
panic("Too many homes")
}
var home *pageState
if len(homes) == 0 {
home = s.newPage(page.KindHome)
s.workAllPages = append(s.workAllPages, home)
} else {
home = homes[0]
}

s.home = home

// Will create content-less root sections.
newSections := s.assembleSections()
s.workAllPages = append(s.workAllPages, newSections...)
}

return nil
}

// Will create taxonomy term/list pages with not content page.
func (h *HugoSites) createMissingTaxonomyPages() error {

for _, s := range h.Sites {

taxonomyTermEnabled := s.isEnabled(page.KindTaxonomyTerm)
taxonomyEnabled := s.isEnabled(page.KindTaxonomy)
Expand Down Expand Up @@ -741,25 +780,46 @@ func (h *HugoSites) removePageByFilename(filename string) {
}
}

func (h *HugoSites) createPageCollections() error {
func (h *HugoSites) removeNoBuildPages() error {
for _, s := range h.Sites {
for _, p := range s.rawAllPages {
if !s.isEnabled(p.Kind()) {
continue
}
s.workAllPages = s.filterNoBuildPages(s.workAllPages)
s.workAllPages, s.headlessPages = s.filterHeadlessPages(s.workAllPages)

shouldBuild := s.shouldBuild(p)
s.buildStats.update(p)
if shouldBuild {
if p.m.headless {
s.headlessPages = append(s.headlessPages, p)
} else {
s.workAllPages = append(s.workAllPages, p)
}
}
}

return nil
}

func (s *Site) filterNoBuildPages(pages pageStatePages) pageStatePages {
tmp := pages[:0]
for _, p := range pages {
if s.shouldBuild(p) {
tmp = append(tmp, p)
}
}
return tmp
}

func (s *Site) filterHeadlessPages(pages pageStatePages) (pageStatePages, pageStatePages) {
tmp := pages[:0]
headless := make(pageStatePages, 0)
for _, p := range pages {
if p.m.headless {
headless = append(headless, p)
} else {
tmp = append(tmp, p)
}
}
return tmp, headless
}

func (h *HugoSites) createPageCollections() error {
for _, s := range h.Sites {
s.workAllPages = make(pageStatePages, len(s.rawAllPages))
copy(s.workAllPages, s.rawAllPages)
}

// TODO(bep) move these
allPages := newLazyPagesFactory(func() page.Pages {
var pages page.Pages
for _, s := range h.Sites {
Expand Down
20 changes: 18 additions & 2 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,32 @@ func (h *HugoSites) assemble(config *BuildCfg) error {
return err
}

// Create pages for the section pages etc. without content file.
if err := h.createMissingSectionPages(); err != nil {
return err
}

if config.whatChanged.source {
// TODO(bep) cascade taxo meta?
// TODO(bep) cascade consider empty sections with expired etc.
if err := h.assignMetaData(); err != nil {
return err
}

for _, s := range h.Sites {
if err := s.assembleTaxonomies(); err != nil {
return err
}
}
}

// Create pagexs for the section pages etc. without content file.
if err := h.createMissingPages(); err != nil {
// Remove drafts, future posts etc.
if err := h.removeNoBuildPages(); err != nil {
return nil
}

// Create pages for the taxonomies without a content file.
if err := h.createMissingTaxonomyPages(); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 05b7f0d

Please sign in to comment.