diff options
author | Cathrine Paulsen <c.r.paulsen@student.tudelft.nl> | 2022-04-05 10:41:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-05 10:41:24 +0300 |
commit | da00e7714e0233d5cba73bc3c4cf7331304ccb20 (patch) | |
tree | 65609a1e27b98a5a5cc02426ef1c6ec4955c7265 /resources/page | |
parent | ed9aa374dd5e18fbfb207a0a10cde2362061c8ca (diff) |
Add environment as a new filter to _cascade.target
Fixes #9612
Diffstat (limited to 'resources/page')
-rw-r--r-- | resources/page/page_matcher.go | 10 | ||||
-rw-r--r-- | resources/page/page_matcher_test.go | 18 | ||||
-rw-r--r-- | resources/page/testhelpers_test.go | 3 |
3 files changed, 29 insertions, 2 deletions
diff --git a/resources/page/page_matcher.go b/resources/page/page_matcher.go index 15b8ede89..4626186c5 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.Site().Hugo().Environment) { + return false + } + } + return true } diff --git a/resources/page/page_matcher_test.go b/resources/page/page_matcher_test.go index 72aec5f2a..846ab2c03 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,8 +23,13 @@ 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"}, &testPage{path: "p2", kind: "page", lang: "no"}, &testPage{path: "p3", kind: "page", lang: "en"} + p1, p2, p3 := + &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) { m := PageMatcher{Kind: "section"} @@ -50,6 +56,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/testhelpers_test.go b/resources/page/testhelpers_test.go index 3f6accdac..cee1f99e5 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 + site testSite content string @@ -532,7 +533,7 @@ func (p *testPage) SectionsPath() string { } func (p *testPage) Site() Site { - panic("not implemented") + return p.site } func (p *testPage) Sites() Sites { |