refactor(pages): use html instead of json to render custom pages
This commit is contained in:
parent
2d74d8afc3
commit
42a96cbb6e
4 changed files with 60 additions and 102 deletions
|
@ -88,22 +88,7 @@
|
|||
]
|
||||
},
|
||||
"content": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"content": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"type",
|
||||
"content"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
111
data.go
111
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,24 +90,32 @@ 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
|
||||
}
|
||||
|
||||
func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
|
||||
if c.CustomPages == nil {
|
||||
println("null")
|
||||
|
@ -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("<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, "--") {
|
||||
if len(strings.Trim(s, " ")) == 0 {
|
||||
continue
|
||||
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
|
||||
}
|
||||
v += "<li>" + strings.Trim(s, " ") + "</li>"
|
||||
res = template.HTML(b)
|
||||
contentsMap[p.URI] = res
|
||||
}
|
||||
return template.HTML("<ul>" + v + "</ul>")
|
||||
} else if c.Type == OrderedListContentType {
|
||||
v := ""
|
||||
for _, s := range strings.Split(c.Content, "--") {
|
||||
if len(strings.TrimSpace(s)) == 0 {
|
||||
continue
|
||||
}
|
||||
v += "<li>" + strings.TrimSpace(s) + "</li>"
|
||||
}
|
||||
return template.HTML("<ol>" + v + "</ol>")
|
||||
} 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(
|
||||
`<div class="link"><a href="%s">%s</a></div>`,
|
||||
url[:len(url)-1],
|
||||
strings.TrimSpace(sp[0])[1:],
|
||||
)
|
||||
}
|
||||
return template.HTML(fmt.Sprintf(
|
||||
`<nav class="links" style="%s">%s</nav>`,
|
||||
p.Color.Button.GetBackground()+p.Color.Button.GetTextColor(),
|
||||
v,
|
||||
))
|
||||
}
|
||||
slog.Warn("Unknown type", "type", c.Type, "value", c.Content)
|
||||
return ""
|
||||
return res, nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,11 @@
|
|||
<title>{{ .Title }}</title>
|
||||
<link rel="stylesheet" href="{{getAssetPath "styles.css"}}" />
|
||||
{{template "opengraph-base" .SEO}}
|
||||
{{ if .Data.IsCustomPage }}
|
||||
<style>
|
||||
.links { {{ .Data.Color.Button.GetBackground }}{{ .Data.Color.Button.GetTextColor }} }
|
||||
</style>
|
||||
{{ end }}
|
||||
</head>
|
||||
<body style="{{ .Data.GetBackgroundImage }}{{ .Data.GetTextColor }}">
|
||||
<div class="center">
|
||||
|
|
17
test.html
Normal file
17
test.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
<h2>Hello</h2>
|
||||
<p>world, what's up?</p>
|
||||
<h3>Sed proin quis cursus do lobortis ultricies viverra tempus et.</h3>
|
||||
<p>Lectus pulvinar lorem mi enim pharetra sed aliqua et cursus. Sit sem ut elit amet labore lectus sed at vulputate.</p>
|
||||
<nav class="links">
|
||||
<div class="link">
|
||||
<a href="/">home sweet home</a>
|
||||
</div>
|
||||
<div class="link">
|
||||
<a href="https://www.steampowered.com">steam</a>
|
||||
</div>
|
||||
</nav>
|
||||
<ul>
|
||||
<li>Hello 1</li>
|
||||
<li>Not hello 2</li>
|
||||
<li>Final element 3</li>
|
||||
</ul>
|
Loading…
Add table
Add a link
Reference in a new issue