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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ post/post.go | 14 +++++++++--- site/site.go | 12 ++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 archive/tag.go 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) + } +} diff --git a/post/post.go b/post/post.go index 6ce195b..7a6f478 100644 --- a/post/post.go +++ b/post/post.go @@ -37,6 +37,7 @@ type Post struct { Day int InFuture bool WordCount int + Tags []string // Content text -- raw, unprocessed, unfiltered markdown Text string @@ -108,7 +109,7 @@ Title, date and other metadata are derived */ func (p *Post) parse() { // - // Text + Meta[string][string] + // fills p.Text, p.Meta[string][string] // p.splitTextMeta() @@ -116,7 +117,7 @@ func (p *Post) parse() { // Title // p.Title = p.Meta["title"] - // Use filename as backup fi we have no explicit title + // Use filename as backup if we have no explicit title if p.Title == "" { p.Title = p.SourceFile } @@ -131,6 +132,14 @@ func (p *Post) parse() { // WordCount p.WordCount = len(strings.Split(p.Text, " ")) + + // Tags + if p.Meta["tags"] != "" { + tags := strings.Split(p.Meta["tags"], ",") + for _, tag := range tags { + p.Tags = append(p.Tags, strings.TrimSpace(tag)) + } + } } /* @@ -213,7 +222,6 @@ func (p *Post) parseDates() { p.Permalink = p.GenPermalink() } - func (p *Post) CleanFilename() string { return text.SanitizeFilename(text.RemoveExt(p.SourceFile)) } diff --git a/site/site.go b/site/site.go index a897f4e..f897171 100644 --- a/site/site.go +++ b/site/site.go @@ -23,6 +23,7 @@ type Site struct { Home *archive.ListArchive Rss *archive.ListArchive Paged *archive.PagedArchives + Tagged *archive.TagArchives } /* @@ -45,6 +46,10 @@ func (s *Site) Read() { if render.TmplExists("paged") { s.Paged = archive.CreatePaged(15, s.Posts) } + if render.TmplExists("tag") { + s.Tagged = archive.ParseTags(s.Posts) + log.Printf("%v\n", s.Tagged) + } if render.TmplExists("home") { s.Home = archive.NewListArchive(s.Posts) s.Home.Tgt = path.Join(config.Config.HtmlDir, "index.html") @@ -126,6 +131,13 @@ func (s *Site) WriteArchives() { render.Write(p) } } + if render.TmplExists("tag") { + for _, t := range *s.Tagged { + log.Printf("%s\n%v\n\n", t.Tag, t.Posts) + t.Site = s + render.Write(t) + } + } } func (s *Site) WritePosts() { -- cgit v1.2.3