Make Page an interface

The main motivation of this commit is to add a `page.Page` interface to replace the very file-oriented `hugolib.Page` struct.
This is all a preparation step for issue  #5074, "pages from other data sources".

But this also fixes a set of annoying limitations, especially related to custom output formats, and shortcodes.

Most notable changes:

* The inner content of shortcodes using the `{{%` as the outer-most delimiter will now be sent to the content renderer, e.g. Blackfriday.
  This means that any markdown will partake in the global ToC and footnote context etc.
* The Custom Output formats are now "fully virtualized". This removes many of the current limitations.
* The taxonomy list type now has a reference to the `Page` object.
  This improves the taxonomy template `.Title` situation and make common template constructs much simpler.

See #5074
Fixes #5763
Fixes #5758
Fixes #5090
Fixes #5204
Fixes #4695
Fixes #5607
Fixes #5707
Fixes #5719
Fixes #3113
Fixes #5706
Fixes #5767
Fixes #5723
Fixes #5769
Fixes #5770
Fixes #5771
Fixes #5759
Fixes #5776
Fixes #5777
Fixes #5778
This commit is contained in:
Bjørn Erik Pedersen
2019-01-02 12:33:26 +01:00
parent 44f5c1c14c
commit 597e418cb0
206 changed files with 14442 additions and 9679 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2017-present The Hugo Authors. All rights reserved.
// Copyright 2019 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.
@@ -20,6 +20,7 @@ import (
"testing"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/resources/page"
"github.com/stretchr/testify/require"
)
@@ -117,65 +118,66 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
require.Len(t, s.RegularPages, 21)
require.Len(t, s.RegularPages(), 21)
tests := []struct {
sections string
verify func(p *Page)
verify func(assert *require.Assertions, p page.Page)
}{
{"elsewhere", func(p *Page) {
assert.Len(p.Pages, 1)
for _, p := range p.Pages {
assert.Equal([]string{"elsewhere"}, p.sections)
{"elsewhere", func(assert *require.Assertions, p page.Page) {
assert.Len(p.Pages(), 1)
for _, p := range p.Pages() {
assert.Equal("elsewhere", p.SectionsPath())
}
}},
{"post", func(p *Page) {
assert.Len(p.Pages, 2)
for _, p := range p.Pages {
{"post", func(assert *require.Assertions, p page.Page) {
assert.Len(p.Pages(), 2)
for _, p := range p.Pages() {
assert.Equal("post", p.Section())
}
}},
{"empty1", func(p *Page) {
{"empty1", func(assert *require.Assertions, p page.Page) {
// > b,c
assert.NotNil(p.s.getPage(KindSection, "empty1", "b"))
assert.NotNil(p.s.getPage(KindSection, "empty1", "b", "c"))
assert.NotNil(getPage(p, "/empty1/b"))
assert.NotNil(getPage(p, "/empty1/b/c"))
}},
{"empty2", func(p *Page) {
{"empty2", func(assert *require.Assertions, p page.Page) {
// > b,c,d where b and d have content files.
b := p.s.getPage(KindSection, "empty2", "b")
b := getPage(p, "/empty2/b")
assert.NotNil(b)
assert.Equal("T40_-1", b.title)
c := p.s.getPage(KindSection, "empty2", "b", "c")
assert.Equal("T40_-1", b.Title())
c := getPage(p, "/empty2/b/c")
assert.NotNil(c)
assert.Equal("Cs", c.title)
d := p.s.getPage(KindSection, "empty2", "b", "c", "d")
assert.Equal("Cs", c.Title())
d := getPage(p, "/empty2/b/c/d")
assert.NotNil(d)
assert.Equal("T41_-1", d.title)
assert.Equal("T41_-1", d.Title())
assert.False(c.Eq(d))
assert.True(c.Eq(c))
assert.False(c.Eq("asdf"))
}},
{"empty3", func(p *Page) {
{"empty3", func(assert *require.Assertions, p page.Page) {
// b,c,d with regular page in b
b := p.s.getPage(KindSection, "empty3", "b")
b := getPage(p, "/empty3/b")
assert.NotNil(b)
assert.Len(b.Pages, 1)
assert.Equal("empty3.md", b.Pages[0].File.LogicalName())
assert.Len(b.Pages(), 1)
assert.Equal("empty3.md", b.Pages()[0].File().LogicalName())
}},
{"empty3", func(p *Page) {
xxx := p.s.getPage(KindPage, "empty3", "nil")
{"empty3", func(assert *require.Assertions, p page.Page) {
xxx := getPage(p, "/empty3/nil")
assert.Nil(xxx)
assert.Equal(xxx.Eq(nil), true)
}},
{"top", func(p *Page) {
assert.Equal("Tops", p.title)
assert.Len(p.Pages, 2)
assert.Equal("mypage2.md", p.Pages[0].LogicalName())
assert.Equal("mypage3.md", p.Pages[1].LogicalName())
{"top", func(assert *require.Assertions, p page.Page) {
assert.Equal("Tops", p.Title())
assert.Len(p.Pages(), 2)
assert.Equal("mypage2.md", p.Pages()[0].File().LogicalName())
assert.Equal("mypage3.md", p.Pages()[1].File().LogicalName())
home := p.Parent()
assert.True(home.IsHome())
assert.Len(p.Sections(), 0)
@@ -185,30 +187,31 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
assert.True(active)
assert.Equal(p, p.FirstSection())
}},
{"l1", func(p *Page) {
assert.Equal("L1s", p.title)
assert.Len(p.Pages, 2)
{"l1", func(assert *require.Assertions, p page.Page) {
assert.Equal("L1s", p.Title())
assert.Len(p.Pages(), 2)
assert.True(p.Parent().IsHome())
assert.Len(p.Sections(), 2)
}},
{"l1,l2", func(p *Page) {
assert.Equal("T2_-1", p.title)
assert.Len(p.Pages, 3)
assert.Equal(p, p.Pages[0].Parent())
assert.Equal("L1s", p.Parent().title)
assert.Equal("/l1/l2/", p.URLPath.URL)
{"l1,l2", func(assert *require.Assertions, p page.Page) {
assert.Equal("T2_-1", p.Title())
assert.Len(p.Pages(), 3)
assert.Equal(p, p.Pages()[0].Parent())
assert.Equal("L1s", p.Parent().Title())
assert.Equal("/l1/l2/", p.RelPermalink())
assert.Len(p.Sections(), 1)
for _, child := range p.Pages {
for _, child := range p.Pages() {
assert.Equal(p, child.CurrentSection())
active, err := child.InSection(p)
assert.NoError(err)
assert.True(active)
active, err = p.InSection(child)
assert.NoError(err)
assert.True(active)
active, err = p.InSection(p.s.getPage(KindHome))
active, err = p.InSection(getPage(p, "/"))
assert.NoError(err)
assert.False(active)
@@ -227,25 +230,25 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
assert.True(isDescendant)
}
assert.Equal(p, p.CurrentSection())
assert.True(p.Eq(p.CurrentSection()))
}},
{"l1,l2_2", func(p *Page) {
assert.Equal("T22_-1", p.title)
assert.Len(p.Pages, 2)
assert.Equal(filepath.FromSlash("l1/l2_2/page_2_2_1.md"), p.Pages[0].Path())
assert.Equal("L1s", p.Parent().title)
{"l1,l2_2", func(assert *require.Assertions, p page.Page) {
assert.Equal("T22_-1", p.Title())
assert.Len(p.Pages(), 2)
assert.Equal(filepath.FromSlash("l1/l2_2/page_2_2_1.md"), p.Pages()[0].File().Path())
assert.Equal("L1s", p.Parent().Title())
assert.Len(p.Sections(), 0)
}},
{"l1,l2,l3", func(p *Page) {
var nilp *Page
{"l1,l2,l3", func(assert *require.Assertions, p page.Page) {
nilp, _ := p.GetPage("this/does/not/exist")
assert.Equal("T3_-1", p.title)
assert.Len(p.Pages, 2)
assert.Equal("T2_-1", p.Parent().title)
assert.Equal("T3_-1", p.Title())
assert.Len(p.Pages(), 2)
assert.Equal("T2_-1", p.Parent().Title())
assert.Len(p.Sections(), 0)
l1 := p.s.getPage(KindSection, "l1")
l1 := getPage(p, "/l1")
isDescendant, err := l1.IsDescendant(p)
assert.NoError(err)
assert.False(isDescendant)
@@ -274,32 +277,35 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
assert.False(isAncestor)
}},
{"perm a,link", func(p *Page) {
assert.Equal("T9_-1", p.title)
{"perm a,link", func(assert *require.Assertions, p page.Page) {
assert.Equal("T9_-1", p.Title())
assert.Equal("/perm-a/link/", p.RelPermalink())
assert.Len(p.Pages, 4)
first := p.Pages[0]
assert.Len(p.Pages(), 4)
first := p.Pages()[0]
assert.Equal("/perm-a/link/t1_1/", first.RelPermalink())
th.assertFileContent("public/perm-a/link/t1_1/index.html", "Single|T1_1")
last := p.Pages[3]
last := p.Pages()[3]
assert.Equal("/perm-a/link/t1_5/", last.RelPermalink())
}},
}
home := s.getPage(KindHome)
home := s.getPage(page.KindHome)
for _, test := range tests {
sections := strings.Split(test.sections, ",")
p := s.getPage(KindSection, sections...)
assert.NotNil(p, fmt.Sprint(sections))
t.Run(fmt.Sprintf("sections %s", test.sections), func(t *testing.T) {
assert := require.New(t)
sections := strings.Split(test.sections, ",")
p := s.getPage(page.KindSection, sections...)
assert.NotNil(p, fmt.Sprint(sections))
if p.Pages != nil {
assert.Equal(p.Pages, p.data["Pages"])
}
assert.NotNil(p.Parent(), fmt.Sprintf("Parent nil: %q", test.sections))
test.verify(p)
if p.Pages() != nil {
assert.Equal(p.Pages(), p.Data().(page.Data).Pages())
}
assert.NotNil(p.Parent(), fmt.Sprintf("Parent nil: %q", test.sections))
test.verify(assert, p)
})
}
assert.NotNil(home)
@@ -307,7 +313,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
assert.Len(home.Sections(), 9)
assert.Equal(home.Sections(), s.Info.Sections())
rootPage := s.getPage(KindPage, "mypage.md")
rootPage := s.getPage(page.KindPage, "mypage.md")
assert.NotNil(rootPage)
assert.True(rootPage.Parent().IsHome())
@@ -317,7 +323,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
// If we later decide to do something about this, we will have to do some normalization in
// getPage.
// TODO(bep)
sectionWithSpace := s.getPage(KindSection, "Spaces in Section")
sectionWithSpace := s.getPage(page.KindSection, "Spaces in Section")
require.NotNil(t, sectionWithSpace)
require.Equal(t, "/spaces-in-section/", sectionWithSpace.RelPermalink())