diff --git a/common/urls/baseURL.go b/common/urls/baseURL.go index f3d2c82f335..9b1ba7db885 100644 --- a/common/urls/baseURL.go +++ b/common/urls/baseURL.go @@ -75,7 +75,7 @@ func (b BaseURL) URL() *url.URL { return &c } -func newBaseURLFromString(b string) (BaseURL, error) { +func NewBaseURLFromString(b string) (BaseURL, error) { var result BaseURL base, err := url.Parse(b) diff --git a/common/urls/baseURL_test.go b/common/urls/baseURL_test.go index 3d073df3ce3..9279ffa955f 100644 --- a/common/urls/baseURL_test.go +++ b/common/urls/baseURL_test.go @@ -21,7 +21,7 @@ import ( func TestBaseURL(t *testing.T) { c := qt.New(t) - b, err := newBaseURLFromString("http://example.com") + b, err := NewBaseURLFromString("http://example.com") c.Assert(err, qt.IsNil) c.Assert(b.String(), qt.Equals, "http://example.com") @@ -36,7 +36,7 @@ func TestBaseURL(t *testing.T) { _, err = b.WithProtocol("mailto:") c.Assert(err, qt.Not(qt.IsNil)) - b, err = newBaseURLFromString("mailto:hugo@rules.com") + b, err = NewBaseURLFromString("mailto:hugo@rules.com") c.Assert(err, qt.IsNil) c.Assert(b.String(), qt.Equals, "mailto:hugo@rules.com") @@ -51,16 +51,16 @@ func TestBaseURL(t *testing.T) { // Test with "non-URLs". Some people will try to use these as a way to get // relative URLs working etc. - b, err = newBaseURLFromString("/") + b, err = NewBaseURLFromString("/") c.Assert(err, qt.IsNil) c.Assert(b.String(), qt.Equals, "/") - b, err = newBaseURLFromString("") + b, err = NewBaseURLFromString("") c.Assert(err, qt.IsNil) c.Assert(b.String(), qt.Equals, "") // BaseURL with sub path - b, err = newBaseURLFromString("http://example.com/sub") + b, err = NewBaseURLFromString("http://example.com/sub") c.Assert(err, qt.IsNil) c.Assert(b.String(), qt.Equals, "http://example.com/sub") c.Assert(b.HostURL(), qt.Equals, "http://example.com") diff --git a/config/allconfig/allconfig.go b/config/allconfig/allconfig.go index 44ac04759c1..8afb0eb3890 100644 --- a/config/allconfig/allconfig.go +++ b/config/allconfig/allconfig.go @@ -24,12 +24,12 @@ import ( "github.com/gohugoio/hugo/cache/filecache" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/types" + "github.com/gohugoio/hugo/common/urls" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/config/privacy" "github.com/gohugoio/hugo/config/security" "github.com/gohugoio/hugo/config/services" "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/hugolib/paths" "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/markup/markup_config" "github.com/gohugoio/hugo/media" @@ -163,8 +163,14 @@ func (c *Config) Compile() error { disabledLangs[lang] = true } + baseURL, err := urls.NewBaseURLFromString(c.BaseURL) + if err != nil { + return err + } + c.C = ConfigCompiled{ Timeout: timeout, + BaseURL: baseURL, DisabledKinds: disabledKinds, DisabledLanguages: disabledLangs, KindOutputFormats: kindOutputFormats, @@ -185,7 +191,7 @@ func (c Config) IsLangDisabled(lang string) bool { // ConfigCompiled holds values and functions that are derived from the config. type ConfigCompiled struct { Timeout time.Duration - BaseURL paths.BaseURL + BaseURL urls.BaseURL KindOutputFormats map[string]output.Formats DisabledKinds map[string]bool DisabledLanguages map[string]bool diff --git a/config/compositeConfig.go b/config/compositeConfig.go deleted file mode 100644 index ff2660c1cf7..00000000000 --- a/config/compositeConfig.go +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2021 The Hugo Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package config - -import ( - "github.com/gohugoio/hugo/common/maps" -) - -// NewCompositeConfig creates a new composite Provider with a read-only base -// and a writeable layer. -func NewCompositeConfig(base, layer Provider) Provider { - return &compositeConfig{ - base: base, - layer: layer, - } -} - -// compositeConfig contains a read only config base with -// a possibly writeable config layer on top. -type compositeConfig struct { - base Provider - layer Provider -} - -func (c *compositeConfig) GetBool(key string) bool { - if c.layer.IsSet(key) { - return c.layer.GetBool(key) - } - return c.base.GetBool(key) -} - -func (c *compositeConfig) GetInt(key string) int { - if c.layer.IsSet(key) { - return c.layer.GetInt(key) - } - return c.base.GetInt(key) -} - -func (c *compositeConfig) Merge(key string, value any) { - c.layer.Merge(key, value) -} - -func (c *compositeConfig) GetParams(key string) maps.Params { - if c.layer.IsSet(key) { - return c.layer.GetParams(key) - } - return c.base.GetParams(key) -} - -func (c *compositeConfig) GetStringMap(key string) map[string]any { - if c.layer.IsSet(key) { - return c.layer.GetStringMap(key) - } - return c.base.GetStringMap(key) -} - -func (c *compositeConfig) GetStringMapString(key string) map[string]string { - if c.layer.IsSet(key) { - return c.layer.GetStringMapString(key) - } - return c.base.GetStringMapString(key) -} - -func (c *compositeConfig) GetStringSlice(key string) []string { - if c.layer.IsSet(key) { - return c.layer.GetStringSlice(key) - } - return c.base.GetStringSlice(key) -} - -func (c *compositeConfig) Get(key string) any { - if c.layer.IsSet(key) { - return c.layer.Get(key) - } - return c.base.Get(key) -} - -func (c *compositeConfig) IsSet(key string) bool { - if c.layer.IsSet(key) { - return true - } - return c.base.IsSet(key) -} - -func (c *compositeConfig) GetString(key string) string { - if c.layer.IsSet(key) { - return c.layer.GetString(key) - } - return c.base.GetString(key) -} - -func (c *compositeConfig) Set(key string, value any) { - c.layer.Set(key, value) -} - -func (c *compositeConfig) SetDefaults(params maps.Params) { - c.layer.SetDefaults(params) -} - -func (c *compositeConfig) WalkParams(walkFn func(params ...maps.KeyParams) bool) { - panic("not supported") -} - -func (c *compositeConfig) SetDefaultMergeStrategy() { - panic("not supported") -} diff --git a/config/compositeConfig_test.go b/config/compositeConfig_test.go deleted file mode 100644 index 60644102fd2..00000000000 --- a/config/compositeConfig_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2021 The Hugo Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package config - -import ( - "testing" - - qt "github.com/frankban/quicktest" -) - -func TestCompositeConfig(t *testing.T) { - c := qt.New(t) - - c.Run("Set and get", func(c *qt.C) { - base, layer := New(), New() - cfg := NewCompositeConfig(base, layer) - - layer.Set("a1", "av") - base.Set("b1", "bv") - cfg.Set("c1", "cv") - - c.Assert(cfg.Get("a1"), qt.Equals, "av") - c.Assert(cfg.Get("b1"), qt.Equals, "bv") - c.Assert(cfg.Get("c1"), qt.Equals, "cv") - c.Assert(cfg.IsSet("c1"), qt.IsTrue) - c.Assert(layer.IsSet("c1"), qt.IsTrue) - c.Assert(base.IsSet("c1"), qt.IsFalse) - }) -} diff --git a/helpers/pathspec.go b/helpers/pathspec.go index b8ca0d930da..9bf8ede4f07 100644 --- a/helpers/pathspec.go +++ b/helpers/pathspec.go @@ -69,7 +69,7 @@ func NewPathSpecWithBaseBaseFsProvided(fs *hugofs.Fs, cfg config.AllProvider, lo ProcessingStats: NewProcessingStats(p.Lang()), } - basePath := ps.BaseURL.Path() + basePath := cfg.BaseURL().Path() if basePath != "" && basePath != "/" { ps.BasePath = basePath } diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index a0ac324ef9b..33910836ca0 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -586,7 +586,7 @@ func (cfg *BuildCfg) shouldRender(p *pageState) bool { } func (h *HugoSites) renderCrossSitesSitemap() error { - if !h.isMultiLingual() || h.IsMultihost() { + if !h.isMultiLingual() || h.Conf.IsMultihost() { return nil } diff --git a/hugolib/hugo_smoke_test.go b/hugolib/hugo_smoke_test.go index eacc2cf9e63..604e82afce3 100644 --- a/hugolib/hugo_smoke_test.go +++ b/hugolib/hugo_smoke_test.go @@ -26,7 +26,7 @@ import ( func TestHello(t *testing.T) { files := ` -- hugo.toml -- -title: "Hello" +title = "Hello" baseURL="https://example.org" disableKinds = ["term", "taxonomy", "section", "page"] -- content/p1.md -- diff --git a/hugolib/page__paths.go b/hugolib/page__paths.go index 1956e1508cd..e83d348cb74 100644 --- a/hugolib/page__paths.go +++ b/hugolib/page__paths.go @@ -129,7 +129,7 @@ func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.Target Kind: p.Kind(), Sections: p.SectionsEntries(), UglyURLs: false, // TODO1 s.uglyURLs(p), - ForcePrefix: s.h.IsMultihost() || alwaysInSubDir, + ForcePrefix: s.h.Conf.IsMultihost() || alwaysInSubDir, Dir: dir, URL: pm.urlPaths.URL, } diff --git a/hugolib/paths/paths.go b/hugolib/paths/paths.go index 74c708878c1..ddabdaf0b91 100644 --- a/hugolib/paths/paths.go +++ b/hugolib/paths/paths.go @@ -19,6 +19,7 @@ import ( "strings" hpaths "github.com/gohugoio/hugo/common/paths" + "github.com/gohugoio/hugo/common/urls" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/modules" @@ -32,7 +33,7 @@ type Paths struct { Fs *hugofs.Fs Cfg config.AllProvider - BaseURL + // TODO1 get rid of these. BaseURLString string BaseURLNoPathString string @@ -57,7 +58,7 @@ type Paths struct { func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) { baseURLstr := "https://example.com" // TODOD1 cfg.GetString("baseURL") - baseURL, err := newBaseURLFromString(baseURLstr) + baseURL, err := urls.NewBaseURLFromString(baseURLstr) if err != nil { return nil, fmt.Errorf("Failed to create baseURL from %q:: %w", baseURLstr, err) } @@ -97,9 +98,9 @@ func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) { var baseURLNoPathString = baseURLNoPath.String() p := &Paths{ - Fs: fs, - Cfg: cfg, - BaseURL: baseURL, + Fs: fs, + Cfg: cfg, + // TODO1 BaseURL: baseURL, BaseURLString: baseURLString, BaseURLNoPathString: baseURLNoPathString, AbsResourcesDir: absResourcesDir, diff --git a/hugolib/site.go b/hugolib/site.go index ce69e3d359a..fd52a66a3ea 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1114,7 +1114,7 @@ func (s *Site) createNodeMenuEntryURL(in string) string { menuEntryURL := in menuEntryURL = helpers.SanitizeURLKeepTrailingSlash(s.s.PathSpec.URLize(menuEntryURL)) if !s.conf.CanonifyURLs { - menuEntryURL = paths.AddContextRoot(s.s.PathSpec.BaseURL.String(), menuEntryURL) + menuEntryURL = paths.AddContextRoot(s.s.PathSpec.Cfg.BaseURL().String(), menuEntryURL) } return menuEntryURL } @@ -1214,7 +1214,7 @@ func (s *Site) assembleMenus() { // get any language code to prefix the target file path with. func (s *Site) getLanguageTargetPathLang(alwaysInSubDir bool) string { - if s.h.IsMultihost() { + if s.h.Conf.IsMultihost() { return s.Language().Lang } @@ -1223,7 +1223,7 @@ func (s *Site) getLanguageTargetPathLang(alwaysInSubDir bool) string { // get any lanaguagecode to prefix the relative permalink with. func (s *Site) getLanguagePermalinkLang(alwaysInSubDir bool) string { - if !s.h.isMultiLingual() || s.h.IsMultihost() { + if !s.h.isMultiLingual() || s.h.Conf.IsMultihost() { return "" } @@ -1316,7 +1316,7 @@ func (s *Site) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (page.Page, } func (s *Site) permalink(link string) string { - return s.PathSpec.PermalinkForBaseURL(link, s.PathSpec.BaseURL.String()) + return s.PathSpec.PermalinkForBaseURL(link, s.PathSpec.Cfg.BaseURL().String()) } func (s *Site) absURLPath(targetPath string) string { @@ -1324,7 +1324,7 @@ func (s *Site) absURLPath(targetPath string) string { if s.conf.RelativeURLs { path = helpers.GetDottedRelativePath(targetPath) } else { - url := s.PathSpec.BaseURL.String() + url := s.PathSpec.Cfg.BaseURL().String() if !strings.HasSuffix(url, "/") { url += "/" } @@ -1401,8 +1401,8 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath s pd.AbsURLPath = s.absURLPath(targetPath) } - if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") { - pd.LiveReloadBaseURL = s.PathSpec.BaseURL.URL() + if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") { // TODOD1 + pd.LiveReloadBaseURL = s.PathSpec.Cfg.BaseURL().URL() if s.Cfg.GetInt("liveReloadPort") != -1 { pd.LiveReloadBaseURL.Host = fmt.Sprintf("%s:%d", pd.LiveReloadBaseURL.Hostname(), s.Cfg.GetInt("liveReloadPort")) } diff --git a/hugolib/site_render.go b/hugolib/site_render.go index b48ef059f45..db3f4f3e1f8 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -384,7 +384,7 @@ func (s *Site) renderAliases() error { // renderMainLanguageRedirect creates a redirect to the main language home, // depending on if it lives in sub folder (e.g. /en) or not. func (s *Site) renderMainLanguageRedirect() error { - if !s.h.isMultiLingual() || s.h.IsMultihost() { + if !s.h.isMultiLingual() || s.h.Conf.IsMultihost() { // No need for a redirect return nil } diff --git a/resources/page/page_paths.go b/resources/page/page_paths.go index 8c718fd7781..20f2383da26 100644 --- a/resources/page/page_paths.go +++ b/resources/page/page_paths.go @@ -94,12 +94,12 @@ func (p TargetPaths) PermalinkForOutputFormat(s *helpers.PathSpec, f output.Form var baseURL string var err error if f.Protocol != "" { - baseURL, err = s.BaseURL.WithProtocol(f.Protocol) + baseURL, err = s.Cfg.BaseURL().WithProtocol(f.Protocol) if err != nil { return "" } } else { - baseURL = s.BaseURL.String() + baseURL = s.Cfg.BaseURL().String() } return s.PermalinkForBaseURL(p.Link, baseURL) diff --git a/resources/resource.go b/resources/resource.go index 45dd22b2c44..cccaf392adf 100644 --- a/resources/resource.go +++ b/resources/resource.go @@ -297,7 +297,7 @@ func (l *genericResource) Params() maps.Params { } func (l *genericResource) Permalink() string { - return l.spec.PermalinkForBaseURL(l.relPermalinkForRel(l.relTargetDirFile.path(), true), l.spec.BaseURL.HostURL()) + return l.spec.PermalinkForBaseURL(l.relPermalinkForRel(l.relTargetDirFile.path(), true), l.spec.Cfg.BaseURL().HostURL()) } func (l *genericResource) Publish() error { @@ -506,7 +506,7 @@ func (r *genericResource) openPublishFileForWriting(relTargetPath string) (io.Wr } func (l *genericResource) permalinkFor(target string) string { - return l.spec.PermalinkForBaseURL(l.relPermalinkForRel(target, true), l.spec.BaseURL.HostURL()) + return l.spec.PermalinkForBaseURL(l.relPermalinkForRel(target, true), l.spec.Cfg.BaseURL().HostURL()) } func (l *genericResource) relPermalinkFor(target string) string {