Get the list commands up to multi-site level

This commit also unexports some Site methods, making it clear what the external build API really is.

See #2312
This commit is contained in:
Bjørn Erik Pedersen 2016-08-05 16:11:03 +02:00
parent 90de511017
commit 8b657a11ee
4 changed files with 65 additions and 42 deletions

View file

@ -51,13 +51,17 @@ var listDraftsCmd = &cobra.Command{
viper.Set("BuildDrafts", true) viper.Set("BuildDrafts", true)
site := &hugolib.Site{} sites, err := hugolib.NewHugoSitesFromConfiguration()
if err := site.PreProcess(hugolib.BuildCfg{}); err != nil { if err != nil {
return newSystemError("Error creating sites", err)
}
if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
return newSystemError("Error Processing Source Content", err) return newSystemError("Error Processing Source Content", err)
} }
for _, p := range site.AllPages { for _, p := range sites.Pages() {
if p.IsDraft() { if p.IsDraft() {
fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName())) fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
} }
@ -82,13 +86,17 @@ posted in the future.`,
viper.Set("BuildFuture", true) viper.Set("BuildFuture", true)
site := &hugolib.Site{} sites, err := hugolib.NewHugoSitesFromConfiguration()
if err := site.PreProcess(hugolib.BuildCfg{}); err != nil { if err != nil {
return newSystemError("Error creating sites", err)
}
if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
return newSystemError("Error Processing Source Content", err) return newSystemError("Error Processing Source Content", err)
} }
for _, p := range site.AllPages { for _, p := range sites.Pages() {
if p.IsFuture() { if p.IsFuture() {
fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName())) fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
} }
@ -113,13 +121,17 @@ expired.`,
viper.Set("BuildExpired", true) viper.Set("BuildExpired", true)
site := &hugolib.Site{} sites, err := hugolib.NewHugoSitesFromConfiguration()
if err := site.PreProcess(hugolib.BuildCfg{}); err != nil { if err != nil {
return newSystemError("Error creating sites", err)
}
if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
return newSystemError("Error Processing Source Content", err) return newSystemError("Error Processing Source Content", err)
} }
for _, p := range site.AllPages { for _, p := range sites.Pages() {
if p.IsExpired() { if p.IsExpired() {
fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName())) fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
} }

View file

