/
categories.go
66 lines (54 loc) · 1.37 KB
/
categories.go
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
package main
import (
"io/ioutil"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
func categoriesForSite(site Site) ([]string, error) {
resp := make([]string, 0)
posts := make(listPostsResponse)
// Get directory listing for Location /source/_posts/
fis, err := ioutil.ReadDir(site.Location + "/source/_posts/")
if err != nil {
return resp, err
}
for iter := 0; iter < len(fis); iter++ {
slug := strings.Replace(fis[iter].Name(), ".md", "", 1)
item := listPostItem{
Slug: slug,
Filename: fis[iter].Name(),
}
// Pull rest of information from yaml
filedata, err := ioutil.ReadFile(site.Location + "/source/_posts/" + fis[iter].Name())
if err != nil {
continue
}
postconfig := postYaml{}
err = yaml.Unmarshal([]byte(filedata), &postconfig)
if err != nil {
continue
}
item.Author = postconfig.Author
item.Title = postconfig.Title
item.Permalink = postconfig.Permalink
item.Categories = postconfig.Categories
posts[slug] = item
}
cMap := make(map[string]bool)
// Get unique categories as map keys
for k := range posts {
for cIdx := range posts[k].Categories {
if !cMap[posts[k].Categories[cIdx]] {
cMap[posts[k].Categories[cIdx]] = true
}
}
}
// Pull out keys into an array to return
for ck, _ := range cMap {
resp = append(resp, ck)
}
// But make sure we do an alpha sort first
sort.Strings(resp)
return resp, nil
}