Add parent as owner to Site

And pull up common member vars like Tmpl and Multilinguage.

Or: the final multilingual TODO-fixes.

See #2309
This commit is contained in:
Bjørn Erik Pedersen 2016-08-08 10:12:39 +02:00
parent 9798a95670
commit d953e39e63
5 changed files with 63 additions and 61 deletions

View file

@ -74,7 +74,7 @@ func (mh *MetaHandle) Convert(i interface{}, s *Site, results HandleResults) {
return return
} }
results <- h.PageConvert(p, s.Tmpl) results <- h.PageConvert(p, s.owner.tmpl)
} }
} }

View file

@ -35,7 +35,10 @@ import (
type HugoSites struct { type HugoSites struct {
Sites []*Site Sites []*Site
Multilingual *Multilingual tmpl tpl.Template
runMode runmode
multilingual *Multilingual
} }
// NewHugoSites creates a new collection of sites given the input sites, building // NewHugoSites creates a new collection of sites given the input sites, building
@ -47,7 +50,12 @@ func newHugoSites(sites ...*Site) (*HugoSites, error) {
return nil, err return nil, err
} }
return &HugoSites{Multilingual: langConfig, Sites: sites}, nil h := &HugoSites{multilingual: langConfig, Sites: sites}
for _, s := range sites {
s.owner = h
}
return h, nil
} }
// NewHugoSitesFromConfiguration creates HugoSites from the global Viper config. // NewHugoSitesFromConfiguration creates HugoSites from the global Viper config.
@ -92,7 +100,6 @@ func (h *HugoSites) reset() {
} }
func (h *HugoSites) reCreateFromConfig() error { func (h *HugoSites) reCreateFromConfig() error {
oldSite := h.Sites[0]
sites, err := createSitesFromConfig() sites, err := createSitesFromConfig()
if err != nil { if err != nil {
@ -106,13 +113,13 @@ func (h *HugoSites) reCreateFromConfig() error {
} }
h.Sites = sites h.Sites = sites
h.Multilingual = langConfig
for _, s := range h.Sites { for _, s := range sites {
// TODO(bep) ml Tmpl s.owner = h
s.Tmpl = oldSite.Tmpl
} }
h.multilingual = langConfig
return nil return nil
} }
@ -157,18 +164,14 @@ func (h *HugoSites) Build(config BuildCfg) error {
} }
} }
h.runMode.Watching = config.Watching
// We should probably refactor the Site and pull up most of the logic from there to here, // We should probably refactor the Site and pull up most of the logic from there to here,
// but that seems like a daunting task. // but that seems like a daunting task.
// So for now, if there are more than one site (language), // So for now, if there are more than one site (language),
// we pre-process the first one, then configure all the sites based on that. // we pre-process the first one, then configure all the sites based on that.
firstSite := h.Sites[0] firstSite := h.Sites[0]
for _, s := range h.Sites {
// TODO(bep) ml
s.Multilingual = h.Multilingual
s.RunMode.Watching = config.Watching
}
if err := firstSite.preProcess(config); err != nil { if err := firstSite.preProcess(config); err != nil {
return err return err
} }
@ -178,8 +181,6 @@ func (h *HugoSites) Build(config BuildCfg) error {
if len(h.Sites) > 1 { if len(h.Sites) > 1 {
// Initialize the rest // Initialize the rest
for _, site := range h.Sites[1:] { for _, site := range h.Sites[1:] {
// TODO(bep) ml Tmpl
site.Tmpl = firstSite.Tmpl
site.initializeSiteInfo() site.initializeSiteInfo()
} }
} }
@ -231,11 +232,7 @@ func (h *HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
return errors.New("Rebuild does not support 'ResetState'. Use Build.") return errors.New("Rebuild does not support 'ResetState'. Use Build.")
} }
for _, s := range h.Sites { h.runMode.Watching = config.Watching
// TODO(bep) ml
s.Multilingual = h.Multilingual
s.RunMode.Watching = config.Watching
}
firstSite := h.Sites[0] firstSite := h.Sites[0]
@ -300,7 +297,7 @@ func (h *HugoSites) Analyze() error {
// Render the cross-site artifacts. // Render the cross-site artifacts.
func (h *HugoSites) render() error { func (h *HugoSites) render() error {
if !h.Multilingual.enabled() { if !h.multilingual.enabled() {
return nil return nil
} }
@ -353,7 +350,7 @@ func (h *HugoSites) setupTranslations(master *Site) {
if len(h.Sites) > 1 { if len(h.Sites) > 1 {
pages := h.Sites[0].AllPages pages := h.Sites[0].AllPages
allTranslations := pagesToTranslationsMap(h.Multilingual, pages) allTranslations := pagesToTranslationsMap(h.multilingual, pages)
assignTranslationsToPages(allTranslations, pages) assignTranslationsToPages(allTranslations, pages)
} }
} }
@ -374,7 +371,7 @@ func (h *HugoSites) preRender() error {
go func(pages <-chan *Page, wg *sync.WaitGroup) { go func(pages <-chan *Page, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
for p := range pages { for p := range pages {
if err := handleShortcodes(p, s.Tmpl); err != nil { if err := handleShortcodes(p, s.owner.tmpl); err != nil {
jww.ERROR.Printf("Failed to handle shortcodes for page %s: %s", p.BaseFileName(), err) jww.ERROR.Printf("Failed to handle shortcodes for page %s: %s", p.BaseFileName(), err)
} }

View file

@ -72,7 +72,10 @@ func (ml *Multilingual) enabled() bool {
} }
func (s *Site) multilingualEnabled() bool { func (s *Site) multilingualEnabled() bool {
return s.Multilingual != nil && s.Multilingual.enabled() if s.owner == nil {
return false
}
return s.owner.multilingual != nil && s.owner.multilingual.enabled()
} }
func toSortedLanguages(l map[string]interface{}) (helpers.Languages, error) { func toSortedLanguages(l map[string]interface{}) (helpers.Languages, error) {

View file

@ -32,9 +32,9 @@ func (s *Site) ShowPlan(out io.Writer) (err error) {
} else { } else {
fmt.Fprintf(out, " (renderer: n/a)") fmt.Fprintf(out, " (renderer: n/a)")
} }
if s.Tmpl != nil { if s.owner.tmpl != nil {
for _, l := range p.layouts() { for _, l := range p.layouts() {
fmt.Fprintf(out, " (layout: %s, exists: %t)", l, s.Tmpl.Lookup(l) != nil) fmt.Fprintf(out, " (layout: %s, exists: %t)", l, s.owner.tmpl.Lookup(l) != nil)
} }
} }
fmt.Fprintf(out, "\n") fmt.Fprintf(out, "\n")

View file

@ -76,11 +76,11 @@ var (
// //
// 5. The entire collection of files is written to disk. // 5. The entire collection of files is written to disk.
type Site struct { type Site struct {
owner *HugoSites
Pages Pages Pages Pages
AllPages Pages AllPages Pages
rawAllPages Pages rawAllPages Pages
Files []*source.File Files []*source.File
Tmpl tpl.Template
Taxonomies TaxonomyList Taxonomies TaxonomyList
Source source.Input Source source.Input
Sections Taxonomy Sections Taxonomy
@ -89,9 +89,6 @@ type Site struct {
timer *nitro.B timer *nitro.B
targets targetList targets targetList
targetListInit sync.Once targetListInit sync.Once
RunMode runmode
// TODO(bep ml remove
Multilingual *Multilingual
draftCount int draftCount int
futureCount int futureCount int
expiredCount int expiredCount int
@ -101,7 +98,7 @@ type Site struct {
// reset returns a new Site prepared for rebuild. // reset returns a new Site prepared for rebuild.
func (s *Site) reset() *Site { func (s *Site) reset() *Site {
return &Site{Language: s.Language, Multilingual: s.Multilingual} return &Site{Language: s.Language}
} }
// newSite creates a new site in the given language. // newSite creates a new site in the given language.
@ -423,7 +420,7 @@ type runmode struct {
} }
func (s *Site) running() bool { func (s *Site) running() bool {
return s.RunMode.Watching return s.owner.runMode.Watching
} }
func init() { func init() {
@ -478,7 +475,7 @@ func (s *Site) reBuild(events []fsnotify.Event) (bool, error) {
if len(tmplChanged) > 0 { if len(tmplChanged) > 0 {
s.prepTemplates(nil) s.prepTemplates(nil)
s.Tmpl.PrintErrors() s.owner.tmpl.PrintErrors()
s.timerStep("template prep") s.timerStep("template prep")
} }
@ -602,10 +599,10 @@ func (s *Site) reBuild(events []fsnotify.Event) (bool, error) {
} }
func (s *Site) loadTemplates() { func (s *Site) loadTemplates() {
s.Tmpl = tpl.InitializeT() s.owner.tmpl = tpl.InitializeT()
s.Tmpl.LoadTemplates(s.absLayoutDir()) s.owner.tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() { if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme") s.owner.tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
} }
} }
@ -613,12 +610,12 @@ func (s *Site) prepTemplates(withTemplate func(templ tpl.Template) error) error
s.loadTemplates() s.loadTemplates()
if withTemplate != nil { if withTemplate != nil {
if err := withTemplate(s.Tmpl); err != nil { if err := withTemplate(s.owner.tmpl); err != nil {
return err return err
} }
} }
s.Tmpl.MarkReady() s.owner.tmpl.MarkReady()
return nil return nil
} }
@ -724,7 +721,7 @@ func (s *Site) preProcess(config BuildCfg) (err error) {
return return
} }
s.prepTemplates(config.withTemplate) s.prepTemplates(config.withTemplate)
s.Tmpl.PrintErrors() s.owner.tmpl.PrintErrors()
s.timerStep("initialize & template prep") s.timerStep("initialize & template prep")
if err = s.readDataFromSourceFS(); err != nil { if err = s.readDataFromSourceFS(); err != nil {
@ -856,8 +853,8 @@ func (s *Site) initializeSiteInfo() {
languages helpers.Languages languages helpers.Languages
) )
if s.Multilingual != nil { if s.owner != nil && s.owner.multilingual != nil {
languages = s.Multilingual.Languages languages = s.owner.multilingual.Languages
} }
params := lang.Params() params := lang.Params()
@ -872,6 +869,12 @@ func (s *Site) initializeSiteInfo() {
languagePrefix = "/" + lang.Lang languagePrefix = "/" + lang.Lang
} }
var multilingual *Multilingual
if s.owner != nil {
multilingual = s.owner.multilingual
}
s.Info = SiteInfo{ s.Info = SiteInfo{
BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))), BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))),
Title: lang.GetString("Title"), Title: lang.GetString("Title"),
@ -880,8 +883,7 @@ func (s *Site) initializeSiteInfo() {
LanguageCode: lang.GetString("languagecode"), LanguageCode: lang.GetString("languagecode"),
Copyright: lang.GetString("copyright"), Copyright: lang.GetString("copyright"),
DisqusShortname: lang.GetString("DisqusShortname"), DisqusShortname: lang.GetString("DisqusShortname"),
// TODO(bep) ml consolidate the below (make into methods etc.) multilingual: multilingual,
multilingual: s.Multilingual,
Language: lang, Language: lang,
LanguagePrefix: languagePrefix, LanguagePrefix: languagePrefix,
Languages: languages, Languages: languages,
@ -1420,8 +1422,8 @@ func (s *Site) renderAliases() error {
} }
} }
if s.Multilingual.enabled() { if s.owner.multilingual.enabled() {
mainLang := s.Multilingual.DefaultLang.Lang mainLang := s.owner.multilingual.DefaultLang.Lang
mainLangURL := helpers.AbsURL(mainLang, false) mainLangURL := helpers.AbsURL(mainLang, false)
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL) jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL); err != nil { if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL); err != nil {
@ -1450,7 +1452,7 @@ func (s *Site) renderPages() error {
var layouts []string var layouts []string
if !p.IsRenderable() { if !p.IsRenderable() {
self := "__" + p.TargetPath() self := "__" + p.TargetPath()
_, err := s.Tmpl.GetClone().New(self).Parse(string(p.Content)) _, err := s.owner.tmpl.GetClone().New(self).Parse(string(p.Content))
if err != nil { if err != nil {
results <- err results <- err
continue continue
@ -2154,7 +2156,7 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts
func (s *Site) findFirstLayout(layouts ...string) (string, bool) { func (s *Site) findFirstLayout(layouts ...string) (string, bool) {
for _, layout := range layouts { for _, layout := range layouts {
if s.Tmpl.Lookup(layout) != nil { if s.owner.tmpl.Lookup(layout) != nil {
return layout, true return layout, true
} }
} }
@ -2164,7 +2166,7 @@ func (s *Site) findFirstLayout(layouts ...string) (string, bool) {
func (s *Site) renderThing(d interface{}, layout string, w io.Writer) error { func (s *Site) renderThing(d interface{}, layout string, w io.Writer) error {
// If the template doesn't exist, then return, but leave the Writer open // If the template doesn't exist, then return, but leave the Writer open
if templ := s.Tmpl.Lookup(layout); templ != nil { if templ := s.owner.tmpl.Lookup(layout); templ != nil {
return templ.Execute(w, d) return templ.Execute(w, d)
} }
return fmt.Errorf("Layout not found: %s", layout) return fmt.Errorf("Layout not found: %s", layout)