mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
37445bc6aa
Two new configuration properties, `Paginate` (default `0`) and `PaginatePath` (default `page`) are added. Setting `paginate` to a positive value will split the list pages for the home page, sections and taxonomies into chunks of size of the `paginate` property. A `.Paginator` is provided to help building a pager menu. There are two ways to configure a `.Paginator`: 1. The simplest way is just to call `.Paginator.Pages` from a template. It will contain the pages for "that page" (`.Data.Pages` will (like today) contain all the pages). 2. Select a sub-set of the pages with the available template functions and pass the slice to `.Paginate` : `{{ range (.Paginate (where .Data.Pages "Type" "post")).Pages }}` **NOTE:** For a given Node, it's one of the options above. It's perfectly legitimate to iterate over the same pager more than once, but it's static and cannot change. The `.Paginator` contains enough information to build a full-blown paginator interface. The pages are built on the form (note: BLANK means no value, i.e. home page): ``` [SECTION/TAXONOMY/BLANK]/index.html [SECTION/TAXONOMY/BLANK]/page/1/index.html => redirect to [SECTION/TAXONOMY/BLANK]/index.html [SECTION/TAXONOMY/BLANK]/page/2/index.html .... ``` Fixes #96
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package hugolib
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/hugo/source"
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplitPages(t *testing.T) {
|
|
|
|
pages := createTestPages(21)
|
|
chunks := splitPages(pages, 5)
|
|
assert.Equal(t, 5, len(chunks))
|
|
|
|
for i := 0; i < 4; i++ {
|
|
assert.Equal(t, 5, len(chunks[i]))
|
|
}
|
|
|
|
lastChunk := chunks[4]
|
|
assert.Equal(t, 1, len(lastChunk))
|
|
|
|
}
|
|
|
|
func TestPaginator(t *testing.T) {
|
|
|
|
pages := createTestPages(21)
|
|
urlFactory := func(page int) string {
|
|
return fmt.Sprintf("page/%d/", page)
|
|
}
|
|
|
|
paginator := newPaginator(pages, 5, urlFactory)
|
|
paginatorPages := paginator.Pagers()
|
|
|
|
assert.Equal(t, 5, len(paginatorPages))
|
|
assert.Equal(t, 21, paginator.TotalNumberOfElements())
|
|
assert.Equal(t, 5, paginator.PageSize())
|
|
assert.Equal(t, 5, paginator.TotalPages())
|
|
|
|
first := paginatorPages[0]
|
|
assert.Equal(t, "page/1/", first.Url())
|
|
assert.Equal(t, first, first.First())
|
|
assert.Equal(t, true, first.HasNext())
|
|
assert.Equal(t, false, first.HasPrev())
|
|
assert.Equal(t, 5, first.NumberOfElements())
|
|
assert.Equal(t, 1, first.PageNumber())
|
|
|
|
third := paginatorPages[2]
|
|
assert.Equal(t, true, third.HasNext())
|
|
assert.Equal(t, true, third.HasPrev())
|
|
|
|
last := paginatorPages[4]
|
|
assert.Equal(t, "page/5/", last.Url())
|
|
assert.Equal(t, last, last.Last())
|
|
assert.Equal(t, false, last.HasNext())
|
|
assert.Equal(t, true, last.HasPrev())
|
|
assert.Equal(t, 1, last.NumberOfElements())
|
|
assert.Equal(t, 5, last.PageNumber())
|
|
|
|
}
|
|
|
|
func TestPaginationUrlFactory(t *testing.T) {
|
|
viper.Set("PaginatePath", "zoo")
|
|
unicode := newPaginationUrlFactory("новости проекта")
|
|
fooBar := newPaginationUrlFactory("foo", "bar")
|
|
|
|
assert.Equal(t, "/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B0/", unicode(1))
|
|
assert.Equal(t, "/foo/bar/", fooBar(1))
|
|
assert.Equal(t, "/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B0/zoo/4/", unicode(4))
|
|
assert.Equal(t, "/foo/bar/zoo/12345/", fooBar(12345))
|
|
|
|
}
|
|
|
|
func createTestPages(num int) Pages {
|
|
pages := make(Pages, num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
pages[i] = &Page{
|
|
Node: Node{
|
|
UrlPath: UrlPath{
|
|
Section: "z",
|
|
Url: fmt.Sprintf("http://base/x/y/p%d.html", num),
|
|
},
|
|
Site: &SiteInfo{
|
|
BaseUrl: "http://base/",
|
|
},
|
|
},
|
|
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", num)))},
|
|
}
|
|
}
|
|
|
|
return pages
|
|
}
|