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 0f3c4c7
Show file tree
Hide file tree
Showing 52 changed files with 734 additions and 566 deletions.
4 changes: 2 additions & 2 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (cc *convertCmd) convertContents(format metadecoders.Format) error {

site.Log.FEEDBACK.Println("processing", len(site.AllPages), "content files")
for _, p := range site.AllPages {
if err := cc.convertAndSavePage(p, site, format); err != nil {
if err := cc.convertAndSavePage(p.(*hugolib.Page), site, format); err != nil {
return err
}
}
Expand All @@ -135,7 +135,7 @@ func (cc *convertCmd) convertContents(format metadecoders.Format) error {

func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, targetFormat metadecoders.Format) error {
// The resources are not in .Site.AllPages.
for _, r := range p.Resources.ByType("page") {
for _, r := range p.Resources().ByType("page") {
if err := cc.convertAndSavePage(r.(*hugolib.Page), site, targetFormat); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,

if navigate {
if onePageName != "" {
p = c.hugo.GetContentPage(onePageName)
p = c.hugo.GetContentPage(onePageName).(*hugolib.Page)
}
}

Expand Down
15 changes: 9 additions & 6 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.",
}

for _, p := range sites.Pages() {
if p.IsDraft() {
jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
pp := p.(*hugolib.Page)
if pp.IsDraft() {
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
}

}
Expand Down Expand Up @@ -102,8 +103,9 @@ posted in the future.`,
}

for _, p := range sites.Pages() {
if p.IsFuture() {
jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
pp := p.(*hugolib.Page)
if pp.IsFuture() {
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
}

}
Expand Down Expand Up @@ -138,8 +140,9 @@ expired.`,
}

for _, p := range sites.Pages() {
if p.IsExpired() {
jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
pp := p.(*hugolib.Page)
if pp.IsExpired() {
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
}

}
Expand Down
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
Loading

0 comments on commit 0f3c4c7

Please sign in to comment.