]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
hugolib: Simplify sites collection
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 17 Feb 2026 12:03:10 +0000 (13:03 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 17 Feb 2026 18:10:47 +0000 (19:10 +0100)
hugolib/hugo_sites.go
hugolib/site.go

index 125c97fe0b8de15e87280b1458e0987348489f74..41625191f67ba7c499a66c3c8eabc81dee47aa4d 100644 (file)
@@ -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) {
index bed66edfc42f7205bafd88d396499e268460db3a..cb9060a57bf07f03ba1aa4d07d7f7cb81a4eb0ce 100644 (file)
@@ -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.