]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Deprecate site.Language.Params and some other fixes
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 17 May 2023 07:59:57 +0000 (09:59 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 17 May 2023 20:13:29 +0000 (22:13 +0200)
Updates #10947

hugolib/config_test.go
hugolib/hugo_sites_build.go
hugolib/site_new.go
langs/language.go
resources/page/site.go

index aa7785f929c423ff87fac65f32753c6060bda1e1..93060134d362cc5463106c4351a96999dba90b44 100644 (file)
@@ -742,19 +742,35 @@ themeconfigdirparam: {{ site.Params.themeconfigdirparam }}
 
 }
 
-// TODO(beo) find a better place for this.
 func TestReproCommentsIn10947(t *testing.T) {
        t.Parallel()
 
        files := `
 -- hugo.toml --
 baseURL = "https://example.com"
--- content/mysection/_index.md --
+disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
+[languages]
+[languages.en]
+title = "English Title"
+[languages.en.params]
+myparam = "enParamValue"
+[languages.sv]
+title = "Svensk Title"
+[languages.sv.params]
+myparam = "svParamValue"
+-- content/mysection/_index.en.md --
+---
+title: "My English Section"
 ---
-title: "My Section"
+-- content/mysection/_index.sv.md --
+---
+title: "My Swedish Section"
 ---
 -- layouts/index.html --
-Sections: {{ if site.Sections }}true{{ end }}|
+{{ range $i, $e := (slice site .Site) }}
+{{ $i }}|AllPages: {{ len .AllPages }}|Sections: {{ if .Sections }}true{{ end }}| Author: {{ .Authors }}|BuildDrafts: {{ .BuildDrafts }}|IsMultiLingual: {{ .IsMultiLingual }}|Param: {{ .Language.Params.myparam }}|
+{{ end }}
+
 
 
 `
@@ -765,6 +781,18 @@ Sections: {{ if site.Sections }}true{{ end }}|
                },
        ).Build()
 
-       b.AssertFileContent("public/index.html", "Sections: true|")
+       b.Assert(b.H.Log.LogCounters().WarnCounter.Count(), qt.Equals, uint64(2))
+       b.AssertFileContent("public/index.html", `
+AllPages: 4|
+Sections: true|
+Param: enParamValue    
+Param: enParamValue    
+IsMultiLingual: true
+`)
+
+       b.AssertFileContent("public/sv/index.html", `
+Param: svParamValue    
+
+`)
 
 }
index 52ed34bf38b702da951b9c904a1e8aebdcdb4f66..f86d425b366d60fb29e07fb8ac1bc998787b280a 100644 (file)
@@ -22,6 +22,7 @@ import (
        "strings"
        "time"
 
+       "github.com/gohugoio/hugo/langs"
        "github.com/gohugoio/hugo/publisher"
 
        "github.com/gohugoio/hugo/hugofs"
@@ -40,6 +41,11 @@ import (
        "github.com/gohugoio/hugo/helpers"
 )
 
+func init() {
+       // To avoid circular dependencies, we set this here.
+       langs.DeprecationFunc = helpers.Deprecated
+}
+
 // Build builds all sites. If filesystem events are provided,
 // this is considered to be a potential partial rebuild.
 func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
index f449b857a4f1f4b6d410b1b731dec2321ea81065..2b959a8da2e825aa95c7ce6a203b7cdf6bc6a43b 100644 (file)
@@ -132,6 +132,8 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
                        return nil, err
                }
 
