Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc validation and deprecation #11297

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/loggers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type Logger interface {
WarnCommand(command string) logg.LevelLogger
Warnf(format string, v ...any)
Warnln(v ...any)
Deprecatef(fail bool, format string, v ...any)
}

type logAdapter struct {
Expand Down Expand Up @@ -297,6 +298,15 @@ func (l *logAdapter) sprint(v ...any) string {
return strings.TrimRight(fmt.Sprintln(v...), "\n")
}

func (l *logAdapter) Deprecatef(fail bool, format string, v ...any) {
format = "DEPRECATED: " + format
if fail {
l.errorl.Logf(format, v...)
} else {
l.warnl.Logf(format, v...)
}
}

type logWriter struct {
l logg.LevelLogger
}
Expand Down
28 changes: 21 additions & 7 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 @@ -239,19 +240,32 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
disabledKinds := make(map[string]bool)
for _, kind := range c.DisableKinds {
kind = strings.ToLower(kind)
if kind == "taxonomyterm" {
if newKind := kinds.IsDeprecatedAndReplacedWith(kind); newKind != "" {
logger.Deprecatef(false, "Kind %q used in disableKinds is deprecated, use %q instead.", kind, newKind)
// Legacy config.
kind = "taxonomy"
kind = newKind
}
if kinds.GetKindAny(kind) == "" {
logger.Warnf("Unknown kind %q in disableKinds configuration.", kind)
continue
}
disabledKinds[kind] = true
}
kindOutputFormats := make(map[string]output.Formats)
isRssDisabled := disabledKinds["rss"]
outputFormats := c.OutputFormats.Config
for kind, formats := range c.Outputs {
if newKind := kinds.IsDeprecatedAndReplacedWith(kind); newKind != "" {
logger.Deprecatef(false, "Kind %q used in outputs configuration is deprecated, use %q instead.", kind, newKind)
kind = newKind
}
if disabledKinds[kind] {
continue
}
if kinds.GetKindAny(kind) == "" {
logger.Warnf("Unknown kind %q in outputs configuration.", kind)
continue
}
for _, format := range formats {
if isRssDisabled && format == "rss" {
// Legacy config.
Expand Down Expand Up @@ -940,11 +954,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
55 changes: 55 additions & 0 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,3 +1611,58 @@ List.
b.AssertDestinationExists("categories/index.html", false)

}

func TestKindsUnknown(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
disableKinds = ['foo', 'home']
[outputs]
foo = ['HTML', 'AMP', 'RSS']
-- layouts/_default/list.html --
List.



`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
LogLevel: logg.LevelWarn,
},
).Init()

b.AssertLogContains("WARN Unknown kind \"foo\" in disableKinds configuration.\n")
b.AssertLogContains("WARN Unknown kind \"foo\" in outputs configuration.\n")

}

func TestDeprecateTaxonomyTerm(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
disableKinds = ['taxonomyTerm']
[outputs]
taxonomyterm = ['HTML', 'AMP', 'RSS']
-- layouts/_default/list.html --
List.



`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
LogLevel: logg.LevelWarn,
BuildCfg: BuildCfg{SkipRender: true},
},
).Init()

b.AssertLogContains("WARN DEPRECATED: Kind \"taxonomyterm\" used in disableKinds is deprecated, use \"taxonomy\" instead.\n")
b.AssertLogContains("WARN DEPRECATED: Kind \"taxonomyterm\" used in outputs configuration is deprecated, use \"taxonomy\" instead.\n")

}
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
Loading
Loading