Skip to content

Commit

Permalink
repo: generalize blog by introducing a blog config
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelPour committed Jul 11, 2023
1 parent 26e0f00 commit abd2f88
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
11 changes: 5 additions & 6 deletions cmd/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Blog</title>
<title>{{.Cfg.Title}}</title>
<link rel="icon" href="data:,">
<link rel="alternate" type="application/rss+xml" title="Feed" href="/rss.xml">
<style>
Expand All @@ -13,17 +13,16 @@
</style>
</head>
<body>
<h2>evilcookie</h2>
Hi, I'm Raphael. I write about my software developer journey.
You can find my stuff on <a href='https://github.com/RaphaelPour'>github</a>.
<h2>{{.Cfg.Title}}</h2>
{{.Cfg.Description}}
<ul>
{{range .}}
{{range .Posts}}
<li>
<span class='date'>[{{.CreatedAt}}]</span>
<a href='{{ .Link }}'>{{ .Title }}</a>
</li>
{{else}}<li><strong>no posts</strong></li>{{end}}
</ul>
<a href='rss.xml'>RSS</a>|<a href='impressum.html'>Impressum</a>
{{.Cfg.Footer}}
</body>
</html>
31 changes: 23 additions & 8 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down
39 changes: 39 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit abd2f88

Please sign in to comment.