@ -37,6 +37,8 @@ type HugoSites struct {
Multilingual *Multilingual Multilingual *Multilingual
} }
// NewHugoSites creates a new collection of sites given the input sites, building
// a language configuration based on those.
func NewHugoSites(sites ...*Site) (*HugoSites, error) { func NewHugoSites(sites ...*Site) (*HugoSites, error) {
languages := make(Languages, len(sites)) languages := make(Languages, len(sites))
for i, s := range sites { for i, s := range sites {
@ -56,11 +58,11 @@ func NewHugoSites(sites ...*Site) (*HugoSites, error) {
// NewHugoSitesFromConfiguration creates HugoSites from the global Viper config. // NewHugoSitesFromConfiguration creates HugoSites from the global Viper config.
func NewHugoSitesFromConfiguration() (*HugoSites, error) { func NewHugoSitesFromConfiguration() (*HugoSites, error) {
sites := make([]*Site, 0) var sites []*Site
multilingual := viper.GetStringMap("Languages") multilingual := viper.GetStringMap("Languages")
if len(multilingual) == 0 { if len(multilingual) == 0 {
// TODO(bep) multilingo langConfigsList = append(langConfigsList, NewLanguage("en")) // TODO(bep) multilingo langConfigsList = append(langConfigsList, NewLanguage("en"))
sites = append(sites, NewSite(NewLanguage("en"))) sites = append(sites, newSite(NewLanguage("en")))
} }
if len(multilingual) > 0 { if len(multilingual) > 0 {
@ -73,7 +75,7 @@ func NewHugoSitesFromConfiguration() (*HugoSites, error) {
} }
for _, lang := range languages { for _, lang := range languages {
sites = append(sites, NewSite(lang)) sites = append(sites, newSite(lang))
} }
} }
@ -98,13 +100,14 @@ func (h HugoSites) toSiteInfos() []*SiteInfo {
return infos return infos
} }
// BuildCfg holds build options used to, as an example, skip the render step.
type BuildCfg struct { type BuildCfg struct {
// Whether we are in watch (server) mode // Whether we are in watch (server) mode
Watching bool Watching bool
// Print build stats at the end of a build // Print build stats at the end of a build
PrintStats bool PrintStats bool
// Skip rendering. Useful for testing. // Skip rendering. Useful for testing.
skipRender bool SkipRender bool
// Use this to add templates to use for rendering. // Use this to add templates to use for rendering.
// Useful for testing. // Useful for testing.
withTemplate func(templ tpl.Template) error withTemplate func(templ tpl.Template) error
@ -131,7 +134,7 @@ func (h HugoSites) Build(config BuildCfg) error {
s.RunMode.Watching = config.Watching s.RunMode.Watching = config.Watching
} }
if err := firstSite.PreProcess(config); err != nil { if err := firstSite.preProcess(config); err != nil {
return err return err
} }
@ -146,7 +149,7 @@ func (h HugoSites) Build(config BuildCfg) error {
} }
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.PostProcess(); err != nil { if err := s.postProcess(); err != nil {
return err return err
} }
} }
@ -155,10 +158,10 @@ func (h HugoSites) Build(config BuildCfg) error {
return err return err
} }
if !config.skipRender { if !config.SkipRender {
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.Render(); err != nil { if err := s.render(); err != nil {
return err return err
} }
@ -201,7 +204,7 @@ func (h HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
if sourceChanged { if sourceChanged {
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.PostProcess(); err != nil { if err := s.postProcess(); err != nil {
return err return err
} }
} }
@ -211,9 +214,9 @@ func (h HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
return err return err
} }
if !config.skipRender { if !config.SkipRender {
for _, s := range h.Sites { for _, s := range h.Sites {
if err := s.Render(); err != nil { if err := s.render(); err != nil {
return err return err
} }
if config.PrintStats { if config.PrintStats {
@ -256,7 +259,7 @@ func (h *HugoSites) render() error {
return nil return nil
} }
func (s *HugoSites) setupTranslations(master *Site) { func (h *HugoSites) setupTranslations(master *Site) {
for _, p := range master.rawAllPages { for _, p := range master.rawAllPages {
if p.Lang() == "" { if p.Lang() == "" {
@ -265,7 +268,7 @@ func (s *HugoSites) setupTranslations(master *Site) {
shouldBuild := p.shouldBuild() shouldBuild := p.shouldBuild()
for i, site := range s.Sites { for i, site := range h.Sites {
if strings.HasPrefix(site.Language.Lang, p.Lang()) { if strings.HasPrefix(site.Language.Lang, p.Lang()) {
site.updateBuildStats(p) site.updateBuildStats(p)
if shouldBuild { if shouldBuild {
@ -283,14 +286,14 @@ func (s *HugoSites) setupTranslations(master *Site) {
} }
} }
for i := 1; i < len(s.Sites); i++ { for i := 1; i < len(h.Sites); i++ {
s.Sites[i].AllPages = s.Sites[0].AllPages h.Sites[i].AllPages = h.Sites[0].AllPages
} }
} }
if len(s.Sites) > 1 { if len(h.Sites) > 1 {
pages := s.Sites[0].AllPages pages := h.Sites[0].AllPages
allTranslations := pagesToTranslationsMap(s.Multilingual, pages) allTranslations := pagesToTranslationsMap(h.Multilingual, pages)
assignTranslationsToPages(allTranslations, pages) assignTranslationsToPages(allTranslations, pages)
} }
} }
@ -357,6 +360,11 @@ func (h *HugoSites) preRender() error {
return nil return nil
} }
// Pages returns all pages for all sites.
func (h HugoSites) Pages() Pages {
return h.Sites[0].AllPages
}
func handleShortcodes(p *Page, t tpl.Template) error { func handleShortcodes(p *Page, t tpl.Template) error {
if len(p.contentShortCodes) > 0 { if len(p.contentShortCodes) > 0 {
jww.DEBUG.Printf("Replace %d shortcodes in %q", len(p.contentShortCodes), p.BaseFileName()) jww.DEBUG.Printf("Replace %d shortcodes in %q", len(p.contentShortCodes), p.BaseFileName())
@ -417,7 +425,7 @@ func doBuildSite(s *Site, render bool, additionalTemplates ...string) error {
return nil return nil
} }
config := BuildCfg{skipRender: !render, withTemplate: addTemplates} config := BuildCfg{SkipRender: !render, withTemplate: addTemplates}
return sites.Build(config) return sites.Build(config)
} }

View file

@ -131,8 +131,8 @@ func TestMultiSitesBuild(t *testing.T) {
// Check taxonomies // Check taxonomies
enTags := enSite.Taxonomies["tags"] enTags := enSite.Taxonomies["tags"]
frTags := frSite.Taxonomies["plaques"] frTags := frSite.Taxonomies["plaques"]
require.Len(t, enTags, 2, fmt.Sprintf("Tags in en: %=v", enTags)) require.Len(t, enTags, 2, fmt.Sprintf("Tags in en: %v", enTags))
require.Len(t, frTags, 2, fmt.Sprintf("Tags in fr: %=v", frTags)) require.Len(t, frTags, 2, fmt.Sprintf("Tags in fr: %v", frTags))
require.NotNil(t, enTags["tag1"]) require.NotNil(t, enTags["tag1"])
require.NotNil(t, frTags["frtag1"]) require.NotNil(t, frTags["frtag1"])
readDestination(t, "public/fr/plaques/frtag1/index.html") readDestination(t, "public/fr/plaques/frtag1/index.html")

View file

@ -99,18 +99,20 @@ type Site struct {
Language *Language Language *Language
} }
// TODO(bep) multilingo
// Reset returns a new Site prepared for rebuild. // Reset returns a new Site prepared for rebuild.
// TODO(bep) multilingo
func (s *Site) Reset() *Site { func (s *Site) Reset() *Site {
return &Site{Language: s.Language, Multilingual: s.Multilingual} return &Site{Language: s.Language, Multilingual: s.Multilingual}
} }
func NewSite(lang *Language) *Site { // newSite creates a new site in the given language.
func newSite(lang *Language) *Site {
return &Site{Language: lang} return &Site{Language: lang}
} }
// newSite creates a new site in the default language.
func newSiteDefaultLang() *Site { func newSiteDefaultLang() *Site {
return NewSite(newDefaultLanguage()) return newSite(newDefaultLanguage())
} }
// Convenience func used in tests. // Convenience func used in tests.
@ -593,8 +595,9 @@ func (s *Site) ReBuild(events []fsnotify.Event) (bool, error) {
} }
// TODO(bep) ml
func (s *Site) Analyze() error { func (s *Site) Analyze() error {
if err := s.PreProcess(BuildCfg{}); err != nil { if err := s.preProcess(BuildCfg{}); err != nil {
return err return err
} }
return s.ShowPlan(os.Stdout) return s.ShowPlan(os.Stdout)
@ -718,7 +721,7 @@ func (s *Site) readDataFromSourceFS() error {
return err return err
} }
func (s *Site) PreProcess(config BuildCfg) (err error) { func (s *Site) preProcess(config BuildCfg) (err error) {
s.timerStep("Go initialization") s.timerStep("Go initialization")
if err = s.initialize(); err != nil { if err = s.initialize(); err != nil {
return return
@ -740,7 +743,7 @@ func (s *Site) PreProcess(config BuildCfg) (err error) {
} }
func (s *Site) PostProcess() (err error) { func (s *Site) postProcess() (err error) {
s.setupPrevNext() s.setupPrevNext()
@ -763,7 +766,7 @@ func (s *Site) setupPrevNext() {
} }
} }
func (s *Site) Render() (err error) { func (s *Site) render() (err error) {
if err = tpl.SetTranslateLang(s.Language.Lang); err != nil { if err = tpl.SetTranslateLang(s.Language.Lang); err != nil {
return return
} }
@ -831,7 +834,7 @@ func (s *Site) initialize() (err error) {
return return
} }
// HomeURL is a convenience method giving the absolute URL to the home page. // HomeAbsURL is a convenience method giving the absolute URL to the home page.
func (s *SiteInfo) HomeAbsURL() string { func (s *SiteInfo) HomeAbsURL() string {
base := "/" base := "/"
if s.Multilingual { if s.Multilingual {
@ -1950,7 +1953,7 @@ func (s *Site) renderRobotsTXT() error {
rLayouts := []string{"robots.txt", "_default/robots.txt", "_internal/_default/robots.txt"} rLayouts := []string{"robots.txt", "_default/robots.txt", "_internal/_default/robots.txt"}
outBuffer := bp.GetBuffer() outBuffer := bp.GetBuffer()
defer bp.PutBuffer(outBuffer) defer bp.PutBuffer(outBuffer)
err := s.render("robots", n, outBuffer, s.appendThemeTemplates(rLayouts)...) err := s.renderForLayouts("robots", n, outBuffer, s.appendThemeTemplates(rLayouts)...)
if err == nil { if err == nil {
err = s.writeDestFile("robots.txt", outBuffer) err = s.writeDestFile("robots.txt", outBuffer)
@ -2011,7 +2014,7 @@ func (s *Site) renderAndWriteXML(name string, dest string, d interface{}, layout
defer bp.PutBuffer(renderBuffer) defer bp.PutBuffer(renderBuffer)
renderBuffer.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n") renderBuffer.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n")
err := s.render(name, d, renderBuffer, layouts...) err := s.renderForLayouts(name, d, renderBuffer, layouts...)
if err != nil { if err != nil {
return err return err
@ -2041,7 +2044,7 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
renderBuffer := bp.GetBuffer() renderBuffer := bp.GetBuffer()
defer bp.PutBuffer(renderBuffer) defer bp.PutBuffer(renderBuffer)
err := s.render(name, d, renderBuffer, layouts...) err := s.renderForLayouts(name, d, renderBuffer, layouts...)
if err != nil { if err != nil {
return err return err
@ -2126,7 +2129,7 @@ Your rendered home page is blank: /index.html is zero-length
return err return err
} }
func (s *Site) render(name string, d interface{}, w io.Writer, layouts ...string) error { func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts ...string) error {
layout, found := s.findFirstLayout(layouts...) layout, found := s.findFirstLayout(layouts...)
if found == false { if found == false {
jww.WARN.Printf("Unable to locate layout for %s: %s\n", name, layouts) jww.WARN.Printf("Unable to locate layout for %s: %s\n", name, layouts)