From abd2f880d1bfc8ee7c58a09d99aa4f3317167dff Mon Sep 17 00:00:00 2001 From: Raphael Pour Date: Tue, 11 Jul 2023 22:00:13 +0200 Subject: [PATCH] repo: generalize blog by introducing a blog config --- CHANGELOG.md | 1 + cmd/index.tmpl | 11 +++++------ cmd/render.go | 31 +++++++++++++++++++++++-------- internal/config/config.go | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 internal/config/config.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f71730d..da420a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * limit image width to 80 chars * outsource templates to files and embed them into the binary * repo: make pkgs metadata and highlighter internal (#84) + * repo: generalize blog by introducing a blog config *Not released yet* diff --git a/cmd/index.tmpl b/cmd/index.tmpl index ad84f36..2951f22 100644 --- a/cmd/index.tmpl +++ b/cmd/index.tmpl @@ -2,7 +2,7 @@ - Blog + {{.Cfg.Title}} -

evilcookie

- Hi, I'm Raphael. I write about my software developer journey. - You can find my stuff on github. +

{{.Cfg.Title}}

+ {{.Cfg.Description}} - RSS|Impressum + {{.Cfg.Footer}} diff --git a/cmd/render.go b/cmd/render.go index 909dfe2..327d573 100644 --- a/cmd/render.go +++ b/cmd/render.go @@ -24,10 +24,12 @@ import ( "path/filepath" "regexp" "sort" + plainTemplate "text/template" "time" _ "embed" + "github.com/RaphaelPour/blogctl/internal/config" "github.com/RaphaelPour/blogctl/internal/highlighter" "github.com/RaphaelPour/blogctl/internal/metadata" "github.com/gomarkdown/markdown" @@ -66,12 +68,16 @@ var renderCmd = &cobra.Command{ Short: "Renders blog to static website", Long: "Collects all posts and renders the markdown using the metadata as static website", RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load(BlogPath) + if err != nil { + return err + } feed := &feeds.Feed{ - Title: "evilcookie.de", - Link: &feeds.Link{Href: "https://evilcookie.de"}, - Description: "drunken stack developer", - Author: &feeds.Author{Name: "Raphael Pour"}, + Title: cfg.Title, + Link: &feeds.Link{Href: fmt.Sprintf("https://%s", cfg.Domain)}, + Description: cfg.Description, + Author: &feeds.Author{Name: cfg.Author}, Created: time.Now(), } @@ -222,17 +228,18 @@ var renderCmd = &cobra.Command{ Content: string(post.Rendered), Link: &feeds.Link{ Href: fmt.Sprintf( - "https://evilcookie.de/%s.html", + "https://%s/%s.html", + cfg.Domain, slug(post.Title), ), }, - Author: &feeds.Author{Name: "Raphael Pour"}, + Author: &feeds.Author{Name: cfg.Author}, Created: time.Unix(post.Timestamp, 0), }) } /* Put everything together */ - t, err := template.New("blog").Parse(indexTemplate) + t, err := plainTemplate.New("blog").Parse(indexTemplate) if err != nil { return fmt.Errorf("Error parsing the html template: %s", err) } @@ -244,7 +251,15 @@ var renderCmd = &cobra.Command{ return fmt.Errorf("Error creating index file: %s", err) } - if err := t.Execute(file, publishedPosts); err != nil { + var indexVars = struct { + Posts []Post + Cfg config.Config + }{ + Posts: publishedPosts, + Cfg: *cfg, + } + + if err := t.Execute(file, indexVars); err != nil { return fmt.Errorf("Error rendering posts: %s", err) } diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..505976f --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,39 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" +) + +const ( + CONFIG_FILE = "blog.json" +) + +type Config struct { + Version int `json:"version"` + Domain string `json:"domain"` + Author string `json:"author"` + Title string `json:"title"` + Description string `json:"description"` + Footer string `json:"footer"` +} + +func ConfigPath(postPath string) string { + return filepath.Join(postPath, CONFIG_FILE) +} + +func Load(postPath string) (*Config, error) { + raw, err := os.ReadFile(ConfigPath(postPath)) + if err != nil { + return nil, fmt.Errorf("Error reading config: %s", err) + } + + config := new(Config) + if err := json.Unmarshal(raw, &config); err != nil { + return nil, fmt.Errorf("Error parsing config: %s", err) + } + + return config, nil +}