Skip to content

Commit

Permalink
hugolib: Add .Site.Sites
Browse files Browse the repository at this point in the history
Fixes #5504
  • Loading branch information
bep committed Dec 6, 2018
1 parent 831d23c commit 8378358
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 8 additions & 0 deletions hugolib/hugo_sites.go
Expand Up @@ -55,6 +55,14 @@ type HugoSites struct {
gitInfo *gitInfo gitInfo *gitInfo
} }


func (h *HugoSites) siteInfos() SiteInfos {
infos := make(SiteInfos, len(h.Sites))
for i, site := range h.Sites {
infos[i] = &site.Info
}
return infos
}

func (h *HugoSites) pickOneAndLogTheRest(errors []error) error { func (h *HugoSites) pickOneAndLogTheRest(errors []error) error {
if len(errors) == 0 { if len(errors) == 0 {
return nil return nil
Expand Down
7 changes: 1 addition & 6 deletions hugolib/page.go
Expand Up @@ -369,12 +369,7 @@ func (p *Page) Summary() template.HTML {


// Sites is a convenience method to get all the Hugo sites/languages configured. // Sites is a convenience method to get all the Hugo sites/languages configured.
func (p *Page) Sites() SiteInfos { func (p *Page) Sites() SiteInfos {
infos := make(SiteInfos, len(p.s.owner.Sites)) return p.s.owner.siteInfos()
for i, site := range p.s.owner.Sites {
infos[i] = &site.Info
}

return infos
} }


// SearchKeywords implements the related.Document interface needed for fast page searches. // SearchKeywords implements the related.Document interface needed for fast page searches.
Expand Down
4 changes: 4 additions & 0 deletions hugolib/site.go
Expand Up @@ -424,6 +424,10 @@ func (s *SiteInfo) Hugo() hugo.Info {
return s.hugoInfo return s.hugoInfo
} }


// Sites is a convenience method to get all the Hugo sites/languages configured.
func (s *SiteInfo) Sites() SiteInfos {
return s.s.owner.siteInfos()
}
func (s *SiteInfo) String() string { func (s *SiteInfo) String() string {
return fmt.Sprintf("Site(%q)", s.Title) return fmt.Sprintf("Site(%q)", s.Title)
} }
Expand Down
11 changes: 9 additions & 2 deletions hugolib/template_test.go
Expand Up @@ -242,6 +242,7 @@ func TestTemplateFuncs(t *testing.T) {
b := newTestSitesBuilder(t).WithDefaultMultiSiteConfig() b := newTestSitesBuilder(t).WithDefaultMultiSiteConfig()


homeTpl := `Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }} homeTpl := `Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }}
Sites: {{ site.Sites.First.Home.Language.Lang }}
Hugo: {{ hugo.Generator }} Hugo: {{ hugo.Generator }}
` `


Expand All @@ -252,8 +253,14 @@ Hugo: {{ hugo.Generator }}


b.CreateSites().Build(BuildCfg{}) b.CreateSites().Build(BuildCfg{})


b.AssertFileContent("public/en/index.html", "Site: en / en / http://example.com/blog", b.AssertFileContent("public/en/index.html",
"Site: en / en / http://example.com/blog",
"Sites: en",
"Hugo: <meta name=\"generator\" content=\"Hugo") "Hugo: <meta name=\"generator\" content=\"Hugo")
b.AssertFileContent("public/fr/index.html", "Site: fr / fr / http://example.com/blog") b.AssertFileContent("public/fr/index.html",
"Site: fr / fr / http://example.com/blog",
"Sites: en",
"Hugo: <meta name=\"generator\" content=\"Hugo",
)


} }

0 comments on commit 8378358

Please sign in to comment.