From ded6e3b25bca083cabe21bda6f4d01f6766587bc Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Thu, 9 Feb 2017 19:40:44 -0800 Subject: initial commit (fmstatic -> snkt) --- archive/archive.go | 51 +++++++++++++++++++++++++++++++++++++ archive/paged.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 archive/archive.go create mode 100644 archive/paged.go (limited to 'archive') 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) + } +} -- cgit v1.2.3