Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetPage, ref and relref improvements #4796

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions hugolib/disableKinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,23 @@ func assertDisabledKinds(th testHelper, s *Site, disabled ...string) {
}, disabled, KindPage, "public/sect/p1/index.html", "Single|P1")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindHome)
p, _ := s.getPage(nil, "/")
if isDisabled {
return p == nil
}
return p != nil
}, disabled, KindHome, "public/index.html", "Home")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindSection, "sect")
p, _ := s.getPage(nil, "sect")
if isDisabled {
return p == nil
}
return p != nil
}, disabled, KindSection, "public/sect/index.html", "Sects")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindTaxonomy, "tags", "tag1")
p, _ := s.getPage(nil, "tags/tag1")

if isDisabled {
return p == nil
Expand All @@ -156,7 +156,7 @@ func assertDisabledKinds(th testHelper, s *Site, disabled ...string) {
}, disabled, KindTaxonomy, "public/tags/tag1/index.html", "Tag1")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindTaxonomyTerm, "tags")
p, _ := s.getPage(nil, "tags")
if isDisabled {
return p == nil
}
Expand All @@ -165,7 +165,7 @@ func assertDisabledKinds(th testHelper, s *Site, disabled ...string) {
}, disabled, KindTaxonomyTerm, "public/tags/index.html", "Tags")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindTaxonomyTerm, "categories")
p, _ := s.getPage(nil, "categories")