+               langs.SetParams(language, conf.Params)
+
                s := &Site{
                        conf:     conf,
                        language: language,
@@ -411,6 +413,10 @@ func (s *Site) Author() map[string]any {
        return s.conf.Author
 }
 
+func (s *Site) Authors() page.AuthorList {
+       return page.AuthorList{}
+}
+
 func (s *Site) Social() map[string]string {
        return s.conf.Social
 }
@@ -434,6 +440,14 @@ func (s *Site) Data() map[string]any {
        return s.s.h.Data()
 }
 
+func (s *Site) BuildDrafts() bool {
+       return s.conf.BuildDrafts
+}
+
+func (s *Site) IsMultiLingual() bool {
+       return s.h.isMultiLingual()
+}
+
 func (s *Site) LanguagePrefix() string {
        conf := s.s.Conf
        if !conf.IsMultiLingual() {
index c904b0c6b0fbd7ca90aed0aa2a65e164a8caa925..5bf80274d039ad0fc2c82ebd1ac6503259ab17a4 100644 (file)
@@ -23,6 +23,7 @@ import (
        "golang.org/x/text/language"
 
        "github.com/gohugoio/hugo/common/htime"
+       "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/locales"
        translators "github.com/gohugoio/localescompressed"
 )
@@ -42,6 +43,9 @@ type Language struct {
        tag           language.Tag
        collator      *Collator
        location      *time.Location
+
+       // This is just an alias of Site.Params.
+       params maps.Params
 }
 
 // NewLanguage creates a new language.
@@ -76,7 +80,25 @@ func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig L
        }
 
        return l, l.loadLocation(timeZone)
+}
+
+// This is injected from hugolib to avoid cirular dependencies.
+var DeprecationFunc = func(item, alternative string, err bool) {}
+
+const paramsDeprecationWarning = `.Language.Params is deprecated and will be removed in a future release. Use site.Params instead.
+
+Also, for all but custom parameters, you need to use the built in Hugo variables, e.g. site.Title, site.LanguageCode; site.Language.Params.Title will not work.
 
+See https://gohugo.io/content-management/multilingual/#changes-in-hugo-01120
+
+`
+
+// Params returns the language params.
+// Note that this is the same as the Site.Params, but we keep it here for legacy reasons.
+// Deprecated: Use the site.Params instead.
+func (l *Language) Params() maps.Params {
+       DeprecationFunc(".Language.Params", paramsDeprecationWarning, false)
+       return l.params
 }
 
 func (l *Language) loadLocation(tzStr string) error {
@@ -113,6 +135,10 @@ func (l Languages) AsOrdinalSet() map[string]int {
 // Internal access to unexported Language fields.
 // This construct is to prevent them from leaking to the templates.
 
+func SetParams(l *Language, params maps.Params) {
+       l.params = params
+}
+
 func GetTimeFormatter(l *Language) htime.TimeFormatter {
        return l.timeFormatter
 }
index 67b15cd8a3e31b345b4dadf8d6e7b1c7fff0d505..d36857bb1b5c673d423833818258dc058d54bba6 100644 (file)
@@ -37,6 +37,9 @@ type Site interface {
 
        GetPage(ref ...string) (Page, error)
 
+       // AllPages returns all pages for all languages.
+       AllPages() Pages
+
        // Returns all the regular Pages in this Site.
        RegularPages() Pages
 
@@ -104,6 +107,9 @@ type Site interface {
        // Author is deprecated and will be removed in a future release.
        Author() map[string]interface{}
 
+       // Authors is deprecated and will be removed in a future release.
+       Authors() AuthorList
+
        // Returns the social links for this site.
        Social() map[string]string
 
@@ -115,6 +121,12 @@ type Site interface {
 
        // For internal use only.
        GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error)
+
+       // BuildDraft is deprecated and will be removed in a future release.
+       BuildDrafts() bool
+
+       // IsMultilingual reports whether this site is configured with more than one language.
+       IsMultiLingual() bool
 }
 
 // Sites represents an ordered list of sites (languages).
@@ -146,6 +158,9 @@ func (s *siteWrapper) Social() map[string]string {
 func (s *siteWrapper) Author() map[string]interface{} {
        return s.s.Author()
 }
+func (s *siteWrapper) Authors() AuthorList {
+       return AuthorList{}
+}
 
 func (s *siteWrapper) GoogleAnalytics() string {
        return s.s.GoogleAnalytics()
@@ -159,6 +174,10 @@ func (s *siteWrapper) Language() *langs.Language {
        return s.s.Language()
 }
 
+func (s *siteWrapper) AllPages() Pages {
+       return s.s.AllPages()
+}
+
 func (s *siteWrapper) RegularPages() Pages {
        return s.s.RegularPages()
 }
@@ -247,6 +266,14 @@ func (s *siteWrapper) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Pag
        return s.s.GetPageWithTemplateInfo(info, ref...)
 }
 
+func (s *siteWrapper) BuildDrafts() bool {
+       return s.s.BuildDrafts()
+}
+
+func (s *siteWrapper) IsMultiLingual() bool {
+       return s.s.IsMultiLingual()
+}
+
 func (s *siteWrapper) DisqusShortname() string {
        return s.s.DisqusShortname()
 }
@@ -259,6 +286,9 @@ type testSite struct {
 func (s testSite) Author() map[string]interface{} {
        return nil
 }
+func (s testSite) Authors() AuthorList {
+       return AuthorList{}
+}
 
 func (s testSite) Social() map[string]string {
        return make(map[string]string)
@@ -332,6 +362,10 @@ func (t testSite) Pages() Pages {
        return nil
 }
 
+func (t testSite) AllPages() Pages {
+       return nil
+}
+
 func (t testSite) RegularPages() Pages {
        return nil
 }
@@ -368,6 +402,14 @@ func (testSite) DisqusShortname() string {
        return ""
 }
 
+func (s testSite) BuildDrafts() bool {
+       return false
+}
+
+func (s testSite) IsMultiLingual() bool {
+       return false
+}
+
 // NewDummyHugoSite creates a new minimal test site.
 func NewDummyHugoSite(cfg config.Provider) Site {
        return testSite{