Skip to content

Commit

Permalink
Extend PageMatcher with Environment keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
CathrinePaulsen committed Mar 24, 2022
1 parent a461e9d commit ccdeeb7
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
27 changes: 27 additions & 0 deletions hugolib/cascade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,33 @@ kind="section"
b.AssertFileContent("public/index.html", `
P1|p1:p1|p2:|
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|
`)
})

Expand Down
4 changes: 4 additions & 0 deletions hugolib/page__meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions resources/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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.Environment()) {
return false
}
}

return true
}

Expand Down
15 changes: 14 additions & 1 deletion resources/page/page_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions resources/page/page_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions 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
environment string

content string

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

0 comments on commit ccdeeb7

Please sign in to comment.