From 33cfc1d4e8f3b5b55202588e5b5148a48b43d427 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sun, 5 Oct 2025 22:40:28 +0200 Subject: refactor(backend): generalize file parsing --- backend/data.go | 24 +++++++++++----------- backend/logs.go | 38 ++++++++-------------------------- backend/parser.go | 43 +++++++++++++++++++++++++++++++++++++++ backend/templates/base.html | 6 +++--- backend/templates/components.html | 6 ++---- backend/templates/home.html | 2 +- backend/templates/log.html | 2 +- 7 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 backend/parser.go (limited to 'backend') 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 }} {{ $styles := asset "styles.css" }} - + - + {{ if ne .PubDate "" }}{{ end }} @@ -21,7 +21,7 @@ - + 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 @@
{{ range .Logs }} {{ 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 @@

logs

- {{ .Description }} + {{ .PageDescription }}

{{ 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"}}
-

{{ .LogTitle }}

+

{{ .Title }}

{{ .Description }}

-- cgit v1.2.3