diff options
-rw-r--r-- | archive/tag.go | 72 | ||||
-rw-r--r-- | post/post.go | 14 | ||||
-rw-r--r-- | site/site.go | 12 |
3 files changed, 95 insertions, 3 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) + } +} 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() { |