-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (146 loc) · 3.34 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"os"
"regexp"
"sort"
"strconv"
"github.com/yuin/goldmark"
)
type ConfigDescription struct {
OneLine string
Full string
}
type NewsItem struct {
Year int
Month int
Day int
Date string
Content template.HTML
}
type Config struct {
Title string
Name string
Description ConfigDescription
AboutMe template.HTML
News []NewsItem
}
func renderMarkdownFile(path string) (string, error) {
mdBytes, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read %s: %w", path, err)
}
var htmlBytes bytes.Buffer
err = goldmark.Convert(mdBytes, &htmlBytes)
if err != nil {
return "", fmt.Errorf("failed to render markdown from %s: %w", path, err)
}
return htmlBytes.String(), nil
}
func parseConfig() Config {
configText, err := os.ReadFile("config.json")
if err != nil {
log.Fatalf("failed to read config.json: %v", err)
}
var config Config
err = json.Unmarshal(configText, &config)
if err != nil {
log.Fatalf("failed to parse config.json: %v", err)
}
return config
}
var monthNames = [...]string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}
func renderNewsItemFromFile(path string) (*NewsItem, error) {
newsFilenameRegex := regexp.MustCompile(`(?P<Year>\d{4})-(?P<Month>\d{2})-(?P<Day>\d{2})-.+\.md$`)
match := newsFilenameRegex.FindStringSubmatch(path)
if match == nil {
return nil, fmt.Errorf("invalid filename: %s", path)
}
year, err := strconv.Atoi(match[1])
if err != nil {
return nil, fmt.Errorf("not a valid year: %s", match[1])
}
month, err := strconv.Atoi(match[2])
if err != nil {
return nil, fmt.Errorf("not a valid month: %s", match[2])
}
day, err := strconv.Atoi(match[3])
if err != nil {
return nil, fmt.Errorf("not a valid day: %s", match[3])
}
html, err := renderMarkdownFile(path)
if err != nil {
return nil, fmt.Errorf("failed to render news item: %w", err)
}
return &NewsItem{
Year: year,
Month: month,
Day: day,
Date: fmt.Sprintf("%s'%d", monthNames[month-1], year-2000),
Content: template.HTML(html),
}, nil
}
func sortNewToOld(news []NewsItem) {
sort.Slice(news, func(i, j int) bool {
a := news[i]
b := news[j]
if a.Year > b.Year {
return true
}
if a.Year == b.Year && a.Month > b.Month {
return true
}
if a.Year == b.Year && a.Month == b.Month && a.Day > b.Day {
return true
}
return false
})
}
func main() {
tmpl, err := template.ParseFiles("index.template.html")
if err != nil {
log.Fatalf("failed to open index.template.html: %v", err)
}
config := parseConfig()
aboutMe, err := renderMarkdownFile("about_me.md")
if err != nil {
log.Fatalf("failed to render about me: %v", err)
}
config.AboutMe = template.HTML(aboutMe)
newsDir, err := os.ReadDir("news/")
if err != nil {
log.Fatalf("failed to read news/: %v", err)
}
for _, newsDirEntry := range newsDir {
path := "news/" + newsDirEntry.Name()
item, err := renderNewsItemFromFile(path)
if err != nil {
log.Fatalf("failed to render news item from %s: %v", path, err)
}
config.News = append(config.News, *item)
}
sortNewToOld(config.News)
out, err := os.Create("index.html")
if err != nil {
log.Fatalf("failed to create out.html: %v", err)
}
tmpl.Execute(out, config)
out.Close()
}