aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-05 22:40:28 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-05 22:40:28 +0200
commit33cfc1d4e8f3b5b55202588e5b5148a48b43d427 (patch)
tree8163aebcb5d3844e30b71b84b17934e38f618217 /backend
parente9713ae0fe2fd5f714fc73a46adfb5810899a05c (diff)
refactor(backend): generalize file parsing
Diffstat (limited to 'backend')
-rw-r--r--backend/data.go24
-rw-r--r--backend/logs.go38
-rw-r--r--backend/parser.go43
-rw-r--r--backend/templates/base.html6
-rw-r--r--backend/templates/components.html6
-rw-r--r--backend/templates/home.html2
-rw-r--r--backend/templates/log.html2
7 files changed, 71 insertions, 50 deletions
diff --git a/backend/data.go b/backend/data.go
index d98c8b0..bc3136c 100644
--- a/backend/data.go
+++ b/backend/data.go
@@ -24,16 +24,16 @@ type dataUsable interface {
}
type data struct {
- title string
- Article bool
- Domain string
- URL string
- Image string
- Description string
- Name string
- Links []Link
- Logo *Logo
- Quote string
+ title string
+ Article bool
+ Domain string
+ URL string
+ Image string
+ PageDescription string
+ Name string
+ Links []Link
+ Logo *Logo
+ Quote string
}
func (d *data) SetData(data *data) {
@@ -48,8 +48,8 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string
if d.Name == "" {
d.Name = cfg.Name
}
- if d.Description == "" {
- d.Description = cfg.Description
+ if d.PageDescription == "" {
+ d.PageDescription = cfg.Description
}
if d.Links == nil {
d.Links = cfg.Links
diff --git a/backend/logs.go b/backend/logs.go
index bd93353..46e47c5 100644
--- a/backend/logs.go
+++ b/backend/logs.go
@@ -1,7 +1,6 @@
package backend
import (
- "errors"
"fmt"
"html/template"
"log/slog"
@@ -14,9 +13,7 @@ import (
"sync"
"time"
- "git.anhgelus.world/anhgelus/small-world/markdown"
"github.com/go-chi/chi/v5"
- "github.com/pelletier/go-toml/v2"
)
var (
@@ -25,12 +22,9 @@ var (
type logData struct {
*data
- LogTitle string `toml:"title"`
- Description string `toml:"description"`
- Img image `toml:"image"`
- PubLocalDate toml.LocalDate `toml:"publication_date"`
- Content template.HTML `toml:"-"`
- Slug string `toml:"-"`
+ EntryInfo
+ Content template.HTML `toml:"-"`
+ Slug string `toml:"-"`
}
func (d *logData) SetData(dt *data) {
@@ -146,7 +140,7 @@ func handleLog(w http.ResponseWriter, r *http.Request) {
func parseLog(d *logData, path, slug string) bool {
d.Article = true
- d.LogTitle = slug
+ d.EntryInfo.Title = slug
d.title = slug
d.Slug = slug
b, err := os.ReadFile(path + ".md")
@@ -156,27 +150,13 @@ func parseLog(d *logData, path, slug string) bool {
}
panic(err)
}
- var dd string
- splits := strings.SplitN(string(b), "---", 2)
- if len(splits) == 2 {
- dd = splits[1]
- err = toml.Unmarshal([]byte(splits[0]), &d)
- if err != nil {
- panic(err)
- }
- d.title = d.LogTitle
- d.Image = d.Img.Src
- } else {
- dd = string(b)
- }
- d.Content, err = markdown.Parse(dd, &markdown.Option{ImageSource: getStatic})
- var errMd *markdown.ParseError
- errors.As(err, &errMd)
- if errMd != nil {
- slog.Error("parsing markdown")
- fmt.Println(errMd.Pretty())
+ var ok bool
+ d.Content, ok = parse(b, &d.EntryInfo)
+ if !ok {
return false
}
+ d.title = d.EntryInfo.Title
+ d.Image = d.Img.Src
logs[path] = d
return true
}
diff --git a/backend/parser.go b/backend/parser.go
new file mode 100644
index 0000000..a0cb01f
--- /dev/null
+++ b/backend/parser.go
@@ -0,0 +1,43 @@
+package backend
+
+import (
+ "errors"
+ "fmt"
+ "html/template"
+ "log/slog"
+ "strings"
+
+ "git.anhgelus.world/anhgelus/small-world/markdown"
+ "github.com/pelletier/go-toml/v2"
+)
+
+type EntryInfo struct {
+ Title string `toml:"title"`
+ Description string `toml:"description"`
+ Img image `toml:"image"`
+ PubLocalDate toml.LocalDate `toml:"publication_date"`
+}
+
+func parse(b []byte, info *EntryInfo) (template.HTML, bool) {
+ var dd string
+ splits := strings.SplitN(string(b), "---", 2)
+ if len(splits) == 2 && info != nil {
+ err := toml.Unmarshal([]byte(splits[0]), info)
+ if err != nil {
+ slog.Warn("parsing entry info", "error", err)
+ } else {
+ dd = splits[1]
+ }
+ } else {
+ dd = string(b)
+ }
+ content, err := markdown.Parse(dd, &markdown.Option{ImageSource: getStatic})
+ var errMd *markdown.ParseError
+ errors.As(err, &errMd)
+ if errMd != nil {
+ slog.Error("parsing markdown")
+ fmt.Println(errMd.Pretty())
+ return "", false
+ }
+ return content, true
+}
diff --git a/backend/templates/base.html b/backend/templates/base.html
index 3af37f9..5e6982b 100644
--- a/backend/templates/base.html
+++ b/backend/templates/base.html
@@ -6,13 +6,13 @@
<title>{{ .Title }}</title>
{{ $styles := asset "styles.css" }}<link rel="stylesheet" href="{{ $styles.Src }}" integrity="{{ $styles.Checksum }}">
<link rel="shortcut icon" href="{{ static .Logo.Favicon }}">
- <meta property="description" content="{{ .Description }}" />
+ <meta property="description" content="{{ .PageDescription }}" />
<!-- Open Graph -->
<meta property="og:title" content="{{ .Title }}" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://{{ .Domain }}{{ .URL }}" />
<meta property="og:image" content="{{ fullStatic .Image }}" />
- <meta property="og:description" content="{{ .Description }}" />
+ <meta property="og:description" content="{{ .PageDescription }}" />
<meta property="og:local" content="fr_FR" />
<meta property="og:site_name" content="{{ .Name }}" />
{{ if ne .PubDate "" }}<meta property="article:published_time" content="{{ .PubDate }}">{{ end }}
@@ -21,7 +21,7 @@
<meta property="twitter:domain" content="{{ .Domain }}" />
<meta property="twitter:url" content="https://{{ .Domain }}{{ .URL }}" />
<meta name="twitter:title" content="{{ .Title }}" />
- <meta name="twitter:description" content="{{ .Description }}" />
+ <meta name="twitter:description" content="{{ .PageDescription }}" />
<meta name="twitter:image" content="{{ fullStatic .Image }}" />
</head>
<body hx-push-url="true">
diff --git a/backend/templates/components.html b/backend/templates/components.html
index cb0fb12..280fa3d 100644
--- a/backend/templates/components.html
+++ b/backend/templates/components.html
@@ -2,14 +2,12 @@
<article>
{{ range .Logs }}
<article>
- <h2><a href="/log/{{ .Slug }}">{{ .LogTitle }}</a></h2>
+ <h2><a href="/log/{{ .Slug }}">{{ .Title }}</a></h2>
<figure>
<a href="/log/{{ .Slug }}"><img src="{{ static .Img.Src }}" alt="{{ .Img.Alt }}"></a>
<figcaption>{{ .Img.Legend }}</figcaption>
</figure>
- <p>
- {{ .Description }}
- </p>
+ <p>{{ .Description }}</p>
</article>
{{ end }}
{{ if ne .PagesNumber 1 }}
diff --git a/backend/templates/home.html b/backend/templates/home.html
index de9d2af..c570845 100644
--- a/backend/templates/home.html
+++ b/backend/templates/home.html
@@ -3,7 +3,7 @@
<div class="introduction">
<h1>logs</h1>
<p>
- {{ .Description }}
+ {{ .PageDescription }}
</p>
</div>
{{ template "logs_display" . }}
diff --git a/backend/templates/log.html b/backend/templates/log.html
index f872631..47a5d04 100644
--- a/backend/templates/log.html
+++ b/backend/templates/log.html
@@ -1,6 +1,6 @@
{{define "body"}}
<article id="content">
- <h1>{{ .LogTitle }}</h1>
+ <h1>{{ .Title }}</h1>
<p>
{{ .Description }}
</p>