From 06d17d98b34c6daf786aa808f73e6f4692d50da0 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Mon, 10 Apr 2017 18:47:41 -0700 Subject: tag wip --- archive/tag.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 archive/tag.go (limited to 'archive') diff --git a/archive/tag.go b/archive/tag.go new file mode 100644 index 0000000..038ccb8 --- /dev/null +++ b/archive/tag.go @@ -0,0 +1,72 @@ +package archive + +import ( + "adammathes.com/snkt/config" + "adammathes.com/snkt/post" + "adammathes.com/snkt/render" + "fmt" + "path" +) + +var tagTmplName = "tag" + +/* +Tag archive shows set of posts broken by tag +Output goes to Config.HtmlDir/tag/{tag}.html +*/ +type TagArchive struct { + Posts post.Posts + Tag string + Site interface{} +} + +func (ta TagArchive) Render() []byte { + return render.Render(tagTmplName, ta) +} + +/* TODO: make this configurable */ +func (ta TagArchive) Target() string { + return path.Join(config.Config.HtmlDir, "/tag/", fmt.Sprintf("%s.html", ta.Tag)) +} + +type TagArchives []*TagArchive + +func ParseTags(posts post.Posts) *TagArchives { + + if !render.TmplExists(tagTmplName) { + fmt.Printf("no tag template\n") + return nil + } + + var tas TagArchives + + // create a map of [tag]posts + var tags map[string]post.Posts + tags = make(map[string]post.Posts) + + for _, p := range posts { + for _, t := range p.Tags { + _, ok := tags[t] + if !ok { + var ps post.Posts + tags[t] = ps + } + tags[t] = append(tags[t], p) + } + } + + for tag, posts := range tags { + var ta TagArchive + ta.Tag = tag + ta.Posts = posts + tas = append(tas, &ta) + } + + return &tas +} + +func (tas *TagArchives) Write() { + for _, ta := range *tas { + render.Write(ta) + } +} -- cgit v1.2.3