aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--custom_page.schema.json17
-rw-r--r--data.go121
-rw-r--r--templates/base/base.gohtml5
-rw-r--r--test.html17
4 files changed, 59 insertions, 101 deletions
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()
}
-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"`
+func (c *Config) IsCustomPage() bool {
+ return false
}
-type CustomContent struct {
- Type string `json:"type" toml:"type"`
- Content string `json:"content" toml:"content"`
+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 Content interface {
- Get() template.HTML
+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 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 (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
- }
- 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, "--") {
- 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:],
- )
+func (p *CustomPage) IsCustomPage() bool {
+ return true
+}
+
+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(fmt.Sprintf(
- `<nav class="links" style="%s">%s</nav>`,
- 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>{{ .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">
diff --git a/test.html b/test.html
new file mode 100644
index 0000000..616a510
--- /dev/null
+++ b/test.html
@@ -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> \ No newline at end of file