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 3, 2019
1 parent 1534f56 commit ea23fa7
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 89 deletions.
17 changes: 0 additions & 17 deletions hugolib/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,3 @@ func (p *Page) Group(key interface{}, in interface{}) (interface{}, error) {
}
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)
if err != nil {
return nil, err
}
return PageGroup{Key: key, Pages: pages}, nil
}
22 changes: 3 additions & 19 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"reflect"

"github.com/gohugoio/hugo/common/hugo"
Expand Down Expand Up @@ -122,7 +121,7 @@ type Page struct {
// Sections etc. will have child pages. These were earlier placed in .Data.Pages,
// but can now be more intuitively also be fetched directly from .Pages.
// This collection will be nil for regular pages.
Pages Pages
Pages page.Pages

// Since Hugo 0.32, a Page can have resources such as images and CSS associated
// with itself. The resource will typically be placed relative to the Page,
Expand All @@ -136,7 +135,7 @@ type Page struct {

// translations will contain references to this page in other language
// if available.
translations Pages
translations page.Pages

// A key that maps to translation(s) of this page. This value is fetched
// from the page front matter.
Expand Down Expand Up @@ -221,7 +220,7 @@ type Page struct {
origOnCopy *Page

// Will only be set for section pages and the home page.
subSections Pages
subSections page.Pages

s *Site

Expand Down Expand Up @@ -515,21 +514,6 @@ type Position struct {
NextInSection page.Page
}

// TODO(bep) page move
type Pages []page.Page

func (ps Pages) String() string {
return fmt.Sprintf("Pages(%d)", len(ps))
}

// Used in tests.
func (ps Pages) shuffle() {
for i := range ps {
j := rand.Intn(i + 1)
ps[i], ps[j] = ps[j], ps[i]
}
}

func (ps Pages) findPagePosByFilename(filename string) int {
for i, x := range ps {
if x.(*Page).Filename() == filename {
Expand Down
14 changes: 7 additions & 7 deletions hugolib/pagecollections.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ import (
// PageCollections contains the page collections for a site.
type PageCollections struct {
// Includes only pages of all types, and only pages in the current language.
Pages Pages
Pages page.Pages

// Includes all pages in all languages, including the current one.
// Includes pages of all types.
AllPages Pages
AllPages page.Pages

// A convenience cache for the traditional index types, taxonomies, home page etc.
// This is for the current language only.
indexPages Pages
indexPages page.Pages

// A convenience cache for the regular pages.
// This is for the current language only.
RegularPages Pages
RegularPages page.Pages

// A convenience cache for the all the regular pages.
AllRegularPages Pages
AllRegularPages page.Pages

// Includes absolute all pages (of all types), including drafts etc.
rawAllPages Pages
rawAllPages page.Pages

// Includes headless bundles, i.e. bundles that produce no output for its content page.
headlessPages Pages
headlessPages page.Pages

pageIndex *cache.Lazy
}
Expand Down
4 changes: 3 additions & 1 deletion hugolib/pages_related.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package hugolib
import (
"sync"

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

"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/related"
"github.com/spf13/cast"
Expand Down Expand Up @@ -134,7 +136,7 @@ func (p Pages) withInvertedIndex(search func(idx *related.InvertedIndex) ([]rela
}

type cachedPostingList struct {
p Pages
p page.Pages

postingList *related.InvertedIndex
}
Expand Down
33 changes: 0 additions & 33 deletions hugolib/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,39 +450,6 @@ func toPagesGroup(seq interface{}) (PagesGroup, error) {
return nil, nil
}

func toPages(seq interface{}) (Pages, error) {
if seq == nil {
return Pages{}, nil
}

switch v := seq.(type) {
case Pages:
return v, nil
case *Pages:
return *(v), nil
case WeightedPages:
return v.Pages(), nil
case PageGroup:
return v.Pages, nil
case []interface{}:
pages := make(Pages, len(v))
success := true
for i, vv := range v {
p, ok := vv.(*Page)
if !ok {
success = false
break
}
pages[i] = p
}
if success {
return pages, nil
}
}

return nil, fmt.Errorf("cannot convert type %T to Pages", seq)
}

// probablyEqual checks page lists for probable equality.
// It may return false positives.
// The motivation behind this is to avoid potential costly reflect.DeepEqual
Expand Down
18 changes: 8 additions & 10 deletions hugolib/pageGroup.go → resources/page/page_group.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 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.
Expand All @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib
package page

import (
"errors"
Expand All @@ -20,7 +20,7 @@ import (
"strings"
"time"

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

// PageGroup represents a group of pages, grouped by the key.
Expand Down Expand Up @@ -172,8 +172,7 @@ func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
var tmp reflect.Value
var keyt reflect.Type
for _, e := range p {
ep := e.(*Page)
param := ep.getParamToLower(key)
param := resource.GetParamToLower(e, key)
if param != nil {
if _, ok := param.([]string); !ok {
keyt = reflect.TypeOf(param)
Expand All @@ -187,8 +186,8 @@ func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
}

for _, e := range p {
ep := e.(*Page)
param := ep.getParam(key, false)
param := resource.GetParam(e, key)

if param == nil || reflect.TypeOf(param) != keyt {
continue
}
Expand Down Expand Up @@ -285,8 +284,7 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
sorter := func(p Pages) Pages {
var r Pages
for _, e := range p {
ep := e.(*Page)
param := ep.getParamToLower(key)
param := resource.GetParamToLower(e, key)
if param != nil {
if _, ok := param.(time.Time); ok {
r = append(r, e)
Expand All @@ -301,7 +299,7 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
return r
}
formatter := func(p *Page) string {
return p.getParamToLower(key).(time.Time).Format(format)
return resource.GetParamToLower(p, key).(time.Time).Format(format)
}
return p.groupByDateField(sorter, formatter, order...)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 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.
Expand All @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib
package page

import (
"errors"
Expand Down
86 changes: 86 additions & 0 deletions resources/page/pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package page

import (
"fmt"
"math/rand"

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

// Pages is a slice of pages. This is the most common list type in Hugo.
type Pages []Page

func (ps Pages) String() string {
return fmt.Sprintf("Pages(%d)", len(ps))
}

// Used in tests.
func (ps Pages) shuffle() {
for i := range ps {
j := rand.Intn(i + 1)
ps[i], ps[j] = ps[j], ps[i]
}
}

// 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 ToPages(seq interface{}) (Pages, error) {
if seq == nil {
return Pages{}, nil
}

switch v := seq.(type) {
case Pages:
return v, nil
case *Pages:
return *(v), nil
case WeightedPages:
return v.Pages(), nil
case PageGroup:
return v.Pages, nil
case []interface{}:
pages := make(Pages, len(v))
success := true
for i, vv := range v {
p, ok := vv.(*Page)
if !ok {
success = false
break
}
pages[i] = p
}
if success {
return pages, nil
}
}

return nil, fmt.Errorf("cannot convert type %T to Pages", seq)
}

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

0 comments on commit ea23fa7

Please sign in to comment.