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

Extend PageMatcher with Environment keyword #9715

Merged
merged 3 commits into from
Apr 5, 2022
Merged
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
3 changes: 3 additions & 0 deletions docs/content/en/content-management/front-matter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions hugolib/cascade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions resources/page/page_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 17 additions & 1 deletion resources/page/page_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package page

import (
"github.com/gohugoio/hugo/common/hugo"
"path/filepath"
"testing"

Expand All @@ -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"}
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion resources/page/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type testPage struct {
linkTitle string
lang string
section string
site testSite

content string

Expand Down Expand Up @@ -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 {
Expand Down