if isDisabled {
return p == nil
Expand All @@ -175,7 +175,7 @@ func assertDisabledKinds(th testHelper, s *Site, disabled ...string) {
}, disabled, KindTaxonomyTerm, "public/categories/index.html", "Category Terms")
assertDisabledKind(th,
func(isDisabled bool) bool {
p := s.getPage(KindTaxonomy, "categories", "hugo")
p, _ := s.getPage(nil, "categories/hugo")
if isDisabled {
return p == nil
}
Expand Down
30 changes: 15 additions & 15 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ p1 = "p1en"
assert.Len(sites, 2)

nnSite := sites[0]
nnHome := nnSite.getPage(KindHome)
nnHome, _ := nnSite.getPage(nil, "/")
assert.Len(nnHome.AllTranslations(), 2)
assert.Len(nnHome.Translations(), 1)
assert.True(nnHome.IsTranslated())

enHome := sites[1].getPage(KindHome)
enHome, _ := sites[1].getPage(nil, "/")

p1, err := enHome.Param("p1")
assert.NoError(err)
Expand Down Expand Up @@ -242,7 +242,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Nil(t, gp2)

enSite := sites[0]
enSiteHome := enSite.getPage(KindHome)
enSiteHome, _ := enSite.getPage(nil, "/")
require.True(t, enSiteHome.IsTranslated())

require.Equal(t, "en", enSite.Language.Lang)
Expand Down Expand Up @@ -310,10 +310,10 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
// isn't ideal in a multilingual setup. You want a way to get the current language version if available.
// Now you can do lookups with translation base name to get that behaviour.
// Let us test all the regular page variants:
getPageDoc1En := enSite.getPage(KindPage, filepath.ToSlash(doc1en.Path()))
getPageDoc1EnBase := enSite.getPage(KindPage, "sect/doc1")
getPageDoc1Fr := frSite.getPage(KindPage, filepath.ToSlash(doc1fr.Path()))
getPageDoc1FrBase := frSite.getPage(KindPage, "sect/doc1")
getPageDoc1En, _ := enSite.getPage(nil, filepath.ToSlash(doc1en.Path()))
getPageDoc1EnBase, _ := enSite.getPage(nil, "sect/doc1")
getPageDoc1Fr, _ := frSite.getPage(nil, filepath.ToSlash(doc1fr.Path()))
getPageDoc1FrBase, _ := frSite.getPage(nil, "sect/doc1")
require.Equal(t, doc1en, getPageDoc1En)
require.Equal(t, doc1fr, getPageDoc1Fr)
require.Equal(t, doc1en, getPageDoc1EnBase)
Expand All @@ -331,7 +331,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Single", "Shortcode: Hello", "LingoDefault")

// Check node translations
homeEn := enSite.getPage(KindHome)
homeEn, _ := enSite.getPage(nil, "/")
require.NotNil(t, homeEn)
require.Len(t, homeEn.Translations(), 3)
require.Equal(t, "fr", homeEn.Translations()[0].Lang())
Expand All @@ -341,7 +341,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Equal(t, "På bokmål", homeEn.Translations()[2].title, configSuffix)
require.Equal(t, "Bokmål", homeEn.Translations()[2].Language().LanguageName, configSuffix)

sectFr := frSite.getPage(KindSection, "sect")
sectFr, _ := frSite.getPage(nil, "sect")
require.NotNil(t, sectFr)

require.Equal(t, "fr", sectFr.Lang())
Expand All @@ -351,12 +351,12 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {

nnSite := sites[2]
require.Equal(t, "nn", nnSite.Language.Lang)
taxNn := nnSite.getPage(KindTaxonomyTerm, "lag")
taxNn, _ := nnSite.getPage(nil, "lag")
require.NotNil(t, taxNn)
require.Len(t, taxNn.Translations(), 1)
require.Equal(t, "nb", taxNn.Translations()[0].Lang())

taxTermNn := nnSite.getPage(KindTaxonomy, "lag", "sogndal")
taxTermNn, _ := nnSite.getPage(nil, "lag/sogndal")
require.NotNil(t, taxTermNn)
require.Len(t, taxTermNn.Translations(), 1)
require.Equal(t, "nb", taxTermNn.Translations()[0].Lang())
Expand Down Expand Up @@ -411,7 +411,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
}

// Check bundles
bundleFr := frSite.getPage(KindPage, "bundles/b1/index.md")
bundleFr, _ := frSite.getPage(nil, "bundles/b1/index.md")
require.NotNil(t, bundleFr)
require.Equal(t, "/blog/fr/bundles/b1/", bundleFr.RelPermalink())
require.Equal(t, 1, len(bundleFr.Resources))
Expand All @@ -420,7 +420,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Equal(t, "/blog/fr/bundles/b1/logo.png", logoFr.RelPermalink())
b.AssertFileContent("public/fr/bundles/b1/logo.png", "PNG Data")

bundleEn := enSite.getPage(KindPage, "bundles/b1/index.en.md")
bundleEn, _ := enSite.getPage(nil, "bundles/b1/index.en.md")
require.NotNil(t, bundleEn)
require.Equal(t, "/blog/en/bundles/b1/", bundleEn.RelPermalink())
require.Equal(t, 1, len(bundleEn.Resources))
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestMultiSitesRebuild(t *testing.T) {
docFr := readDestination(t, fs, "public/fr/sect/doc1/index.html")
require.True(t, strings.Contains(docFr, "Salut"), "No Salut")

homeEn := enSite.getPage(KindHome)
homeEn, _ := enSite.getPage(nil, "/")
require.NotNil(t, homeEn)
assert.Len(homeEn.Translations(), 3)
require.Equal(t, "fr", homeEn.Translations()[0].Lang())
Expand Down Expand Up @@ -678,7 +678,7 @@ title = "Svenska"
require.True(t, svSite.Language.Lang == "sv", svSite.Language.Lang)
require.True(t, frSite.Language.Lang == "fr", frSite.Language.Lang)

homeEn := enSite.getPage(KindHome)
homeEn, _ := enSite.getPage(nil, "/")
require.NotNil(t, homeEn)
require.Len(t, homeEn.Translations(), 4)
require.Equal(t, "sv", homeEn.Translations()[0].Lang())
Expand Down
10 changes: 5 additions & 5 deletions hugolib/hugo_sites_multihost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ languageName = "Nynorsk"

s1 := b.H.Sites[0]

s1h := s1.getPage(KindHome)
s1h, _ := s1.getPage(nil, "/")
assert.True(s1h.IsTranslated())
assert.Len(s1h.Translations(), 2)
assert.Equal("https://example.com/docs/", s1h.Permalink())
Expand All @@ -66,7 +66,7 @@ languageName = "Nynorsk"
// For multihost, we never want any content in the root.
//
// check url in front matter:
pageWithURLInFrontMatter := s1.getPage(KindPage, "sect/doc3.en.md")
pageWithURLInFrontMatter, _ := s1.getPage(nil, "sect/doc3.en.md")
assert.NotNil(pageWithURLInFrontMatter)
assert.Equal("/superbob", pageWithURLInFrontMatter.URL())
assert.Equal("/docs/superbob/", pageWithURLInFrontMatter.RelPermalink())
Expand All @@ -78,7 +78,7 @@ languageName = "Nynorsk"

s2 := b.H.Sites[1]

s2h := s2.getPage(KindHome)
s2h, _ := s2.getPage(nil, "/")
assert.Equal("https://example.fr/", s2h.Permalink())

b.AssertFileContent("public/fr/index.html", "French Home Page")
Expand All @@ -92,7 +92,7 @@ languageName = "Nynorsk"

// Check bundles

bundleEn := s1.getPage(KindPage, "bundles/b1/index.en.md")
bundleEn, _ := s1.getPage(nil, "bundles/b1/index.en.md")
require.NotNil(t, bundleEn)
require.Equal(t, "/docs/bundles/b1/", bundleEn.RelPermalink())
require.Equal(t, 1, len(bundleEn.Resources))
Expand All @@ -101,7 +101,7 @@ languageName = "Nynorsk"
require.Equal(t, "/docs/bundles/b1/logo.png", logoEn.RelPermalink())
b.AssertFileContent("public/en/bundles/b1/logo.png", "PNG Data")

bundleFr := s2.getPage(KindPage, "bundles/b1/index.md")
bundleFr, _ := s2.getPage(nil, "bundles/b1/index.md")
require.NotNil(t, bundleFr)
require.Equal(t, "/bundles/b1/", bundleFr.RelPermalink())
require.Equal(t, 1, len(bundleFr.Resources))
Expand Down
2 changes: 1 addition & 1 deletion hugolib/language_content_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Content.
b.AssertFileContent("/my/project/public/sv/sect/mybundle/logo.png", "PNG Data")
b.AssertFileContent("/my/project/public/nn/sect/mybundle/logo.png", "PNG Data")

nnSect := nnSite.getPage(KindSection, "sect")
nnSect, _ := nnSite.getPage(nil, "sect")
assert.NotNil(nnSect)
assert.Equal(12, len(nnSect.Pages))
nnHome, _ := nnSite.Info.Home()
Expand Down
44 changes: 37 additions & 7 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,28 @@ func (p *Page) FullFilePath() string {
return filepath.Join(p.Dir(), p.LogicalName())
}

// Returns the canonical, absolute fully-qualifed logical reference used by
// methods such as GetPage and ref/relref shortcodes to unambiguously refer to
// this page. As an absolute path, it is prefixed with a "/".
//
// For pages that have a backing file in the content directory, it is returns
// the path to this file as an absolute path rooted in the content dir. For
// pages or nodes that do not, it returns the virtual path, consistent with
// where you would add a backing content file.
//
// The "/" prefix and support for pages without backing files should be the
// only difference with FullFilePath()
func (p *Page) absoluteSourceRef() string {
sourcePath := p.Source.Path()
if sourcePath != "" {
return "/" + filepath.ToSlash(sourcePath)
} else if len(p.sections) > 0 {
// no backing file, return the virtual source path
return "/" + path.Join(p.sections...)
}
return ""
}

// Pre render prepare steps

func (p *Page) prepareLayouts() error {
Expand Down Expand Up @@ -2007,32 +2029,40 @@ func (p *Page) Hugo() *HugoInfo {
return hugoInfo
}

// GetPage looks up a page for the given ref.
// {{ with .GetPage "blog" }}{{ .Title }}{{ end }}
//
// This will return nil when no page could be found, and will return
// an error if the ref is ambiguous.
func (p *Page) GetPage(ref string) (*Page, error) {
return p.s.getPage(p, ref)
}

func (p *Page) Ref(refs ...string) (string, error) {
if len(refs) == 0 {
return "", nil
}
if len(refs) > 1 {
return p.Site.Ref(refs[0], nil, refs[1])
return p.Site.Ref(refs[0], p, refs[1])
}
return p.Site.Ref(refs[0], nil)
return p.Site.Ref(refs[0], p)
}

func (p *Page) RelRef(refs ...string) (string, error) {
if len(refs) == 0 {
return "", nil
}
if len(refs) > 1 {
return p.Site.RelRef(refs[0], nil, refs[1])
return p.Site.RelRef(refs[0], p, refs[1])
}
return p.Site.RelRef(refs[0], nil)
return p.Site.RelRef(refs[0], p)
}

func (p *Page) String() string {
if p.Path() != "" {
return fmt.Sprintf("Page(%s)", p.Path())
if p.absoluteSourceRef() != "" {
return fmt.Sprintf("Page(%s)", p.absoluteSourceRef())
}
return fmt.Sprintf("Page(%q)", p.title)

}

// Scratch returns the writable context associated with this Page.
Expand Down
Loading