aboutsummaryrefslogtreecommitdiffstats
path: root/archive/paged.go
blob: d308d3d3340c1e0a24447871bf744ed631d23e24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package archive

import (
	"adammathes.com/snkt/config"
	"adammathes.com/snkt/post"
	"adammathes.com/snkt/render"
	"fmt"
	"math"
	"path"
	"sort"
)

var pagedTmplName = "paged"

/*
Paged archive shows set of posts broken up over multiple pages
Output goes to Config.HtmlDir/page/{pageNum}.html
*/
type PagedArchive struct {
	Posts    post.Posts
	PageNum  int
	NextPage int
	PrevPage int

	Site interface{}
}

func (pa PagedArchive) Render() []byte {
	return render.Render(pagedTmplName, pa)
}

/* TODO: make this configurable */
func (pa PagedArchive) Target() string {
	return path.Join(config.Config.HtmlDir, "/page/", fmt.Sprintf("%d.html", pa.PageNum))
}

type PagedArchives []*PagedArchive

func CreatePaged(perPage int, posts post.Posts) *PagedArchives {

	if !render.TmplExists(pagedTmplName) {
		fmt.Printf("no page template\n")
		return nil
	}
	
	var pas PagedArchives

	sort.Sort(sort.Reverse(posts))

	numPages := int(math.Ceil(float64(len(posts)) / float64(perPage)))
	for i := 0; i < numPages; i++ {
		var pa PagedArchive

		var m int
		if (i+1)*perPage > len(posts) {
			m = len(posts)
		} else {
			m = (i + 1) * perPage
		}

		pa.Posts = posts[i*perPage : m]
		pa.PrevPage = i
		pa.PageNum = i + 1
		pa.NextPage = i + 2
		pas = append(pas, &pa)
	}
	return &pas
}

func (pas *PagedArchives) Write() {
	for _, pa := range *pas {
		render.Write(pa)
	}
}