Skip to content

Commit

Permalink
Add parent as owner to Site
Browse files Browse the repository at this point in the history
And pull up common member vars like Tmpl and Multilinguage.

Or: the final multilingual TODO-fixes.

See gohugoio#2309
  • Loading branch information
bep authored and tychoish committed Aug 13, 2017
1 parent c882c49 commit 24ba359
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 61 deletions.
2 changes: 1 addition & 1 deletion hugolib/handler_meta.go
Expand Up @@ -74,7 +74,7 @@ func (mh *MetaHandle) Convert(i interface{}, s *Site, results HandleResults) {
return
}

results <- h.PageConvert(p, s.Tmpl)
results <- h.PageConvert(p, s.owner.tmpl)
}
}

Expand Down
43 changes: 20 additions & 23 deletions hugolib/hugo_sites.go
Expand Up @@ -35,7 +35,10 @@ import (
type HugoSites struct {
Sites []*Site

Multilingual *Multilingual
tmpl tpl.Template
runMode runmode

multilingual *Multilingual
}

// NewHugoSites creates a new collection of sites given the input sites, building
Expand All @@ -47,7 +50,12 @@ func newHugoSites(sites ...*Site) (*HugoSites, error) {
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.
Expand Down Expand Up @@ -92,7 +100,6 @@ func (h *HugoSites) reset() {
}

func (h *HugoSites) reCreateFromConfig() error {
oldSite := h.Sites[0]
sites, err := createSitesFromConfig()

if err != nil {
Expand All @@ -106,13 +113,13 @@ func (h *HugoSites) reCreateFromConfig() error {
}

h.Sites = sites
h.Multilingual = langConfig

for _, s := range h.Sites {
// TODO(bep) ml Tmpl
s.Tmpl = oldSite.Tmpl
for _, s := range sites {
s.owner = h
}

h.multilingual = langConfig

return nil
}

Expand Down Expand Up @@ -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,
// but that seems like a daunting task.
// 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.
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 {
return err
}
Expand All @@ -178,8 +181,6 @@ func (h *HugoSites) Build(config BuildCfg) error {
if len(h.Sites) > 1 {
// Initialize the rest
for _, site := range h.Sites[1:] {
// TODO(bep) ml Tmpl
site.Tmpl = firstSite.Tmpl
site.initializeSiteInfo()
}
}
Expand Down Expand Up @@ -231,11 +232,7 @@ func (h *HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
return errors.New("Rebuild does not support 'ResetState'. Use Build.")
}

for _, s := range h.Sites {
// TODO(bep) ml
s.Multilingual = h.Multilingual
s.RunMode.Watching = config.Watching
}
h.runMode.Watching = config.Watching

firstSite := h.Sites[0]

Expand Down Expand Up @@ -300,7 +297,7 @@ func (h *HugoSites) Analyze() error {
// Render the cross-site artifacts.
func (h *HugoSites) render() error {

if !h.Multilingual.enabled() {
if !h.multilingual.enabled() {
return nil
}

Expand Down Expand Up @@ -353,7 +350,7 @@ func (h *HugoSites) setupTranslations(master *Site) {

if len(h.Sites) > 1 {
pages := h.Sites[0].AllPages
allTranslations := pagesToTranslationsMap(h.Multilingual, pages)
allTranslations := pagesToTranslationsMap(h.multilingual, pages)
assignTranslationsToPages(allTranslations, pages)
}
}
Expand All @@ -374,7 +371,7 @@ func (h *HugoSites) preRender() error {
go func(pages <-chan *Page, wg *sync.WaitGroup) {
defer wg.Done()
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)
}

Expand Down
5 changes: 4 additions & 1 deletion hugolib/multilingual.go
Expand Up @@ -72,7 +72,10 @@ func (ml *Multilingual) enabled() 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) {
Expand Down
4 changes: 2 additions & 2 deletions hugolib/planner.go
Expand Up @@ -32,9 +32,9 @@ func (s *Site) ShowPlan(out io.Writer) (err error) {
} else {
fmt.Fprintf(out, " (renderer: n/a)")
}
if s.Tmpl != nil {
if s.owner.tmpl != nil {
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")
Expand Down
70 changes: 36 additions & 34 deletions hugolib/site.go
Expand Up @@ -76,11 +76,11 @@ var (
//
// 5. The entire collection of files is written to disk.
type Site struct {
owner *HugoSites
Pages Pages
AllPages Pages
rawAllPages Pages
Files []*source.File
Tmpl tpl.Template
Taxonomies TaxonomyList
Source source.Input
Sections Taxonomy
Expand All @@ -89,19 +89,16 @@ type Site struct {
timer *nitro.B
targets targetList
targetListInit sync.Once
RunMode runmode
// TODO(bep ml remove
Multilingual *Multilingual
draftCount int
futureCount int
expiredCount int
Data map[string]interface{}
Language *helpers.Language
draftCount int
futureCount int
expiredCount int
Data map[string]interface{}
Language *helpers.Language
}

// reset returns a new Site prepared for rebuild.
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.
Expand Down Expand Up @@ -423,7 +420,7 @@ type runmode struct {
}

func (s *Site) running() bool {
return s.RunMode.Watching
return s.owner.runMode.Watching
}

func init() {
Expand Down Expand Up @@ -478,7 +475,7 @@ func (s *Site) reBuild(events []fsnotify.Event) (bool, error) {

if len(tmplChanged) > 0 {
s.prepTemplates(nil)
s.Tmpl.PrintErrors()
s.owner.tmpl.PrintErrors()
s.timerStep("template prep")
}

Expand Down Expand Up @@ -602,23 +599,23 @@ func (s *Site) reBuild(events []fsnotify.Event) (bool, error) {
}

func (s *Site) loadTemplates() {
s.Tmpl = tpl.InitializeT()
s.Tmpl.LoadTemplates(s.absLayoutDir())
s.owner.tmpl = tpl.InitializeT()
s.owner.tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
s.owner.tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
}
}

func (s *Site) prepTemplates(withTemplate func(templ tpl.Template) error) error {
s.loadTemplates()

if withTemplate != nil {
if err := withTemplate(s.Tmpl); err != nil {
if err := withTemplate(s.owner.tmpl); err != nil {
return err
}
}

s.Tmpl.MarkReady()
s.owner.tmpl.MarkReady()

return nil
}
Expand Down Expand Up @@ -724,7 +721,7 @@ func (s *Site) preProcess(config BuildCfg) (err error) {
return
}
s.prepTemplates(config.withTemplate)
s.Tmpl.PrintErrors()
s.owner.tmpl.PrintErrors()
s.timerStep("initialize & template prep")

if err = s.readDataFromSourceFS(); err != nil {
Expand Down Expand Up @@ -856,8 +853,8 @@ func (s *Site) initializeSiteInfo() {
languages helpers.Languages
)

if s.Multilingual != nil {
languages = s.Multilingual.Languages
if s.owner != nil && s.owner.multilingual != nil {
languages = s.owner.multilingual.Languages
}

params := lang.Params()
Expand All @@ -872,16 +869,21 @@ func (s *Site) initializeSiteInfo() {
languagePrefix = "/" + lang.Lang
}

var multilingual *Multilingual

if s.owner != nil {
multilingual = s.owner.multilingual
}

s.Info = SiteInfo{
BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))),
Title: lang.GetString("Title"),
Author: lang.GetStringMap("author"),
Social: lang.GetStringMapString("social"),
LanguageCode: lang.GetString("languagecode"),
Copyright: lang.GetString("copyright"),
DisqusShortname: lang.GetString("DisqusShortname"),
// TODO(bep) ml consolidate the below (make into methods etc.)
multilingual: s.Multilingual,
BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))),
Title: lang.GetString("Title"),
Author: lang.GetStringMap("author"),
Social: lang.GetStringMapString("social"),
LanguageCode: lang.GetString("languagecode"),
Copyright: lang.GetString("copyright"),
DisqusShortname: lang.GetString("DisqusShortname"),
multilingual: multilingual,
Language: lang,
LanguagePrefix: languagePrefix,
Languages: languages,
Expand Down Expand Up @@ -1420,8 +1422,8 @@ func (s *Site) renderAliases() error {
}
}

if s.Multilingual.enabled() {
mainLang := s.Multilingual.DefaultLang.Lang
if s.owner.multilingual.enabled() {
mainLang := s.owner.multilingual.DefaultLang.Lang
mainLangURL := helpers.AbsURL(mainLang, false)
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL); err != nil {
Expand Down Expand Up @@ -1450,7 +1452,7 @@ func (s *Site) renderPages() error {
var layouts []string
if !p.IsRenderable() {
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 {
results <- err
continue
Expand Down Expand Up @@ -2154,7 +2156,7 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts

func (s *Site) findFirstLayout(layouts ...string) (string, bool) {
for _, layout := range layouts {
if s.Tmpl.Lookup(layout) != nil {
if s.owner.tmpl.Lookup(layout) != nil {
return layout, true
}
}
Expand All @@ -2164,7 +2166,7 @@ func (s *Site) findFirstLayout(layouts ...string) (string, bool) {
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 templ := s.Tmpl.Lookup(layout); templ != nil {
if templ := s.owner.tmpl.Lookup(layout); templ != nil {
return templ.Execute(w, d)
}
return fmt.Errorf("Layout not found: %s", layout)
Expand Down

0 comments on commit 24ba359

Please sign in to comment.