1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package archive
import (
"adammathes.com/snkt/config"
"adammathes.com/snkt/post"
"adammathes.com/snkt/render"
"fmt"
"path"
"sort"
)
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)
}
func (ta TagArchive) Target() string {
return path.Join(config.Config.HtmlDir, "tag", ta.Tag, "index.html")
}
type TagArchives []*TagArchive
func (tags TagArchives) Len() int {
return len(tags)
}
func (tags TagArchives) Less(i, j int) bool {
return tags[i].Tag < tags[j].Tag
}
func (tags TagArchives) Swap(i, j int) {
tags[i], tags[j] = tags[j], tags[i]
}
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)
}
sort.Sort(tas)
return &tas
}
func (tas *TagArchives) Write() {
for _, ta := range *tas {
render.Write(ta)
}
}
|