Skip to content

Commit

Permalink
Move Pages to resources/page
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 26, 2019
1 parent ec4ecd2 commit 09762ae
Show file tree
Hide file tree
Showing 73 changed files with 2,220 additions and 1,387 deletions.
8 changes: 4 additions & 4 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, ta
}
}

if p.Filename() == "" {
if p.File().Filename() == "" {
// No content file.
return nil
}

errMsg := fmt.Errorf("Error processing file %q", p.Path())

site.Log.INFO.Println("Attempting to convert", p.LogicalName())
site.Log.INFO.Println("Attempting to convert", p.File().Filename())

f, _ := p.File.(src.ReadableFile)
f, _ := p.File().(src.ReadableFile)
file, err := f.Open()
if err != nil {
site.Log.ERROR.Println(errMsg)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, ta

newContent.Write(pf.content)

newFilename := p.Filename()
newFilename := p.File().Filename()

if cc.outputDir != "" {
contentDir := strings.TrimSuffix(newFilename, p.Path())
Expand Down
6 changes: 3 additions & 3 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.",
for _, p := range sites.Pages() {
pp := p.(*hugolib.Page)
if pp.IsDraft() {
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down Expand Up @@ -106,7 +106,7 @@ posted in the future.`,
for _, p := range sites.Pages() {
if resource.IsFuture(p) {
pp := p.(*hugolib.Page)
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down Expand Up @@ -143,7 +143,7 @@ expired.`,
for _, p := range sites.Pages() {
if resource.IsExpired(p) {
pp := p.(*hugolib.Page)
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down
18 changes: 4 additions & 14 deletions docs/content/en/variables/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ See [`.Scratch`](/functions/scratch/) for page-scoped, writable variables.
.Kind
: the page's *kind*. Possible return values are `page`, `home`, `section`, `taxonomy`, or `taxonomyTerm`. Note that there are also `RSS`, `sitemap`, `robotsTXT`, and `404` kinds, but these are only available during the rendering of each of these respective page's kind and therefore *not* available in any of the `Pages` collections.

.Lang
: language taken from the language extension notation.

.Language
: a language object that points to the language's definition in the site
`config`.
: a language object that points to the language's definition in the site `config`. `.Language.Lang` gives you the language code.

.Lastmod
: the date the content was last modified. `.Lastmod` pulls from the `lastmod` field in a content's front matter.
Expand All @@ -96,10 +92,7 @@ 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 (deprecated)
: In older Hugo versions this pointer went the wrong direction. Please use `.PrevPage` instead.

.NextPage
.Next
: 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}}`.

.NextInSection
Expand All @@ -122,9 +115,6 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
: 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 (deprecated)
: In older Hugo versions this pointer went the wrong direction. Please use `.NextPage` instead.

.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
Expand All @@ -133,8 +123,8 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
.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`.

.RSSLink
: link to the taxonomies' RSS link.
.RSSLink (deprecated)
: link to the page's RSS feed. This is deprecated. You should instead do something like this: `{{ with .OutputFormats.Get "RSS" }}{{ . RelPermalink }}{{ end }}`.

.RawContent
: raw markdown content without the front matter. Useful with [remarkjs.com](
Expand Down
78 changes: 9 additions & 69 deletions hugolib/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
package hugolib

import (
"fmt"

"github.com/gohugoio/hugo/resources/resource"

"github.com/gohugoio/hugo/common/collections"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
)

var (
// TODO(bep) page move
_ collections.Grouper = (*Page)(nil)
_ collections.Slicer = (*Page)(nil)
_ collections.Slicer = PageGroup{}
_ collections.Slicer = WeightedPage{}
_ resource.ResourcesConverter = Pages{}
_ collections.Slicer = page.PageGroup{}
_ collections.Slicer = page.WeightedPage{}
_ resource.ResourcesConverter = page.Pages{}
)

// collections.Slicer implementations below. We keep these bridge implementations
Expand All @@ -36,49 +35,7 @@ var (
// Slice is not meant to be used externally. It's a bridge function
// for the template functions. See collections.Slice.
func (p *Page) Slice(items interface{}) (interface{}, error) {
return toPages(items)
}

// Slice is not meant to be used externally. It's a bridge function
// for the template functions. See collections.Slice.
func (p PageGroup) Slice(in interface{}) (interface{}, error) {
switch items := in.(type) {
case PageGroup:
return items, nil
case []interface{}:
groups := make(PagesGroup, len(items))
for i, v := range items {
g, ok := v.(PageGroup)
if !ok {
return nil, fmt.Errorf("type %T is not a PageGroup", v)
}
groups[i] = g
}
return groups, nil
default:
return nil, fmt.Errorf("invalid slice type %T", items)
}
}

// Slice is not meant to be used externally. It's a bridge function
// for the template functions. See collections.Slice.
func (p WeightedPage) Slice(in interface{}) (interface{}, error) {
switch items := in.(type) {
case WeightedPages:
return items, nil
case []interface{}:
weighted := make(WeightedPages, len(items))
for i, v := range items {
g, ok := v.(WeightedPage)
if !ok {
return nil, fmt.Errorf("type %T is not a WeightedPage", v)
}
weighted[i] = g
}
return weighted, nil
default:
return nil, fmt.Errorf("invalid slice type %T", items)
}
return page.ToPages(items)
}

// collections.Grouper implementations below
Expand All @@ -87,26 +44,9 @@ func (p WeightedPage) Slice(in interface{}) (interface{}, error) {
// This method is not meant for external use. It got its non-typed arguments to satisfy
// a very generic interface in the tpl package.
func (p *Page) Group(key interface{}, in interface{}) (interface{}, error) {
pages, err := toPages(in)
if err != nil {
return nil, err
}
return PageGroup{Key: key, Pages: pages}, nil
}

// ToResources wraps resource.ResourcesConverter
func (pages Pages) ToResources() resource.Resources {
r := make(resource.Resources, len(pages))
for i, p := range pages {
r[i] = p
}
return r
}

func (p Pages) Group(key interface{}, in interface{}) (interface{}, error) {
pages, err := toPages(in)
pages, err := page.ToPages(in)
if err != nil {
return nil, err
}
return PageGroup{Key: key, Pages: pages}, nil
return page.PageGroup{Key: key, Pages: pages}, nil
}
10 changes: 5 additions & 5 deletions hugolib/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ tags_weight: %d
require.Len(t, b.H.Sites[0].RegularPages, 2)

b.AssertFileContent("public/index.html",
"pages:2:hugolib.Pages:Page(/page1.md)/Page(/page2.md)",
"pageGroups:2:hugolib.PagesGroup:Page(/page1.md)/Page(/page2.md)",
`weightedPages:2::hugolib.WeightedPages:[WeightedPage(10,"Page") WeightedPage(20,"Page")]`)
"pages:2:page.Pages:Page(/page1.md)/Page(/page2.md)",
"pageGroups:2:page.PagesGroup:Page(/page1.md)/Page(/page2.md)",
`weightedPages:2::page.WeightedPages:[WeightedPage(10,"Page") WeightedPage(20,"Page")]`)
}

func TestAppendFunc(t *testing.T) {
Expand Down Expand Up @@ -132,8 +132,8 @@ tags_weight: %d
require.Len(t, b.H.Sites[0].RegularPages, 2)

b.AssertFileContent("public/index.html",
"pages:2:hugolib.Pages:Page(/page2.md)/Page(/page1.md)",
"appendPages:9:hugolib.Pages:home/page",
"pages:2:page.Pages:Page(/page2.md)/Page(/page1.md)",
"appendPages:9:page.Pages:home/page",
"appendStrings:[]string:[a b c d e]",
"appendStringsSlice:[]string:[a b c c d]",
"union:[]string:[a b c d e]",
Expand Down
2 changes: 1 addition & 1 deletion hugolib/gitinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (g *gitInfo) forPage(p *Page) (*gitmap.GitInfo, bool) {
return nil, false
}

name := strings.TrimPrefix(filepath.ToSlash(p.Filename()), g.contentDir)
name := strings.TrimPrefix(filepath.ToSlash(p.File().Filename()), g.contentDir)
name = strings.TrimPrefix(name, "/")

return g.repo.Files[name], true
Expand Down
Loading

0 comments on commit 09762ae

Please sign in to comment.