diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2014-12-27 14:11:19 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-01-26 12:59:37 +0100 |
commit | 37445bc6aa6273a819724eb25f24f08265d21d4b (patch) | |
tree | ed96a311dd12c5fd7ab6195071a3c12b57da77b0 /hugolib/pagination_test.go | |
parent | 407e80a9abbb3b22397d1ed6c62ce7cefcdd312a (diff) |
Add pagination support for home page, sections and taxonomies
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
Diffstat (limited to 'hugolib/pagination_test.go')
-rw-r--r-- | hugolib/pagination_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/hugolib/pagination_test.go b/hugolib/pagination_test.go new file mode 100644 index 000000000..080866f6f --- /dev/null +++ b/hugolib/pagination_test.go @@ -0,0 +1,95 @@ +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 +} |