From 72c32b6bb5400724e172492451fa1c18723caaf2 Mon Sep 17 00:00:00 2001 From: Cathrine Paulsen Date: Wed, 23 Mar 2022 13:32:38 +0100 Subject: [PATCH 1/3] Extend PageMatcher with Environment keyword Fixes #9612 --- hugolib/cascade_test.go | 26 ++++++++++++++++++++++++++ hugolib/page__meta.go | 4 ++++ hugolib/site.go | 4 ++++ resources/page/page.go | 3 +++ resources/page/page_matcher.go | 10 ++++++++++ resources/page/page_matcher_test.go | 15 ++++++++++++++- resources/page/page_nop.go | 4 ++++ resources/page/testhelpers_test.go | 5 +++++ 8 files changed, 70 insertions(+), 1 deletion(-) diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go index c218aa28263..0a7f66e6c1b 100644 --- a/hugolib/cascade_test.go +++ b/hugolib/cascade_test.go @@ -550,6 +550,32 @@ S1|p1:|p2:p2| `) }) + c.Run("slice with environment _target", func(c *qt.C) { + b := newBuilder(c) + + b.WithContent("_index.md", `+++ +title = "Home" +[[cascade]] +p1 = "p1" +[cascade._target] +path="**p1**" +environment="testing" +[[cascade]] +p2 = "p2" +[cascade._target] +kind="section" +environment="production" ++++ +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", ` +P1|p1:|p2:| +S1|p1:|p2:p2| +`) + }) + c.Run("slice with yaml _target", func(c *qt.C) { b := newBuilder(c) diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 5568aa539c9..13bef7029fa 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -167,6 +167,10 @@ func (p *pageMeta) Description() string { return p.description } +func (p *pageMeta) Environment() string { + return p.s.Environment() +} + func (p *pageMeta) Lang() string { return p.s.Lang() } diff --git a/hugolib/site.go b/hugolib/site.go index 59326cab61f..3fcfe107047 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1897,6 +1897,10 @@ func (s *Site) shouldBuild(p page.Page) bool { s.BuildDrafts, p.Draft(), p.PublishDate(), p.ExpiryDate()) } +func (s *Site) Environment() string { + return s.Info.Hugo().Environment +} + func shouldBuild(buildFuture bool, buildExpired bool, buildDrafts bool, Draft bool, publishDate time.Time, expiryDate time.Time) bool { if !(buildDrafts || !Draft) { diff --git a/resources/page/page.go b/resources/page/page.go index 5133f46fa19..74656f69e68 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -151,6 +151,9 @@ type PageMetaProvider interface { // Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag. Draft() bool + // Environment returns the environment of the Page + Environment() string + // IsHome returns whether this is the home page. IsHome() bool diff --git a/resources/page/page_matcher.go b/resources/page/page_matcher.go index 15b8ede8989..e41bf4f7644 100644 --- a/resources/page/page_matcher.go +++ b/resources/page/page_matcher.go @@ -37,6 +37,9 @@ type PageMatcher struct { // A Glob pattern matching the Page's language, e.g. "{en,sv}". Lang string + + // A Glob pattern matching the Page's Environment, e.g. "{production,development}". + Environment string } // Matches returns whether p matches this matcher. @@ -67,6 +70,13 @@ func (m PageMatcher) Matches(p Page) bool { } } + if m.Environment != "" { + g, err := glob.GetGlob(m.Environment) + if err == nil && !g.Match(p.Environment()) { + return false + } + } + return true } diff --git a/resources/page/page_matcher_test.go b/resources/page/page_matcher_test.go index 72aec5f2a64..581a9342082 100644 --- a/resources/page/page_matcher_test.go +++ b/resources/page/page_matcher_test.go @@ -23,7 +23,10 @@ import ( func TestPageMatcher(t *testing.T) { c := qt.New(t) - p1, p2, p3 := &testPage{path: "/p1", kind: "section", lang: "en"}, &testPage{path: "p2", kind: "page", lang: "no"}, &testPage{path: "p3", kind: "page", lang: "en"} + p1, p2, p3 := + &testPage{path: "/p1", kind: "section", lang: "en", environment: "development"}, + &testPage{path: "p2", kind: "page", lang: "no", environment: "production"}, + &testPage{path: "p3", kind: "page", lang: "en"} c.Run("Matches", func(c *qt.C) { m := PageMatcher{Kind: "section"} @@ -50,6 +53,16 @@ func TestPageMatcher(t *testing.T) { c.Assert(m.Matches(p1), qt.Equals, true) c.Assert(m.Matches(p2), qt.Equals, false) c.Assert(m.Matches(p3), qt.Equals, true) + + m = PageMatcher{Environment: "development"} + c.Assert(m.Matches(p1), qt.Equals, true) + c.Assert(m.Matches(p2), qt.Equals, false) + c.Assert(m.Matches(p3), qt.Equals, false) + + m = PageMatcher{Environment: "production"} + c.Assert(m.Matches(p1), qt.Equals, false) + c.Assert(m.Matches(p2), qt.Equals, true) + c.Assert(m.Matches(p3), qt.Equals, false) }) c.Run("Decode", func(c *qt.C) { diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go index 67b8b8d4b09..1d3440b5a34 100644 --- a/resources/page/page_nop.go +++ b/resources/page/page_nop.go @@ -120,6 +120,10 @@ func (p *nopPage) Description() string { return "" } +func (p *nopPage) Environment() string { + return "" +} + func (p *nopPage) RefFrom(argsm map[string]any, source any) (string, error) { return "", nil } diff --git a/resources/page/testhelpers_test.go b/resources/page/testhelpers_test.go index 6758667996e..21bbebdca7c 100644 --- a/resources/page/testhelpers_test.go +++ b/resources/page/testhelpers_test.go @@ -94,6 +94,7 @@ type testPage struct { linkTitle string lang string section string + environment string content string @@ -168,6 +169,10 @@ func (p *testPage) Data() any { return p.data } +func (p *testPage) Environment() string { + return p.environment +} + func (p *testPage) Sitemap() config.Sitemap { return config.Sitemap{} } From ac427b7d243ccc691d38f481667ab1e05b88b9ff Mon Sep 17 00:00:00 2001 From: Cathrine Paulsen Date: Thu, 24 Mar 2022 16:45:52 +0100 Subject: [PATCH 2/3] Add documentation for environment cascade._target keyword --- docs/content/en/content-management/front-matter.md | 3 +++ resources/page/page.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/content/en/content-management/front-matter.md b/docs/content/en/content-management/front-matter.md index 6986f067fa6..cac4ef9525e 100644 --- a/docs/content/en/content-management/front-matter.md +++ b/docs/content/en/content-management/front-matter.md @@ -189,6 +189,9 @@ kind lang : A Glob pattern matching the Page's language, e.g. "{en,sv}". +environment +: A Glob pattern matching the build environment, e.g. "{production,development}" + Any of the above can be omitted. ### Example diff --git a/resources/page/page.go b/resources/page/page.go index 74656f69e68..8c9c63e0a65 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -151,7 +151,7 @@ type PageMetaProvider interface { // Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag. Draft() bool - // Environment returns the environment of the Page + // Environment returns the build environment. Environment() string // IsHome returns whether this is the home page. From fdc148645b05a12a67f165a1db1520b51736e9e8 Mon Sep 17 00:00:00 2001 From: Cathrine Paulsen Date: Sun, 3 Apr 2022 15:30:46 +0200 Subject: [PATCH 3/3] Remove environment from Page interface Implement the Environment keyword for PageMatcher in a more straightforward way. The extended PageMatcher test is adjusted accordingly and the required missing test functionality is added. --- hugolib/page__meta.go | 4 ---- hugolib/site.go | 4 ---- resources/page/page.go | 3 --- resources/page/page_matcher.go | 2 +- resources/page/page_matcher_test.go | 7 +++++-- resources/page/page_nop.go | 4 ---- resources/page/testhelpers_test.go | 8 ++------ 7 files changed, 8 insertions(+), 24 deletions(-) diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 13bef7029fa..5568aa539c9 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -167,10 +167,6 @@ func (p *pageMeta) Description() string { return p.description } -func (p *pageMeta) Environment() string { - return p.s.Environment() -} - func (p *pageMeta) Lang() string { return p.s.Lang() } diff --git a/hugolib/site.go b/hugolib/site.go index 3fcfe107047..59326cab61f 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1897,10 +1897,6 @@ func (s *Site) shouldBuild(p page.Page) bool { s.BuildDrafts, p.Draft(), p.PublishDate(), p.ExpiryDate()) } -func (s *Site) Environment() string { - return s.Info.Hugo().Environment -} - func shouldBuild(buildFuture bool, buildExpired bool, buildDrafts bool, Draft bool, publishDate time.Time, expiryDate time.Time) bool { if !(buildDrafts || !Draft) { diff --git a/resources/page/page.go b/resources/page/page.go index 8c9c63e0a65..5133f46fa19 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -151,9 +151,6 @@ type PageMetaProvider interface { // Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag. Draft() bool - // Environment returns the build environment. - Environment() string - // IsHome returns whether this is the home page. IsHome() bool diff --git a/resources/page/page_matcher.go b/resources/page/page_matcher.go index e41bf4f7644..4626186c5cf 100644 --- a/resources/page/page_matcher.go +++ b/resources/page/page_matcher.go @@ -72,7 +72,7 @@ func (m PageMatcher) Matches(p Page) bool { if m.Environment != "" { g, err := glob.GetGlob(m.Environment) - if err == nil && !g.Match(p.Environment()) { + if err == nil && !g.Match(p.Site().Hugo().Environment) { return false } } diff --git a/resources/page/page_matcher_test.go b/resources/page/page_matcher_test.go index 581a9342082..846ab2c0341 100644 --- a/resources/page/page_matcher_test.go +++ b/resources/page/page_matcher_test.go @@ -14,6 +14,7 @@ package page import ( + "github.com/gohugoio/hugo/common/hugo" "path/filepath" "testing" @@ -22,10 +23,12 @@ import ( func TestPageMatcher(t *testing.T) { c := qt.New(t) + developmentTestSite := testSite{h: hugo.NewInfo("development", nil)} + productionTestSite := testSite{h: hugo.NewInfo("production", nil)} p1, p2, p3 := - &testPage{path: "/p1", kind: "section", lang: "en", environment: "development"}, - &testPage{path: "p2", kind: "page", lang: "no", environment: "production"}, + &testPage{path: "/p1", kind: "section", lang: "en", site: developmentTestSite}, + &testPage{path: "p2", kind: "page", lang: "no", site: productionTestSite}, &testPage{path: "p3", kind: "page", lang: "en"} c.Run("Matches", func(c *qt.C) { diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go index 1d3440b5a34..67b8b8d4b09 100644 --- a/resources/page/page_nop.go +++ b/resources/page/page_nop.go @@ -120,10 +120,6 @@ func (p *nopPage) Description() string { return "" } -func (p *nopPage) Environment() string { - return "" -} - func (p *nopPage) RefFrom(argsm map[string]any, source any) (string, error) { return "", nil } diff --git a/resources/page/testhelpers_test.go b/resources/page/testhelpers_test.go index 21bbebdca7c..6acb4cfe46e 100644 --- a/resources/page/testhelpers_test.go +++ b/resources/page/testhelpers_test.go @@ -94,7 +94,7 @@ type testPage struct { linkTitle string lang string section string - environment string + site testSite content string @@ -169,10 +169,6 @@ func (p *testPage) Data() any { return p.data } -func (p *testPage) Environment() string { - return p.environment -} - func (p *testPage) Sitemap() config.Sitemap { return config.Sitemap{} } @@ -537,7 +533,7 @@ func (p *testPage) SectionsPath() string { } func (p *testPage) Site() Site { - panic("not implemented") + return p.site } func (p *testPage) Sites() Sites {