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
|
package main
import (
"encoding/json"
"errors"
"html/template"
"os"
"regexp"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/anhgelus/golatt"
)
var (
regexExternalLink = regexp.MustCompile(`https?://`)
)
type ConfigData interface {
GetTextColor() template.CSS
GetBackground() template.CSS
GetBackgroundImage() template.CSS
IsCustomPage() bool
}
type Config struct {
Image string `json:"image" toml:"image"`
Description string `json:"description" toml:"description"`
folder string
Legal string `json:"legal" toml:"legal"`
Font string `json:"font" toml:"font"`
RelMeLinks []string `json:"rel_me_links" toml:"rel_me_links"`
CustomPages []string `json:"custom_pages" toml:"custom_pages"`
Person *Person `json:"person" toml:"person"`
Color *Color `json:"colors" toml:"colors"`
Links []*Link `json:"links" toml:"links"`
Rings []*Ring `json:"rings" toml:"rings"`
}
type Ring struct {
Image string `json:"image" toml:"image"`
Name string `json:"name" toml:"name"`
Link string `json:"link" toml:"link"`
}
type Person struct {
Name string `json:"name" toml:"name"`
Pronouns string `json:"pronouns" toml:"pronouns"`
Image string `json:"image" toml:"image"`
Tags []*Tag `json:"tags" toml:"tags"`
}
type Tag struct {
Name string `json:"name" toml:"name"`
Description string `json:"description" toml:"description"`
Link string `json:"link" toml:"link"`
LinkName string `json:"link_name" toml:"link_name"`
}
type Color struct {
Background *BackgroundColor `json:"background" toml:"background"`
Button *ButtonColor `json:"buttons" toml:"buttons"`
Text string `json:"text" toml:"text"`
TagHover string `json:"tag_hover" toml:"tag_hover"`
}
type BackgroundColor struct {
Type string `json:"type" toml:"type"`
Angle uint `json:"angle" toml:"angle"`
Colors []struct {
Color string `json:"color" toml:"color"`
Position uint `json:"position" toml:"position"`
} `json:"colors" toml:"colors"`
}
type ButtonColor struct {
Text string `json:"text" toml:"text"`
TextHover string `json:"text_hover" toml:"text_hover"`
Background string `json:"background" toml:"background"`
BackgroundHover string `json:"background_hover" toml:"background_hover"`
}
type Link struct {
Link string `json:"link" toml:"link"`
Content string `json:"content" toml:"content"`
}
func getImage(s string) string {
if regexExternalLink.MatchString(s) {
return s
}
return golatt.GetStaticPath(s)
}
func (c *Config) GetBackground() template.CSS {
return c.Color.GetBackground()
}
func (c *Config) GetBackgroundImage() template.CSS {
return template.CSS("--background-image: url(" + getImage(c.Image) + ");")
}
func (c *Config) GetTextColor() template.CSS {
return c.Color.GetTextColor()
}
func (c *Config) IsCustomPage() bool {
return false
}
var legalContent template.HTML
func (c *Config) GetLegal() (template.HTML, error) {
if legalContent == "" {
b, err := os.ReadFile(c.folder + c.Legal)
if err != nil {
return "", err
}
legalContent = template.HTML(b)
}
return legalContent, nil
}
type CustomPage struct {
Title string `json:"title" toml:"title"`
URI string `json:"uri" toml:"uri"`
Image string `json:"image" toml:"image"`
Description string `json:"description" toml:"description"`
Color *Color `json:"colors" toml:"colors"`
Content string `json:"content" toml:"content"`
folder string
}
func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
if c.CustomPages == nil {
println("null")
return nil, nil
}
var pages []*CustomPage
for _, cp := range c.CustomPages {
b, err := os.ReadFile(c.folder + cp)
if err != nil {
return nil, err
}
var p CustomPage
if strings.HasSuffix(cp, ".json") {
err = json.Unmarshal(b, &p)
} else if strings.HasSuffix(cp, ".toml") {
err = toml.Unmarshal(b, &p)
} else {
return nil, errors.New("custom page file must be .json or .toml")
}
if err != nil {
return nil, err
}
p.folder = getFolder(c.folder + cp)
pages = append(pages, &p)
}
return pages, nil
}
func (t *Color) GetTextColor() template.CSS {
return template.CSS("--text-color: " + t.Text + ";")
}
func (t *Color) GetBackground() template.CSS {
bg := t.Background
css := "background: " + bg.Type + "-gradient("
if bg.Type == "linear" {
css += strconv.Itoa(int(bg.Angle)) + "deg,"
}
for _, c := range bg.Colors {
css += c.Color + " " + strconv.Itoa(int(c.Position)) + "%,"
}
return template.CSS(css[:len(css)-1] + ");")
}
func (b *ButtonColor) GetTextColor() template.CSS {
return template.CSS("--text-color: " + b.Text + ";--text-color-hover: " + b.TextHover + ";")
}
func (b *ButtonColor) GetBackground() template.CSS {
return template.CSS("--background: " + b.Background + ";--background-hover: " + b.BackgroundHover + ";")
}
func (t *Color) GetTagColor() template.CSS {
return template.CSS("--tag-hover: " + t.TagHover + ";")
}
func (p *CustomPage) GetTextColor() template.CSS {
return p.Color.GetTextColor()
}
func (p *CustomPage) GetBackgroundImage() template.CSS {
return template.CSS("--background-image: url(" + getImage(p.Image) + ");")
}
func (p *CustomPage) GetBackground() template.CSS {
return p.Color.GetBackground()
}
func (p *CustomPage) IsCustomPage() bool {
return true
}
var contentsMap = map[string]template.HTML{}
func (p *CustomPage) GetContent() (template.HTML, error) {
res, ok := contentsMap[p.URI]
if !ok {
b, err := os.ReadFile(p.folder + p.Content)
if err != nil {
return "", err
}
res = template.HTML(b)
contentsMap[p.URI] = res
}
return res, nil
}
|