/
common_data.go
74 lines (61 loc) · 1.78 KB
/
common_data.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
package sitebuilder
import (
"html/template"
"strings"
)
type LinkItem struct {
Text string `yaml:"text" validate:"required"`
Link string `yaml:"link" validate:"omitempty,url"`
Icon template.HTML `yaml:"icon" validate:"omitempty,filepath"`
}
type TemplateMetadata struct {
Common CommonPageData
Page Page
}
type CommonPageData struct {
SiteName string `validate:"required"`
SiteDescription string `validate:"required"`
BaseURL string `validate:"required,url"`
GitHubIssuesLink string `validate:"required,url"`
githubIcon template.HTML
}
func (commonData CommonPageData) GitHubIcon() template.HTML {
return commonData.githubIcon
}
type Page struct {
Title string `yaml:"title" validate:"required"`
Path string `yaml:"path"`
TemplateName string `yaml:"templateName" validate:"required,filepath"`
RedirectURL string `yaml:"redirectURL"` // Optional.
// Nil if page does not host a Go package.
GoPackage *GoPackage `yaml:"goPackage" validate:"omitempty"`
}
type GoPackage struct {
RootName string `yaml:"rootName" validate:"required"`
GitHubURL string `yaml:"githubURL" validate:"required,url"`
}
var TemplateFunctions = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
"personalInfoTextWrapping": func(infoText string) template.HTML {
words := strings.Split(infoText, " ")
wordCount := len(words)
if wordCount < 3 {
return template.HTML(infoText)
}
var builder strings.Builder
builder.WriteString(`<span class="whitespace-nowrap">`)
cutoff := (wordCount - 1) / 2
for i, word := range words {
builder.WriteString(word)
if i == cutoff {
builder.WriteString("</span>")
}
if i != wordCount-1 {
builder.WriteString(" ")
}
}
return template.HTML(builder.String())
},
}