mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
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:
parent
9798a95670
commit
d953e39e63
5 changed files with 63 additions and 61 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -92,7 +100,6 @@ func (h *HugoSites) reset() {
|
|||
}
|
||||
|
||||
func (h *HugoSites) reCreateFromConfig() error {
|
||||
oldSite := h.Sites[0]
|
||||
sites, err := createSitesFromConfig()
|
||||
|
||||
if err != nil {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -423,7 +420,7 @@ type runmode struct {
|
|||
}
|
||||
|
||||
func (s *Site) running() bool {
|
||||
return s.RunMode.Watching
|
||||
return s.owner.runMode.Watching
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
@ -602,10 +599,10 @@ 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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -613,12 +610,12 @@ 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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue