diff options
author | Adam Mathes <adam@trenchant.org> | 2017-02-09 19:40:44 -0800 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2017-02-09 19:40:44 -0800 |
commit | ded6e3b25bca083cabe21bda6f4d01f6766587bc (patch) | |
tree | 2855fa24e8b2f939bdf091f5faca411e7884ac83 /archive | |
download | snkt-ded6e3b25bca083cabe21bda6f4d01f6766587bc.tar.gz snkt-ded6e3b25bca083cabe21bda6f4d01f6766587bc.tar.bz2 snkt-ded6e3b25bca083cabe21bda6f4d01f6766587bc.zip |
initial commit (fmstatic -> snkt)
Diffstat (limited to 'archive')
-rw-r--r-- | archive/archive.go | 51 | ||||
-rw-r--r-- | archive/paged.go | 74 |
2 files changed, 125 insertions, 0 deletions
diff --git a/archive/archive.go b/archive/archive.go new file mode 100644 index 0000000..ce1b45e --- /dev/null +++ b/archive/archive.go @@ -0,0 +1,51 @@ +package archive + +import ( + "snkt/config" + "snkt/post" + "snkt/render" + "path" +) + +var archiveTmplName = "archive" +var archiveName = "archive.html" + +/* +ListArchive +*/ +type ListArchive struct { + Posts post.Posts + Tgt string + Template string + + Site interface{} +} + +func NewListArchive(posts post.Posts) *ListArchive { + la := ListArchive{ Posts: posts } + return &la +} + +func (a ListArchive) Target() string { + if a.Tgt == "" { + a.Tgt = path.Join(config.Config.HtmlDir, archiveName) + } + return a.Tgt +} + +func (a ListArchive) Render() []byte { + if a.Template == "" { + a.Template = archiveTmplName + } + return render.Render(a.Template, a) +} + + +/* +NewRssArchive takes posts and returns an archive ready for RSS output +*/ +func NewRssArchive(posts post.Posts) *ListArchive { + ra := ListArchive{ Posts: posts, Template: "rss" } + ra.Tgt = path.Join(config.Config.HtmlDir, "rss.xml") + return &ra +} diff --git a/archive/paged.go b/archive/paged.go new file mode 100644 index 0000000..217e6b3 --- /dev/null +++ b/archive/paged.go @@ -0,0 +1,74 @@ +package archive + +import ( + "snkt/config" + "snkt/post" + "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) + } +} |