From 279f73187b9de250de3b1788d1ce543c60832bc6 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 10 Dec 2024 19:00:56 +0100 Subject: feat(custom): config and generation --- data.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'data.go') 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 "" +} -- cgit v1.2.3 From e379601ec1f1ca70f955f7ea9dd07775fb4da93b Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 10 Dec 2024 19:13:44 +0100 Subject: fix(custom): missing method for new config --- data.go | 53 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) (limited to 'data.go') diff --git a/data.go b/data.go index f809e3f..ad6fcea 100644 --- a/data.go +++ b/data.go @@ -18,6 +18,12 @@ const ( OrderedListContentType = "ordered_list" ) +type ConfigData interface { + GetTextColor() template.CSS + GetBackground() template.CSS + GetBackgroundImage() template.CSS +} + type Config struct { Image string `json:"image"` Description string `json:"description"` @@ -76,15 +82,7 @@ type Legal struct { } func (c *Config) GetBackground() template.CSS { - bg := c.Color.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] + ");") + return c.Color.GetBackground() } func (c *Config) GetBackgroundImage() template.CSS { @@ -115,6 +113,7 @@ type Content interface { func (c *Config) LoadCustomPages() ([]*CustomPage, error) { if c.CustomPages == nil { + println("null") return nil, nil } var pages []*CustomPage @@ -123,12 +122,12 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) { if err != nil { return nil, err } - var p *CustomPage - err = json.Unmarshal(b, p) + var p CustomPage + err = json.Unmarshal(b, &p) if err != nil { return nil, err } - pages = append(pages, p) + pages = append(pages, &p) } return pages, nil } @@ -137,6 +136,18 @@ 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 + ";") } @@ -149,6 +160,18 @@ 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(" + golatt.GetStaticPath(p.Image) + ");") +} + +func (p *CustomPage) GetBackground() template.CSS { + return p.Color.GetBackground() +} + func (c *CustomContent) Get() template.HTML { if c.Type == TitleContentType { return template.HTML("

    " + c.Content + "

    ") @@ -159,12 +182,18 @@ func (c *CustomContent) Get() template.HTML { } else if c.Type == ListContentType { v := "" for _, s := range strings.Split(c.Content, "--") { + if len(strings.Trim(s, " ")) == 0 { + continue + } v += "
  • " + strings.Trim(s, " ") + "
  • " } return template.HTML("") } else if c.Type == OrderedListContentType { v := "" for _, s := range strings.Split(c.Content, "--") { + if len(strings.Trim(s, " ")) == 0 { + continue + } v += "
  • " + strings.Trim(s, " ") + "
  • " } return template.HTML("
      " + v + "
    ") -- cgit v1.2.3 From a5955c32fb3b22e37769df49196ddab7b7d53b7d Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 10 Dec 2024 19:55:05 +0100 Subject: feat(custom): support links --- data.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'data.go') diff --git a/data.go b/data.go index ad6fcea..ad51683 100644 --- a/data.go +++ b/data.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "github.com/anhgelus/golatt" "html/template" "log/slog" @@ -16,6 +17,7 @@ const ( ParagraphContentType = "paragraph" ListContentType = "list" OrderedListContentType = "ordered_list" + ButtonsContentType = "links" ) type ConfigData interface { @@ -172,7 +174,15 @@ func (p *CustomPage) GetBackground() template.CSS { return p.Color.GetBackground() } -func (c *CustomContent) Get() template.HTML { +func (p *CustomPage) GetContent() template.HTML { + var res template.HTML + for _, c := range p.Content { + res += c.Get(p) + } + return res +} + +func (c *CustomContent) Get(p *CustomPage) template.HTML { if c.Type == TitleContentType { return template.HTML("

    " + c.Content + "

    ") } else if c.Type == SubtitleContentType { @@ -191,12 +201,36 @@ func (c *CustomContent) Get() template.HTML { } else if c.Type == OrderedListContentType { v := "" for _, s := range strings.Split(c.Content, "--") { - if len(strings.Trim(s, " ")) == 0 { + if len(strings.TrimSpace(s)) == 0 { continue } - v += "
  • " + strings.Trim(s, " ") + "
  • " + v += "
  • " + strings.TrimSpace(s) + "
  • " } return template.HTML("
      " + v + "
    ") + } else if c.Type == ButtonsContentType { + // [Bonsoir](/hello) -- [Bonjour](/not_hello) + v := "" + for _, s := range strings.Split(c.Content, "--") { + if len(strings.TrimSpace(s)) == 0 { + continue + } + sp := strings.Split(s, "](") + if len(sp) != 2 { + slog.Warn("Invalid button", "s", s) + continue + } + url := strings.TrimSpace(sp[1]) + v += fmt.Sprintf( + ``, + url[:len(url)-1], + strings.TrimSpace(sp[0])[1:], + ) + } + return template.HTML(fmt.Sprintf( + ``, + p.Color.Button.GetBackground()+p.Color.Button.GetTextColor(), + v, + )) } slog.Warn("Unknown type", "type", c.Type, "value", c.Content) return "" -- cgit v1.2.3