From 42a96cbb6e81d509d218a36d4cf78fe68742d11e Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 10 Mar 2025 11:14:09 +0100 Subject: [PATCH] refactor(pages): use html instead of json to render custom pages --- custom_page.schema.json | 17 +---- data.go | 123 +++++++++++-------------------------- templates/base/base.gohtml | 5 ++ test.html | 17 +++++ 4 files changed, 60 insertions(+), 102 deletions(-) create mode 100644 test.html diff --git a/custom_page.schema.json b/custom_page.schema.json index c7a1907..adb7fce 100644 --- a/custom_page.schema.json +++ b/custom_page.schema.json @@ -88,22 +88,7 @@ ] }, "content": { - "type": "array", - "items": { - "type": "object", - "properties": { - "type": { - "type": "string" - }, - "content": { - "type": "string" - } - }, - "required": [ - "type", - "content" - ] - } + "type": "string" } }, "required": [ diff --git a/data.go b/data.go index 8c134b1..de58d99 100644 --- a/data.go +++ b/data.go @@ -3,11 +3,9 @@ package main import ( "encoding/json" "errors" - "fmt" "github.com/BurntSushi/toml" "github.com/anhgelus/golatt" "html/template" - "log/slog" "os" "strconv" "strings" @@ -26,6 +24,7 @@ type ConfigData interface { GetTextColor() template.CSS GetBackground() template.CSS GetBackgroundImage() template.CSS + IsCustomPage() bool } type Config struct { @@ -91,22 +90,30 @@ 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.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 []*CustomContent `json:"content" toml:"content"` -} - -type CustomContent struct { - Type string `json:"type" toml:"type"` - Content string `json:"content" toml:"content"` -} - -type Content interface { - Get() template.HTML + 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"` } func (c *Config) LoadCustomPages() ([]*CustomPage, error) { @@ -136,19 +143,6 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) { return pages, nil } -var legalContent template.HTML - -func (c *Config) GetLegal() (template.HTML, error) { - if legalContent == "" { - b, err := os.ReadFile(c.Legal) - if err != nil { - return "", err - } - legalContent = template.HTML(b) - } - return legalContent, nil -} - func (t *Color) GetTextColor() template.CSS { return template.CSS("--text-color: " + t.Text + ";") } @@ -189,64 +183,21 @@ func (p *CustomPage) GetBackground() template.CSS { return p.Color.GetBackground() } -func (p *CustomPage) GetContent() template.HTML { - var res template.HTML - for _, c := range p.Content { - res += c.Get(p) - } - return res +func (p *CustomPage) IsCustomPage() bool { + return true } -func (c *CustomContent) Get(p *CustomPage) 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, "--") { - if len(strings.Trim(s, " ")) == 0 { - continue - } - v += "
  • " + strings.Trim(s, " ") + "
  • " +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.Content) + if err != nil { + return "", err } - return template.HTML("") - } else if c.Type == OrderedListContentType { - v := "" - for _, s := range strings.Split(c.Content, "--") { - if len(strings.TrimSpace(s)) == 0 { - continue - } - 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, - )) + res = template.HTML(b) + contentsMap[p.URI] = res } - slog.Warn("Unknown type", "type", c.Type, "value", c.Content) - return "" + return res, nil } diff --git a/templates/base/base.gohtml b/templates/base/base.gohtml index 70b32af..3b1e37c 100644 --- a/templates/base/base.gohtml +++ b/templates/base/base.gohtml @@ -8,6 +8,11 @@ {{ .Title }} {{template "opengraph-base" .SEO}} + {{ if .Data.IsCustomPage }} + + {{ end }}
    diff --git a/test.html b/test.html new file mode 100644 index 0000000..616a510 --- /dev/null +++ b/test.html @@ -0,0 +1,17 @@ +

    Hello

    +

    world, what's up?

    +

    Sed proin quis cursus do lobortis ultricies viverra tempus et.

    +

    Lectus pulvinar lorem mi enim pharetra sed aliqua et cursus. Sit sem ut elit amet labore lectus sed at vulputate.

    + + \ No newline at end of file