From 279f73187b9de250de3b1788d1ce543c60832bc6 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 10 Dec 2024 19:00:56 +0100 Subject: [PATCH] feat(custom): config and generation --- data.go | 93 ++++++++++++++++++++++++++++--- main.go | 14 +++++ schema.json | 3 + templates/page/custom_page.gohtml | 8 +++ 4 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 templates/page/custom_page.gohtml diff --git a/data.go b/data.go index 49df295..f809e3f 100644 --- a/data.go +++ b/data.go @@ -1,18 +1,31 @@ package main import ( + "encoding/json" "github.com/anhgelus/golatt" "html/template" + "log/slog" + "os" "strconv" + "strings" +) + +const ( + TitleContentType = "title" + SubtitleContentType = "subtitle" + ParagraphContentType = "paragraph" + ListContentType = "list" + OrderedListContentType = "ordered_list" ) type Config struct { - Image string `json:"image"` - Description string `json:"description"` - Person *Person `json:"person"` - Color *Color `json:"colors"` - Links []*Link `json:"links"` - Legal *Legal `json:"legal"` + Image string `json:"image"` + Description string `json:"description"` + Person *Person `json:"person"` + Color *Color `json:"colors"` + Links []*Link `json:"links"` + Legal *Legal `json:"legal"` + CustomPages []string `json:"custom_pages"` } type Person struct { @@ -79,7 +92,49 @@ func (c *Config) GetBackgroundImage() 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 { @@ -93,3 +148,27 @@ func (b *ButtonColor) GetBackground() template.CSS { func (t *Color) GetTagColor() template.CSS { return template.CSS("--tag-hover: " + t.TagHover + ";") } + +func (c *CustomContent) Get() template.HTML { + if c.Type == TitleContentType { + return template.HTML("

" + c.Content + "

") + } else if c.Type == SubtitleContentType { + return template.HTML("

" + c.Content + "

") + } else if c.Type == ParagraphContentType { + return template.HTML("

" + c.Content + "

") + } else if c.Type == ListContentType { + v := "" + for _, s := range strings.Split(c.Content, "--") { + v += "
  • " + strings.Trim(s, " ") + "
  • " + } + return template.HTML("") + } else if c.Type == OrderedListContentType { + v := "" + for _, s := range strings.Split(c.Content, "--") { + v += "
  • " + strings.Trim(s, " ") + "
  • " + } + return template.HTML("
      " + v + "
    ") + } + slog.Warn("Unknown type", "type", c.Type, "value", c.Content) + return "" +} diff --git a/main.go b/main.go index 57ef485..cd23aea 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,10 @@ func main() { if err != nil { panic(err) } + customPages, err := cfg.LoadCustomPages() + if err != nil { + panic(err) + } g := golatt.New(templates) g.DefaultSeoData = &golatt.SeoData{ Image: cfg.Image, @@ -74,6 +78,16 @@ func main() { &cfg). 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) { http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } diff --git a/schema.json b/schema.json index 2678ecf..dcc75ef 100644 --- a/schema.json +++ b/schema.json @@ -176,6 +176,9 @@ "image", "tags" ] + }, + "custom_page": { + "type": "array" } }, "additionalProperties": false, diff --git a/templates/page/custom_page.gohtml b/templates/page/custom_page.gohtml new file mode 100644 index 0000000..15bd0e9 --- /dev/null +++ b/templates/page/custom_page.gohtml @@ -0,0 +1,8 @@ +{{define "body"}} +
    +

    {{ .Title }}

    + {{ range $content := .Content }} + {{ .Get }} + {{ end }} +
    +{{end}}