feat(custom): config and generation
This commit is contained in:
parent
730ec34758
commit
279f73187b
4 changed files with 111 additions and 7 deletions
93
data.go
93
data.go
|
@ -1,18 +1,31 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/anhgelus/golatt"
|
"github.com/anhgelus/golatt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TitleContentType = "title"
|
||||||
|
SubtitleContentType = "subtitle"
|
||||||
|
ParagraphContentType = "paragraph"
|
||||||
|
ListContentType = "list"
|
||||||
|
OrderedListContentType = "ordered_list"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Person *Person `json:"person"`
|
Person *Person `json:"person"`
|
||||||
Color *Color `json:"colors"`
|
Color *Color `json:"colors"`
|
||||||
Links []*Link `json:"links"`
|
Links []*Link `json:"links"`
|
||||||
Legal *Legal `json:"legal"`
|
Legal *Legal `json:"legal"`
|
||||||
|
CustomPages []string `json:"custom_pages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
|
@ -79,7 +92,49 @@ func (c *Config) GetBackgroundImage() template.CSS {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetTextColor() template.CSS {
|
func (c *Config) GetTextColor() template.CSS {
|
||||||
return template.CSS("--text-color: " + c.Color.Text + ";")
|
return c.Color.GetTextColor()
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomPage struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
URI string `json:"uri"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Color *Color `json:"colors"`
|
||||||
|
Content []*CustomContent `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomContent struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Content interface {
|
||||||
|
Get() template.HTML
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
|
||||||
|
if c.CustomPages == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var pages []*CustomPage
|
||||||
|
for _, cp := range c.CustomPages {
|
||||||
|
b, err := os.ReadFile(cp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var p *CustomPage
|
||||||
|
err = json.Unmarshal(b, p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pages = append(pages, p)
|
||||||
|
}
|
||||||
|
return pages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Color) GetTextColor() template.CSS {
|
||||||
|
return template.CSS("--text-color: " + t.Text + ";")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ButtonColor) GetTextColor() template.CSS {
|
func (b *ButtonColor) GetTextColor() template.CSS {
|
||||||
|
@ -93,3 +148,27 @@ func (b *ButtonColor) GetBackground() template.CSS {
|
||||||
func (t *Color) GetTagColor() template.CSS {
|
func (t *Color) GetTagColor() template.CSS {
|
||||||
return template.CSS("--tag-hover: " + t.TagHover + ";")
|
return template.CSS("--tag-hover: " + t.TagHover + ";")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CustomContent) Get() template.HTML {
|
||||||
|
if c.Type == TitleContentType {
|
||||||
|
return template.HTML("<h2>" + c.Content + "</h2>")
|
||||||
|
} else if c.Type == SubtitleContentType {
|
||||||
|
return template.HTML("<h3>" + c.Content + "</h3>")
|
||||||
|
} else if c.Type == ParagraphContentType {
|
||||||
|
return template.HTML("<p>" + c.Content + "</p>")
|
||||||
|
} else if c.Type == ListContentType {
|
||||||
|
v := ""
|
||||||
|
for _, s := range strings.Split(c.Content, "--") {
|
||||||
|
v += "<li>" + strings.Trim(s, " ") + "</li>"
|
||||||
|
}
|
||||||
|
return template.HTML("<ul>" + v + "</ul>")
|
||||||
|
} else if c.Type == OrderedListContentType {
|
||||||
|
v := ""
|
||||||
|
for _, s := range strings.Split(c.Content, "--") {
|
||||||
|
v += "<li>" + strings.Trim(s, " ") + "</li>"
|
||||||
|
}
|
||||||
|
return template.HTML("<ol>" + v + "</ol>")
|
||||||
|
}
|
||||||
|
slog.Warn("Unknown type", "type", c.Type, "value", c.Content)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
14
main.go
14
main.go
|
@ -50,6 +50,10 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
customPages, err := cfg.LoadCustomPages()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
g := golatt.New(templates)
|
g := golatt.New(templates)
|
||||||
g.DefaultSeoData = &golatt.SeoData{
|
g.DefaultSeoData = &golatt.SeoData{
|
||||||
Image: cfg.Image,
|
Image: cfg.Image,
|
||||||
|
@ -74,6 +78,16 @@ func main() {
|
||||||
&cfg).
|
&cfg).
|
||||||
Handle()
|
Handle()
|
||||||
|
|
||||||
|
for _, cp := range customPages {
|
||||||
|
g.NewTemplate("custom_page",
|
||||||
|
cp.URI,
|
||||||
|
cp.Title,
|
||||||
|
cp.Image,
|
||||||
|
cp.Description,
|
||||||
|
cp).
|
||||||
|
Handle()
|
||||||
|
}
|
||||||
|
|
||||||
g.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) {
|
g.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,6 +176,9 @@
|
||||||
"image",
|
"image",
|
||||||
"tags"
|
"tags"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"custom_page": {
|
||||||
|
"type": "array"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
|
8
templates/page/custom_page.gohtml
Normal file
8
templates/page/custom_page.gohtml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{{define "body"}}
|
||||||
|
<main class="custom-page" style="{{ .GetBackground }}">
|
||||||
|
<h2>{{ .Title }}</h2>
|
||||||
|
{{ range $content := .Content }}
|
||||||
|
{{ .Get }}
|
||||||
|
{{ end }}
|
||||||
|
</main>
|
||||||
|
{{end}}
|
Loading…
Add table
Add a link
Reference in a new issue