Skip to content

Commit

Permalink
hugolib: Introduce Page.NextPage and Page.PrevPage
Browse files Browse the repository at this point in the history
Introduce new page position variables in order to fix the ordering issue
of `.Next` and `.Prev` while also allowing an upgrade path via
deprecation.

`.NextInSection` becomes `.NextPageInSection`.
`.PrevInSection` becomes `.PrevPageInSection`.

`.Next` becomes a function returning `.PrevPage`.
`.Prev` becomes a function returning `.NextPage`.

Fixes gohugoio#1061
  • Loading branch information
FelicianoTech authored and kaushalmodi committed Sep 25, 2018
1 parent 048a64b commit 935d18f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
28 changes: 20 additions & 8 deletions docs/content/en/variables/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
.LinkTitle
: access when creating links to the content. If set, Hugo will use the `linktitle` from the front matter before `title`.

.Next
: pointer to the following content (based on the `publishdate` field in front matter).
.Next (deprecated)
: In older Hugo versions this pointer went the wrong direction. Please use `.PrevPage` instead.

.NextInSection
: pointer to the following content within the same section (based on `publishdate` field in front matter).
.NextInSection (deprecated)
: Please use `.NextPageInSection`.

.NextPage
: Pointer to the next [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight-date-linktitle-filepath)). Example: `{{if .NextPage}}{{.NextPage.Permalink}}{{end}}`.

.NextPageInSection
: Pointer to the next [regular page](/variables/site/#site-pages) within the same section. Pages are sorted by Hugo's [default sort](/templates/lists#default-weight-date-linktitle-filepath). Example: `{{if .NextPageInSection}}{{.NextPageInSection.Permalink}}{{end}}`.

.OutputFormats
: contains all formats, including the current format, for a given page. Can be combined the with [`.Get` function](/functions/get/) to grab a specific format. (See [Output Formats](/templates/output-formats/).)
Expand All @@ -118,11 +124,17 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
.PlainWords
: the Page content stripped of HTML as a `[]string` using Go's [`strings.Fields`](https://golang.org/pkg/strings/#Fields) to split `.Plain` into a slice.

.Prev
: Pointer to the previous content (based on `publishdate` in front matter).
.Prev (deprecated)
: In older Hugo versions this pointer went the wrong direction. Please use `.NextPage` instead.

.PrevInSection (deprecated)
: Please use `.PrevPageInSection`.

.PrevPage
: Pointer to the previous [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight-date-linktitle-filepath)). Example: `{{if .PrevPage}}{{.PrevPage.Permalink}}{{end}}`.

.PrevInSection
: Pointer to the previous content within the same section (based on `publishdate` in front matter). For example, `{{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}}`.
.PrevPageInSection
: Pointer to the previous [regular page](/variables/site/#site-pages) within the same section. Pages are sorted by Hugo's [default sort](/templates/lists#default-weight-date-linktitle-filepath). Example: `{{if .PrevPageInSection}}{{.PrevPageInSection.Permalink}}{{end}}`.

.PublishDate
: the date on which the content was or will be published; `.Publishdate` pulls from the `publishdate` field in a content's front matter. See also `.ExpiryDate`, `.Date`, and `.Lastmod`.
Expand Down
14 changes: 7 additions & 7 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {

require.Equal(t, "/superbob", doc3.URL(), "invalid url, was specified on doc3")
b.AssertFileContent("public/superbob/index.html", "doc3|Hello|en")
require.Equal(t, doc2.Next, doc3, "doc3 should follow doc2, in .Next")
require.Equal(t, doc2.PrevPage, doc3, "doc3 should follow doc2, in .PrevPage")

doc1fr := doc1en.Translations()[0]
permalink = doc1fr.Permalink()
Expand Down Expand Up @@ -398,16 +398,16 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Equal(t, template.URL(""), enSite.RegularPages[0].RSSLink())

// Issue #3108
next := enSite.RegularPages[0].Next
require.NotNil(t, next)
require.Equal(t, KindPage, next.Kind)
prevPage := enSite.RegularPages[0].PrevPage
require.NotNil(t, prevPage)
require.Equal(t, KindPage, prevPage.Kind)

for {
if next == nil {
if prevPage == nil {
break
}
require.Equal(t, KindPage, next.Kind)
next = next.Next
require.Equal(t, KindPage, prevPage.Kind)
prevPage = prevPage.PrevPage
}

// Check bundles
Expand Down
32 changes: 28 additions & 4 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,10 @@ type PageMeta struct {
}

type Position struct {
Prev *Page
Next *Page
PrevInSection *Page
NextInSection *Page
PrevPage *Page
NextPage *Page
PrevPageInSection *Page
NextPageInSection *Page
}

type Pages []*Page
Expand Down Expand Up @@ -2308,3 +2308,27 @@ func (p *Page) pathOrTitle() string {
}
return p.title
}

func (p *Page) Next() *Page {
// TODO Remove in Hugo 0.52
helpers.Deprecated("Page", ".Next", "Use .PrevPage", false)
return p.PrevPage
}

func (p *Page) Prev() *Page {
// TODO Remove in Hugo 0.52
helpers.Deprecated("Page", ".Prev", "Use .NextPage", false)
return p.NextPage
}

func (p *Page) NextInSection() *Page {
// TODO Remove in Hugo 0.52
helpers.Deprecated("Page", ".NextInSection", "Use .NextPageInSection", false)
return p.NextPageInSection
}

func (p *Page) PrevInSection() *Page {
// TODO Remove in Hugo 0.52
helpers.Deprecated("Page", ".PrevInSection", "Use .PrevPageInSection", false)
return p.PrevPageInSection
}
8 changes: 4 additions & 4 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,12 @@ func (s *Site) setupSitePages() {
var siteLastChange time.Time

for i, page := range s.RegularPages {
if i < len(s.RegularPages)-1 {
page.Next = s.RegularPages[i+1]
if i > 0 {
page.NextPage = s.RegularPages[i-1]
}

if i > 0 {
page.Prev = s.RegularPages[i-1]
if i < len(s.RegularPages)-1 {
page.PrevPage = s.RegularPages[i+1]
}

// Determine Site.Info.LastChange
Expand Down
5 changes: 3 additions & 2 deletions hugolib/site_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,11 @@ func (s *Site) assembleSections() Pages {

for i, p := range sect.Pages {
if i > 0 {
p.NextInSection = sect.Pages[i-1]
p.NextPageInSection = sect.Pages[i-1]
}

if i < len(sect.Pages)-1 {
p.PrevInSection = sect.Pages[i+1]
p.PrevPageInSection = sect.Pages[i+1]
}
}

Expand Down

0 comments on commit 935d18f

Please sign in to comment.