Home

blog @main - refs - log -
-
https://git.jolheiser.com/blog.git
My nonexistent blog
blog / main.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//go:generate templ generate
//go:generate go run .
package main

import (
	"context"
	"embed"
	"flag"
	"fmt"
	"io"
	iofs "io/fs"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"github.com/alecthomas/chroma/v2/styles"
	"github.com/gorilla/feeds"
)

var (
	//go:embed articles/*
	articleFS embed.FS
	//go:embed static/*
	staticFS embed.FS
	ref      string = "main"
)

func init() {
	if r, ok := os.LookupEnv("BLOG_REF"); ok {
		ref = r
	}
}

func maine() error {
	fs := flag.NewFlagSet("blog", flag.ExitOnError)
	outFlag := fs.String("out", "out", "Output directory")
	fs.StringVar(outFlag, "o", *outFlag, "--out")
	serveFlag := fs.Bool("serve", false, "Serve output dir after generation")
	fs.BoolVar(serveFlag, "s", *serveFlag, "--serve")
	if err := fs.Parse(os.Args[1:]); err != nil {
		return err
	}

	files, err := articleFS.ReadDir("articles")
	if err != nil {
		return err
	}

	categories := make(Articles)
	articles := make([]Article, 0, len(files))
	for _, file := range files {
		if filepath.Ext(file.Name()) != ".md" {
			continue
		}
		path := fmt.Sprintf("articles/%s", file.Name())
		content, err := iofs.ReadFile(articleFS, path)
		if err != nil {
			return err
		}
		article, err := Parse(string(content))
		if err != nil {
			return err
		}
		article.path = path
		categories[article.Category] = append(categories[article.Category], article)
		articles = append(articles, article)
		if err := writeArticle(*outFlag, article); err != nil {
			return err
		}
	}

	if err := writeCSS(*outFlag); err != nil {
		return err
	}

	if err := copyStatic(*outFlag); err != nil {
		return err
	}

	if err := writeRSS(*outFlag, articles); err != nil {
		return err
	}

	fi, err := os.Create(filepath.Join(*outFlag, "index.html"))
	if err != nil {
		return err
	}
	defer fi.Close()

	if err := IndexTemplate(categories).Render(context.Background(), fi); err != nil {
		return err
	}

	if *serveFlag {
		fmt.Println("http://localhost:8080")
		http.Handle("/", http.FileServer(http.Dir("out")))
		http.ListenAndServe(":8080", nil)
	}

	return nil
}

func writeArticle(out string, article Article) error {
	dest := filepath.Join(out, article.Slug(), "index.html")
	if err := os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
		return err
	}

	fi, err := os.Create(dest)
	if err != nil {
		return err
	}
	defer fi.Close()

	return ArticleTemplate(article).Render(context.Background(), fi)
}

func writeRSS(out string, articles []Article) error {
	fi, err := os.Create(filepath.Join(out, "feed.atom"))
	if err != nil {
		return err
	}
	defer fi.Close()

	feed := &feeds.Feed{
		Title:       "jolheiser",
		Link:        &feeds.Link{Href: "https://blog.jolheiser.com/"},
		Description: "jolheiser's blog log in a blog bog",
		Author: &feeds.Author{
			Name:  "John Olheiser",
			Email: "john@jolheiser.com",
		},
		Updated: time.Now(),
		Created: time.Date(2024, time.February, 25, 0, 0, 0, 0, time.Local),
		// Image:       &feeds.Image{},
	}
	for _, article := range articles {
		if article.Draft {
			continue
		}
		url := fmt.Sprintf("https://blog.jolheiser.com/%s", article.Slug())
		feed.Items = append(feed.Items, &feeds.Item{
			Title:       article.Title,
			Link:        &feeds.Link{Href: url},
			Description: article.Summary,
			Id:          url,
			Created:     article.Date,
			// Content:     article.Content,
		})
	}

	return feed.WriteAtom(fi)
}

func writeCSS(out string) error {
	fi, err := os.Create(filepath.Join(out, "chroma.css"))
	if err != nil {
		return err
	}
	defer fi.Close()

	if err := CSS.WriteCSS(fi, styles.Get("catppuccin-latte")); err != nil {
		return err
	}
	fi.WriteString("@media (prefers-color-scheme: dark) {")
	if err := CSS.WriteCSS(fi, styles.Get("catppuccin-mocha")); err != nil {
		return err
	}
	fi.WriteString("}")
	return nil
}

func copyStatic(out string) error {
	files, err := staticFS.ReadDir("static")
	if err != nil {
		return err
	}
	for _, file := range files {
		if err := func() error {
			dstFi, err := os.Create(filepath.Join(out, file.Name()))
			if err != nil {
				return err
			}
			defer dstFi.Close()

			srcFi, err := staticFS.Open("static/" + file.Name())
			if err != nil {
				return err
			}
			defer srcFi.Close()

			if _, err := io.Copy(dstFi, srcFi); err != nil {
				return err
			}

			return nil
		}(); err != nil {
			return err
		}
	}
	return nil
}

func copyFile(dst, src string) error {
	srcFi, err := os.Open(src)
	if err != nil {
		return err
	}
	defer srcFi.Close()

	dstFi, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer dstFi.Close()

	if _, err := io.Copy(dstFi, srcFi); err != nil {
		return err
	}

	return nil
}

func main() {
	if err := maine(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}