From: Bjørn Erik Pedersen Date: Tue, 17 Feb 2026 12:03:10 +0000 (+0100) Subject: hugolib: Simplify sites collection X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=bba2aed3527e5c6086244c0ab76192b35b6ffa73;p=brevno-suite%2Fhugo hugolib: Simplify sites collection --- diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 125c97fe0..41625191f 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "iter" + "slices" "strings" "sync" "sync/atomic" @@ -129,11 +130,7 @@ type hugoSitesSitesProvider struct { } func (sp hugoSitesSitesProvider) Sites() page.Sites { - sites := make(page.Sites, 0) - for s := range sp.h.allSites(nil) { - sites = append(sites, s.Site()) - } - return sites + return slices.Collect(sp.h.allSitesInterface(nil)) } type progressReporter struct { @@ -152,12 +149,19 @@ func (p *progressReporter) Start() { p.t = htime.Now() } +// allSites will range over all sites in the order of the sitesVersionsRoles matrix. +// If include is not nil, it will be used to filter the sites. func (h *HugoSites) allSites(include func(s *Site) bool) iter.Seq[*Site] { + if include == nil { + include = func(s *Site) bool { + return true + } + } return func(yield func(s *Site) bool) { for _, v := range h.sitesVersionsRoles { for _, r := range v { for _, s := range r { - if include != nil && !include(s) { + if !include(s) { continue } if !yield(s) { @@ -169,6 +173,31 @@ func (h *HugoSites) allSites(include func(s *Site) bool) iter.Seq[*Site] { } } +// allSitesInterface is the same as allSites but returns page.Site, which is used in some places where we don't want to expose the full *Site type. +// If include is not nil, it will be used to filter the sites. +func (h *HugoSites) allSitesInterface(include func(s page.Site) bool) iter.Seq[page.Site] { + if include == nil { + include = func(s page.Site) bool { + return true + } + } + return func(yield func(s page.Site) bool) { + for _, v := range h.sitesVersionsRoles { + for _, r := range v { + for _, s := range r { + if !include(s) { + continue + } + // Site() returns a wrapped version of the site that only exposes the page.Site interface. + if !yield(s.Site()) { + return + } + } + } + } + } +} + // allSiteLanguages will range over the first site in each language that's not skipped. func (h *HugoSites) allSiteLanguages(include func(s *Site) bool) iter.Seq[*Site] { return func(yield func(s *Site) bool) { diff --git a/hugolib/site.go b/hugolib/site.go index bed66edfc..cb9060a57 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -648,15 +648,7 @@ func (s *Site) Sites() page.Sites { s.h.printSiteSitesDeprecationInit.Do(func() { hugo.Deprecate(".Site.Sites", "Use hugo.Sites instead.", "v0.156.0") }) - var sites page.Sites - for _, v1 := range s.h.sitesVersionsRoles { - for _, v2 := range v1 { - for _, s := range v2 { - sites = append(sites, s.Site()) - } - } - } - return sites + return slices.Collect(s.h.allSitesInterface(nil)) } // IsDefault reports whether this site is the default across all dimensions.