/
index_page.go
265 lines (219 loc) · 7.02 KB
/
index_page.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package sitebuilder
import (
"bytes"
"fmt"
"html/template"
"time"
"hermannm.dev/wrap"
)
type IndexPageBase struct {
ProfilePictureMobile Image `yaml:"profilePictureMobile"`
ProfilePictureDesktop Image `yaml:"profilePictureDesktop"`
}
type IndexPageMarkdown struct {
IndexPageBase ` yaml:",inline"`
Page Page `yaml:"page"`
PersonalInfo PersonalInfoMarkdown `yaml:"personalInfo"`
ProjectGroups []ProjectGroupMarkdown `yaml:"projectGroups,flow" validate:"required,dive"`
}
type PersonalInfoMarkdown struct {
Birthday string `yaml:"birthday" validate:"required"`
Location string `yaml:"location" validate:"required"`
GitHubURL string `yaml:"githubURL" validate:"required,url"`
LinkedInURL string `yaml:"linkedinURL" validate:"required,url"`
}
type IndexPageTemplate struct {
IndexPageBase
Meta TemplateMetadata
AboutMe template.HTML
PersonalInfo []LinkItem // May omit Link field.
ProjectGroups []ProjectGroupTemplate
}
type ProjectGroupMarkdown struct {
Title string `yaml:"title" validate:"required"`
ProjectSlugs []string `yaml:"projectSlugs,flow" validate:"required,dive"`
ContentDir string `yaml:"contentDir" validate:"required"`
}
type ProjectGroupTemplate struct {
Title string
Projects []ProjectProfile
}
type Image struct {
Path string `yaml:"path" validate:"required,filepath"`
Alt string `yaml:"alt" validate:"required"`
Width int `yaml:"width" validate:"required"`
Height int `yaml:"height" validate:"required"`
}
func (renderer *PageRenderer) RenderIndexPage(contentPath string) (err error) {
defer func() {
if err != nil {
renderer.cancel()
}
}()
content, aboutMeText, err := parseIndexPageContent(contentPath)
if err != nil {
return wrap.Error(err, "failed to parse index page data")
}
projectGroups := parseProjectGroups(content.ProjectGroups)
renderer.parsedPages <- content.Page
// Waits for icons to finish rendering before using them
select {
case <-renderer.ctx.Done():
return renderer.ctx.Err()
case <-renderer.iconsRendered:
}
personalInfo, err := content.PersonalInfo.toTemplateFields(renderer.icons)
if err != nil {
return wrap.Error(err, "failed to parse personal info from index page content")
}
ProjectLoop:
for i := 0; i < renderer.projectCount; i++ {
select {
case project := <-renderer.parsedProjects:
if err = projectGroups.AddIfIncluded(project); err != nil {
return wrap.Errorf(err, "failed to add project '%s' to groups", project.Slug)
}
if projectGroups.IsFull() {
break ProjectLoop
}
case <-renderer.ctx.Done():
return nil
}
}
if !projectGroups.IsFull() {
return fmt.Errorf(
"index page did not receive all projects specified in 'projectGroups' in %s",
contentPath,
)
}
pageTemplate := IndexPageTemplate{
IndexPageBase: content.IndexPageBase,
Meta: TemplateMetadata{
Common: renderer.commonData,
Page: content.Page,
},
AboutMe: aboutMeText,
PersonalInfo: personalInfo,
ProjectGroups: projectGroups.ToSlice(),
}
if err = renderer.renderPage(pageTemplate.Meta, pageTemplate); err != nil {
return wrap.Error(err, "failed to render index page")
}
return nil
}
func parseIndexPageContent(
contentPath string,
) (content IndexPageMarkdown, aboutMeText template.HTML, err error) {
path := fmt.Sprintf("%s/%s", BaseContentDir, contentPath)
aboutMeBuffer := new(bytes.Buffer)
if err := readMarkdownWithFrontmatter(path, aboutMeBuffer, &content); err != nil {
return IndexPageMarkdown{}, "", wrap.Error(err, "failed to read markdown for index page")
}
if err := validate.Struct(content); err != nil {
return IndexPageMarkdown{}, "", wrap.Error(err, "invalid index page metadata")
}
aboutMeText = removeParagraphTagsAroundHTML(aboutMeBuffer.String())
return content, aboutMeText, nil
}
func (personalInfo PersonalInfoMarkdown) toTemplateFields(icons IconMap) ([]LinkItem, error) {
birthday, err := time.Parse(time.DateOnly, personalInfo.Birthday)
if err != nil {
return nil, wrap.Errorf(err, "failed to parse birthday field '%s'", personalInfo.Birthday)
}
fields := []LinkItem{
{Text: fmt.Sprintf("%d years old", ageFromBirthday(birthday))},
{Text: personalInfo.Location},
{Text: "GitHub", Link: personalInfo.GitHubURL},
{Text: "LinkedIn", Link: personalInfo.LinkedInURL},
}
for i, iconName := range [4]string{"person", "map-marker", "GitHub", "LinkedIn"} {
icon, ok := icons[iconName]
if !ok {
return nil, fmt.Errorf("expected '%s' icon in icon map, but found none", iconName)
}
fields[i].Icon = template.HTML(icon.Icon)
}
return fields, nil
}
func ageFromBirthday(birthday time.Time) int {
now := time.Now()
age := now.Year() - birthday.Year()
birthdayCelebratedThisYear := now.YearDay() >= birthday.YearDay()
if !birthdayCelebratedThisYear {
age--
}
return age
}
func parseProjectGroups(groups []ProjectGroupMarkdown) ParsedProjectGroups {
parsedGroups := make([]ParsedProjectGroup, len(groups))
targetNumberOfProjects := 0
for i, group := range groups {
projectsLength := len(group.ProjectSlugs)
projectIndicesBySlug := make(map[string]int, projectsLength)
for j, slug := range group.ProjectSlugs {
projectIndicesBySlug[slug] = j
targetNumberOfProjects++
}
parsedGroups[i] = ParsedProjectGroup{
ProjectGroupTemplate: ProjectGroupTemplate{
Title: group.Title,
Projects: make([]ProjectProfile, projectsLength),
},
projectIndicesBySlug: projectIndicesBySlug,
contentDir: group.ContentDir,
}
}
return ParsedProjectGroups{
list: parsedGroups,
numberOfProjects: 0,
targetNumberOfProjects: targetNumberOfProjects,
}
}
type ParsedProjectGroups struct {
list []ParsedProjectGroup
numberOfProjects int
targetNumberOfProjects int
}
type ParsedProjectGroup struct {
ProjectGroupTemplate
projectIndicesBySlug map[string]int
contentDir string
}
func (groups *ParsedProjectGroups) AddIfIncluded(project ParsedProject) error {
var group ParsedProjectGroup
isIncluded := false
for _, candidate := range groups.list {
if candidate.contentDir == project.ContentDir {
group = candidate
isIncluded = true
break
}
}
if !isIncluded {
return nil
}
index, isIncluded := group.projectIndicesBySlug[project.Slug]
if !isIncluded {
return nil
}
projects := group.Projects
if index >= len(projects) {
return fmt.Errorf("project index in group '%s' is out-of-bounds", group.Title)
}
if project.LogoPath == "" && project.IndexPageFallbackIcon == "" {
return fmt.Errorf("no icon found for project '%s'", project.Slug)
}
group.Projects[index] = project.ProjectProfile
groups.numberOfProjects++
return nil
}
func (groups *ParsedProjectGroups) IsFull() bool {
return groups.numberOfProjects == groups.targetNumberOfProjects
}
func (groups *ParsedProjectGroups) ToSlice() []ProjectGroupTemplate {
slice := make([]ProjectGroupTemplate, 0, len(groups.list))
for _, group := range groups.list {
slice = append(slice, group.ProjectGroupTemplate)
}
return slice
}