diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-03-25 18:18:34 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-03-26 10:20:40 +0100 |
commit | 4dae52af680e6ff2c8cdeb4ce1f219330b27001c (patch) | |
tree | db157b09fc15b25a07512581fbd80536fe8e18ee /hugolib | |
parent | 794d4052b87c98943588b35e1cfecc06e6a0c7f2 (diff) |
Avoid nilpointer on no File on Page
Fixes #5781
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/hugo_sites.go | 2 | ||||
-rw-r--r-- | hugolib/page.go | 8 | ||||
-rw-r--r-- | hugolib/page__meta.go | 8 | ||||
-rw-r--r-- | hugolib/page__new.go | 4 | ||||
-rw-r--r-- | hugolib/page__paths.go | 2 | ||||
-rw-r--r-- | hugolib/page_test.go | 8 | ||||
-rw-r--r-- | hugolib/site.go | 4 |
7 files changed, 24 insertions, 12 deletions
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 0c79f4bdb..9fe2a5bdb 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -577,7 +577,7 @@ func (cfg *BuildCfg) shouldRender(p *pageState) bool { return true } - if cfg.whatChanged != nil && p.File() != nil { + if cfg.whatChanged != nil && !p.File().IsZero() { return cfg.whatChanged.files[p.File().Filename()] } diff --git a/hugolib/page.go b/hugolib/page.go index 576342cfa..99b771eee 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -236,7 +236,7 @@ func (p *pageState) TranslationKey() string { p.translationKeyInit.Do(func() { if p.m.translationKey != "" { p.translationKey = p.Kind() + "/" + p.m.translationKey - } else if p.IsPage() && p.File() != nil { + } else if p.IsPage() && !p.File().IsZero() { p.translationKey = path.Join(p.Kind(), filepath.ToSlash(p.File().Dir()), p.File().TranslationBaseName()) } else if p.IsNode() { p.translationKey = path.Join(p.Kind(), p.SectionsPath()) @@ -462,7 +462,7 @@ func (p *pageState) Render(layout ...string) template.HTML { func (p *pageState) wrapError(err error) error { var filename string - if p.File() != nil { + if !p.File().IsZero() { filename = p.File().Filename() } @@ -665,7 +665,7 @@ func (p *pageState) parseError(err error, input []byte, offset int) error { } func (p *pageState) pathOrTitle() string { - if p.File() != nil { + if !p.File().IsZero() { return p.File().Filename() } @@ -763,7 +763,7 @@ func (p *pageState) sortParentSections() { // For pages that do not (sections witout content page etc.), it returns the // virtual path, consistent with where you would add a source file. func (p *pageState) sourceRef() string { - if p.File() != nil { + if !p.File().IsZero() { sourcePath := p.File().Path() if sourcePath != "" { return "/" + filepath.ToSlash(sourcePath) diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 8532f5016..2c6b0a85d 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -222,7 +222,7 @@ func (p *pageMeta) Params() map[string]interface{} { } func (p *pageMeta) Path() string { - if p.File() != nil { + if !p.File().IsZero() { return p.File().Path() } return p.SectionsPath() @@ -256,7 +256,7 @@ func (p *pageMeta) Section() string { return p.sections[0] } - if p.File() != nil { + if !p.File().IsZero() { return p.File().Section() } @@ -536,8 +536,8 @@ func (pm *pageMeta) setMetadata(p *pageState, frontmatter map[string]interface{} func (p *pageMeta) applyDefaultValues() error { if p.markup == "" { - if p.File() != nil { - // Fall back to {file extension + if !p.File().IsZero() { + // Fall back to file extension p.markup = helpers.GuessType(p.File().Ext()) } if p.markup == "" { diff --git a/hugolib/page__new.go b/hugolib/page__new.go index 0f419b5da..0ac14a5e8 100644 --- a/hugolib/page__new.go +++ b/hugolib/page__new.go @@ -95,6 +95,10 @@ func newPageBase(metaProvider *pageMeta) (*pageState, error) { } func newPageFromMeta(metaProvider *pageMeta) (*pageState, error) { + if metaProvider.f == nil { + metaProvider.f = page.NewZeroFile(metaProvider.s.DistinctWarningLog) + } + ps, err := newPageBase(metaProvider) if err != nil { return nil, err diff --git a/hugolib/page__paths.go b/hugolib/page__paths.go index 0a5dad5ef..8bc7a7535 100644 --- a/hugolib/page__paths.go +++ b/hugolib/page__paths.go @@ -99,7 +99,7 @@ func createTargetPathDescriptor(s *Site, p page.Page, pm *pageMeta) (page.Target d := s.Deps - if p.File() != nil { + if !p.File().IsZero() { dir = p.File().Dir() baseName = p.File().TranslationBaseName() } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 5e9ac696c..1e362e0dc 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -18,6 +18,8 @@ import ( "html/template" "os" + "github.com/gohugoio/hugo/common/loggers" + "path/filepath" "strings" "testing" @@ -1166,6 +1168,12 @@ Content:{{ .Content }} } +// https://github.com/gohugoio/hugo/issues/5781 +func TestPageWithZeroFile(t *testing.T) { + newTestSitesBuilder(t).WithLogger(loggers.NewWarningLogger()).WithSimpleConfigFile(). + WithTemplatesAdded("index.html", "{{ .File.Filename }}{{ with .File }}{{ .Dir }}{{ end }}").Build(BuildCfg{}) +} + func TestShouldBuild(t *testing.T) { t.Parallel() var past = time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) diff --git a/hugolib/site.go b/hugolib/site.go index 145ae2d49..1bf2d639c 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -789,11 +789,11 @@ func (s *siteRefLinker) refLink(ref string, source interface{}, relative bool, o if refURL.Fragment != "" { _ = target link = link + "#" + refURL.Fragment - if pctx, ok := target.(pageContext); ok && target.File() != nil && !pctx.getRenderingConfig().PlainIDAnchors { + if pctx, ok := target.(pageContext); ok && !target.File().IsZero() && !pctx.getRenderingConfig().PlainIDAnchors { if refURL.Path != "" { link = link + ":" + target.File().UniqueID() } - } else if pctx, ok := p.(pageContext); ok && p.File() != nil && !pctx.getRenderingConfig().PlainIDAnchors { + } else if pctx, ok := p.(pageContext); ok && !p.File().IsZero() && !pctx.getRenderingConfig().PlainIDAnchors { link = link + ":" + p.File().UniqueID() } |