diff options
author | Adam Mathes <adam@trenchant.org> | 2017-04-10 18:47:41 -0700 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2017-04-10 18:47:41 -0700 |
commit | 06d17d98b34c6daf786aa808f73e6f4692d50da0 (patch) | |
tree | 4c891512b24cb3a686cdbcd3e0f412d439eded32 /archive | |
parent | 0bde21f3da1152eeaea0a8f712f7244b26d5df02 (diff) | |
download | snkt-06d17d98b34c6daf786aa808f73e6f4692d50da0.tar.gz snkt-06d17d98b34c6daf786aa808f73e6f4692d50da0.tar.bz2 snkt-06d17d98b34c6daf786aa808f73e6f4692d50da0.zip |
tag wip
Diffstat (limited to 'archive')
-rw-r--r-- | archive/tag.go | 72 |
1 files changed, 72 insertions, 0 deletions
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) + } +} |