Skip to content

Commit

Permalink
Add a Page interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 2, 2019
1 parent ce8a09a commit b8fb10e
Show file tree
Hide file tree
Showing 49 changed files with 720 additions and 556 deletions.
8 changes: 8 additions & 0 deletions hugolib/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ func (pages Pages) ToResources() resource.Resources {
}
return r
}

func (p Pages) Group(key interface{}, in interface{}) (interface{}, error) {
pages, err := toPages(in)
if err != nil {
return nil, err
}
return PageGroup{Key: key, Pages: pages}, nil
}
6 changes: 5 additions & 1 deletion hugolib/embedded_shortcodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"testing"

"github.com/spf13/cast"

"path/filepath"

"github.com/gohugoio/hugo/deps"
Expand Down Expand Up @@ -69,7 +71,9 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) {

require.Len(t, s.RegularPages, 1)

output := string(s.RegularPages[0].content())
content, err := s.RegularPages[0].(*Page).Content()
require.NoError(t, err)
output := cast.ToString(content)

if !strings.Contains(output, expected) {
t.Errorf("Got\n%q\nExpected\n%q", output, expected)
Expand Down
38 changes: 23 additions & 15 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/gohugoio/hugo/langs"

"github.com/gohugoio/hugo/i18n"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/tpl"
"github.com/gohugoio/hugo/tpl/tplimpl"
)
Expand Down Expand Up @@ -136,7 +137,7 @@ func (h *HugoSites) langSite() map[string]*Site {

// GetContentPage finds a Page with content given the absolute filename.
// Returns nil if none found.
func (h *HugoSites) GetContentPage(filename string) *Page {
func (h *HugoSites) GetContentPage(filename string) resource.Page {
for _, s := range h.Sites {
pos := s.rawAllPages.findPagePosByFilename(filename)
if pos == -1 {
Expand Down Expand Up @@ -495,20 +496,25 @@ func (h *HugoSites) assignMissingTranslations() error {
for _, nodeType := range []string{KindHome, KindSection, KindTaxonomy, KindTaxonomyTerm} {
nodes := h.findPagesByKindIn(nodeType, allPages)

// TODO(bep) page
// Assign translations
for _, t1 := range nodes {
t1p := t1.(*Page)
for _, t2 := range nodes {
if t1.isNewTranslation(t2) {
t1.translations = append(t1.translations, t2)
t2p := t2.(*Page)
if t1p.isNewTranslation(t2p) {
t1p.translations = append(t1p.translations, t2p)
}
}
}
}

// Now we can sort the translations.
for _, p := range allPages {
if len(p.translations) > 0 {
pageBy(languagePageSort).Sort(p.translations)
// TODO(bep) page
pp := p.(*Page)
if len(pp.translations) > 0 {
pageBy(languagePageSort).Sort(pp.translations)
}
}
return nil
Expand Down Expand Up @@ -548,7 +554,7 @@ func (h *HugoSites) createMissingPages() error {
if s.isEnabled(KindTaxonomyTerm) {
foundTaxonomyTermsPage := false
for _, p := range taxonomyTermsPages {
if p.sectionsPath() == plural {
if p.(*Page).sectionsPath() == plural {
foundTaxonomyTermsPage = true
break
}
Expand All @@ -570,7 +576,7 @@ func (h *HugoSites) createMissingPages() error {
key = s.PathSpec.MakePathSanitized(key)
}
for _, p := range taxonomyPages {
sectionsPath := p.sectionsPath()
sectionsPath := p.(*Page).sectionsPath()

if !strings.HasPrefix(sectionsPath, plural) {
continue
Expand Down Expand Up @@ -631,18 +637,20 @@ func (h *HugoSites) removePageByFilename(filename string) {
func (h *HugoSites) setupTranslations() {
for _, s := range h.Sites {
for _, p := range s.rawAllPages {
if p.Kind == kindUnknown {
p.Kind = p.kindFromSections()
// TODO(bep) page .(*Page) and all others
pp := p.(*Page)
if p.Kind() == kindUnknown {
pp.kind = pp.kindFromSections()
}

if !p.s.isEnabled(p.Kind) {
if !pp.s.isEnabled(p.Kind()) {
continue
}

shouldBuild := p.shouldBuild()
s.updateBuildStats(p)
shouldBuild := pp.shouldBuild()
s.updateBuildStats(pp)
if shouldBuild {
if p.headless {
if pp.headless {
s.headlessPages = append(s.headlessPages, p)
} else {
s.Pages = append(s.Pages, p)
Expand Down Expand Up @@ -676,13 +684,13 @@ func (h *HugoSites) setupTranslations() {

func (s *Site) preparePagesForRender(start bool) error {
for _, p := range s.Pages {
if err := p.prepareForRender(start); err != nil {
if err := p.(*Page).prepareForRender(start); err != nil {
return err
}
}

for _, p := range s.headlessPages {
if err := p.prepareForRender(start); err != nil {
if err := p.(*Page).prepareForRender(start); err != nil {
return err
}
}
Expand Down
15 changes: 8 additions & 7 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,20 @@ func (h *HugoSites) assemble(config *BuildCfg) error {
for _, pages := range []Pages{s.Pages, s.headlessPages} {
for _, p := range pages {
// May have been set in front matter
if len(p.outputFormats) == 0 {
p.outputFormats = s.outputFormats[p.Kind]
pp := p.(*Page)
if len(pp.outputFormats) == 0 {
pp.outputFormats = s.outputFormats[p.Kind()]
}

if p.headless {
if pp.headless {
// headless = 1 output format only
p.outputFormats = p.outputFormats[:1]
pp.outputFormats = pp.outputFormats[:1]
}
for _, r := range p.Resources.ByType(pageResourceType) {
r.(*Page).outputFormats = p.outputFormats
for _, r := range p.Resources().ByType(pageResourceType) {
r.(*Page).outputFormats = pp.outputFormats
}

if err := p.initPaths(); err != nil {
if err := p.(*Page).initPaths(); err != nil {
return err
}

Expand Down
78 changes: 39 additions & 39 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {

gp1 := b.H.GetContentPage(filepath.FromSlash("content/sect/doc1.en.md"))
require.NotNil(t, gp1)
require.Equal(t, "doc1", gp1.title)
require.Equal(t, "doc1", gp1.Title())
gp2 := b.H.GetContentPage(filepath.FromSlash("content/dummysect/notfound.md"))
require.Nil(t, gp2)

Expand All @@ -247,12 +247,12 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
assert.Equal(5, len(enSite.RegularPages))
assert.Equal(32, len(enSite.AllPages))

doc1en := enSite.RegularPages[0]
doc1en := enSite.RegularPages[0].(*Page)
permalink := doc1en.Permalink()
require.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", permalink, "invalid doc1.en permalink")
require.Len(t, doc1en.Translations(), 1, "doc1-en should have one translation, excluding itself")

doc2 := enSite.RegularPages[1]
doc2 := enSite.RegularPages[1].(*Page)
permalink = doc2.Permalink()
require.Equal(t, "http://example.com/blog/en/sect/doc2/", permalink, "invalid doc2 permalink")

Expand All @@ -263,22 +263,20 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
// and do no not do any language code prefixing.
require.Equal(t, "http://example.com/blog/superbob/", permalink, "invalid doc3 permalink")

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.PrevPage, doc3, "doc3 should follow doc2, in .PrevPage")

doc1fr := doc1en.Translations()[0]
doc1fr := doc1en.Translations()[0].(*Page)
permalink = doc1fr.Permalink()
require.Equal(t, "http://example.com/blog/fr/sect/doc1/", permalink, "invalid doc1fr permalink")

require.Equal(t, doc1en.Translations()[0], doc1fr, "doc1-en should have doc1-fr as translation")
require.Equal(t, doc1fr.Translations()[0], doc1en, "doc1-fr should have doc1-en as translation")
require.Equal(t, "fr", doc1fr.Language().Lang)

doc4 := enSite.AllPages[4]
doc4 := enSite.AllPages[4].(*Page)
permalink = doc4.Permalink()
require.Equal(t, "http://example.com/blog/fr/sect/doc4/", permalink, "invalid doc4 permalink")
require.Equal(t, "/blog/fr/sect/doc4/", doc4.URL())

require.Len(t, doc4.Translations(), 0, "found translations for doc4")

Expand All @@ -299,7 +297,8 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Len(t, frSite.AllPages, 32, "should have 32 total pages (including translations and nodes)")

for _, frenchPage := range frSite.RegularPages {
require.Equal(t, "fr", frenchPage.Lang())
p := frenchPage.(*Page)
require.Equal(t, "fr", p.Lang())
}

// See https://github.com/gohugoio/hugo/issues/4285
Expand Down Expand Up @@ -331,32 +330,32 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
homeEn := enSite.getPage(KindHome)
require.NotNil(t, homeEn)
require.Len(t, homeEn.Translations(), 3)
require.Equal(t, "fr", homeEn.Translations()[0].Lang())
require.Equal(t, "nn", homeEn.Translations()[1].Lang())
require.Equal(t, "På nynorsk", homeEn.Translations()[1].title)
require.Equal(t, "nb", homeEn.Translations()[2].Lang())
require.Equal(t, "På bokmål", homeEn.Translations()[2].title, configSuffix)
require.Equal(t, "Bokmål", homeEn.Translations()[2].Language().LanguageName, configSuffix)
require.Equal(t, "fr", homeEn.Translations()[0].(*Page).Lang())
require.Equal(t, "nn", homeEn.Translations()[1].(*Page).Lang())
require.Equal(t, "På nynorsk", homeEn.Translations()[1].Title())
require.Equal(t, "nb", homeEn.Translations()[2].(*Page).Lang())
require.Equal(t, "På bokmål", homeEn.Translations()[2].Title(), configSuffix)
require.Equal(t, "Bokmål", homeEn.Translations()[2].(*Page).Language().LanguageName, configSuffix)

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

require.Equal(t, "fr", sectFr.Lang())
require.Len(t, sectFr.Translations(), 1)
require.Equal(t, "en", sectFr.Translations()[0].Lang())
require.Equal(t, "Sects", sectFr.Translations()[0].title)
require.Equal(t, "en", sectFr.Translations()[0].(*Page).Lang())
require.Equal(t, "Sects", sectFr.Translations()[0].Title())

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

taxTermNn := nnSite.getPage(KindTaxonomy, "lag", "sogndal")
require.NotNil(t, taxTermNn)
require.Len(t, taxTermNn.Translations(), 1)
require.Equal(t, "nb", taxTermNn.Translations()[0].Lang())
require.Equal(t, "nb", taxTermNn.Translations()[0].(*Page).Lang())

// Check sitemap(s)
b.AssertFileContent("public/sitemap.xml",
Expand Down Expand Up @@ -392,36 +391,36 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
require.Equal(t, "Heim", nnSite.Menus["main"].ByName()[0].Name)

// Issue #1302
require.Equal(t, template.URL(""), enSite.RegularPages[0].RSSLink())
require.Equal(t, template.URL(""), enSite.RegularPages[0].(*Page).RSSLink())

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

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

// Check bundles
bundleFr := frSite.getPage(KindPage, "bundles/b1/index.md")
require.NotNil(t, bundleFr)
require.Equal(t, "/blog/fr/bundles/b1/", bundleFr.RelPermalink())
require.Equal(t, 1, len(bundleFr.Resources))
logoFr := bundleFr.Resources.GetMatch("logo*")
require.Equal(t, 1, len(bundleFr.Resources()))
logoFr := bundleFr.Resources().GetMatch("logo*")
require.NotNil(t, logoFr)
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")
require.NotNil(t, bundleEn)
require.Equal(t, "/blog/en/bundles/b1/", bundleEn.RelPermalink())
require.Equal(t, 1, len(bundleEn.Resources))
logoEn := bundleEn.Resources.GetMatch("logo*")
require.Equal(t, 1, len(bundleEn.Resources()))
logoEn := bundleEn.Resources().GetMatch("logo*")
require.NotNil(t, logoEn)
require.Equal(t, "/blog/en/bundles/b1/logo.png", logoEn.RelPermalink())
b.AssertFileContent("public/en/bundles/b1/logo.png", "PNG Data")
Expand Down Expand Up @@ -504,9 +503,9 @@ func TestMultiSitesRebuild(t *testing.T) {
assert.Len(enSite.RegularPages, 6)
assert.Len(enSite.AllPages, 34)
assert.Len(frSite.RegularPages, 5)
require.Equal(t, "new_fr_1", frSite.RegularPages[3].title)
require.Equal(t, "new_en_2", enSite.RegularPages[0].title)
require.Equal(t, "new_en_1", enSite.RegularPages[1].title)
require.Equal(t, "new_fr_1", frSite.RegularPages[3].Title())
require.Equal(t, "new_en_2", enSite.RegularPages[0].Title())
require.Equal(t, "new_en_1", enSite.RegularPages[1].Title())

rendered := readDestination(t, fs, "public/en/new1/index.html")
require.True(t, strings.Contains(rendered, "new_en_1"), rendered)
Expand Down Expand Up @@ -540,7 +539,7 @@ func TestMultiSitesRebuild(t *testing.T) {
},
func(t *testing.T) {
assert.Len(enSite.RegularPages, 6, "Rename")
require.Equal(t, "new_en_1", enSite.RegularPages[1].title)
require.Equal(t, "new_en_1", enSite.RegularPages[1].Title())
rendered := readDestination(t, fs, "public/en/new1renamed/index.html")
require.True(t, strings.Contains(rendered, "new_en_1"), rendered)
}},
Expand Down Expand Up @@ -582,7 +581,7 @@ func TestMultiSitesRebuild(t *testing.T) {
homeEn := enSite.getPage(KindHome)
require.NotNil(t, homeEn)
assert.Len(homeEn.Translations(), 3)
require.Equal(t, "fr", homeEn.Translations()[0].Lang())
require.Equal(t, "fr", homeEn.Translations()[0].(*Page).Lang())

},
},
Expand Down Expand Up @@ -626,12 +625,13 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) {
s := sites.Sites[0]

for _, p := range s.rawAllPages {
pp := p.(*Page)
// No HTML when not processed
require.Equal(t, p.shouldBuild(), bytes.Contains(p.workContent, []byte("</")), p.BaseFileName()+": "+string(p.workContent))
require.Equal(t, pp.shouldBuild(), bytes.Contains(pp.workContent, []byte("</")), pp.BaseFileName()+": "+string(pp.workContent))

require.Equal(t, p.shouldBuild(), p.content() != "", fmt.Sprintf("%v:%v", p.content(), p.shouldBuild()))
require.Equal(t, pp.shouldBuild(), pp.content() != "", fmt.Sprintf("%v:%v", pp.content(), pp.shouldBuild()))

require.Equal(t, p.shouldBuild(), p.content() != "", p.BaseFileName())
require.Equal(t, pp.shouldBuild(), pp.content() != "", pp.BaseFileName())

}
}
Expand Down Expand Up @@ -678,20 +678,20 @@ title = "Svenska"
homeEn := enSite.getPage(KindHome)
require.NotNil(t, homeEn)
require.Len(t, homeEn.Translations(), 4)
require.Equal(t, "sv", homeEn.Translations()[0].Lang())
require.Equal(t, "sv", homeEn.Translations()[0].(*Page).Lang())

require.Len(t, enSite.RegularPages, 5)
require.Len(t, frSite.RegularPages, 4)

// Veriy Swedish site
require.Len(t, svSite.RegularPages, 1)
svPage := svSite.RegularPages[0]
svPage := svSite.RegularPages[0].(*Page)

require.Equal(t, "Swedish Contentfile", svPage.title)
require.Equal(t, "Swedish Contentfile", svPage.Title())
require.Equal(t, "sv", svPage.Lang())
require.Len(t, svPage.Translations(), 2)
require.Len(t, svPage.AllTranslations(), 3)
require.Equal(t, "en", svPage.Translations()[0].Lang())
require.Equal(t, "en", svPage.Translations()[0].(*Page).Lang())

// Regular pages have no children
require.Len(t, svPage.Pages, 0)
Expand Down
Loading

0 comments on commit b8fb10e

Please sign in to comment.