-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.go
executable file
·88 lines (70 loc) · 1.72 KB
/
posts.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/gomarkdown/markdown"
"github.com/metakeule/fmtdate"
)
func getPosts(directory string) {
projectsArr = nil
files, _ := ioutil.ReadDir(directory)
for _, file := range files {
if file.IsDir() {
continue
}
fileContents, _ := ioutil.ReadFile(directory + file.Name())
header := strings.Split(string(fileContents), "---")
lines := strings.Split(header[1], "\n")
var fields post
fields.Markdown = header[2]
html := markdown.ToHTML([]byte(header[2]), nil, nil)
fields.HTML = string(html)
filenameSplit := strings.Split(file.Name(), ".")
fields.FileName = filenameSplit[0]
for _, line := range lines {
pair := strings.Split(line, ": ")
if len(pair) == 2 {
switch pair[0] {
case "title":
fields.Name = pair[1]
case "description":
fields.Description = pair[1]
case "date":
date, _ := fmtdate.Parse("YYYY-MM-DD", pair[1])
dateFormatted := date.Format("January 2, 2006")
fields.Date = date
fields.DateFormatted = dateFormatted
case "colourlight":
fields.ColourLight = pair[1]
case "colourdark":
fields.ColourDark = pair[1]
case "tags":
fields.Tags = pair[1]
}
}
}
switch directory {
case "public/projects/":
projectsArr = append(projectsArr, fields)
break
case "public/blog/":
blogsArr = append(blogsArr, fields)
}
}
fmt.Println("Updated projects")
}
func (d byDate) Len() int {
return len(d)
}
func (d byDate) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d byDate) Less(i, j int) bool {
return d[i].Date.After(d[j].Date)
}
func updateDescriptions() {
for i, link := range linksArr {
linksArr[i].Description = getDescription(link.URL)
}
}