-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
358 lines (297 loc) · 9.02 KB
/
build.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/bitfield/script"
"github.com/gorilla/feeds"
"github.com/otiai10/copy"
lo "github.com/samber/lo"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/hashtag"
)
type BuildCmd struct {
Dev bool `help:"Run in dev mode, which compiles scratch file and drafts" default:"false"`
Rebuild bool `help:"Rebuild the entire archive" default:"false"`
md goldmark.Markdown
}
func (b *BuildCmd) Run() error {
b.md = goldmark.New(
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithExtensions(
&hashtag.Extender{Variant: hashtag.ObsidianVariant},
&ObsidianImageExtender{ImagePath: "/images/"},
&ObsidianEmbedExtender{},
),
)
return b.generateSite()
}
// Page represents the structure of a web page.
type Page struct {
Title string
Body template.HTML
}
// Note represents a single daily note.
type Note struct {
Body template.HTML
Date string
}
// GroupedNotes represents a list of notes grouped by month
type GroupedNotes struct {
Month string
Notes []*Note
}
func (b *BuildCmd) generateSite() error {
if err := os.MkdirAll("build/images", 0755); err != nil {
return fmt.Errorf("create build directory: %w", err)
}
if err := copy.Copy("static", "build"); err != nil {
return fmt.Errorf("copy static files: %w", err)
}
if err := copy.Copy("content/daily-notes/attachments", "build/images"); err != nil {
return fmt.Errorf("copy attachments: %w", err)
}
tmpl, err := template.ParseGlob("templates/*.html")
if err != nil {
return fmt.Errorf("parse shell template: %w", err)
}
if err := generateIndexPage(tmpl); err != nil {
return fmt.Errorf("generate index page: %w", err)
}
notes, err := getAllNotes(b.md)
if err != nil {
return fmt.Errorf("get all notes: %w", err)
}
if err := generateDailyNotesPage(tmpl, lo.Slice(notes, 0, 31)); err != nil {
return fmt.Errorf("generate daily notes page: %w", err)
}
grouped := groupAllNotes(notes)
if err := generateDailyNotesArchive(tmpl, grouped, b.Rebuild); err != nil {
return fmt.Errorf("generate daily notes archive page: %w", err)
}
if err := generateDailyNotesArchiveIndex(tmpl, grouped); err != nil {
return fmt.Errorf("generate daily notes archive page: %w", err)
}
if b.Dev {
fmt.Println("generating scratch page")
if err := generateScratchPage(b.md, tmpl); err != nil {
return fmt.Errorf("generate scratch page: %w", err)
}
}
return nil
}
func generateIndexPage(tmpl *template.Template) error {
content, err := os.ReadFile("content/index.html")
if err != nil {
return fmt.Errorf("read index content: %w", err)
}
page := Page{
Title: "Deepak Jois",
Body: template.HTML(content),
}
return renderPage(tmpl, "build/index.html", page)
}
func getAllNotes(md goldmark.Markdown) (notes []*Note, err error) {
files, err := script.ListFiles("content/daily-notes/*.md").Slice()
if err != nil {
return nil, fmt.Errorf("list daily notes: %w", err)
}
sort.Sort(sort.Reverse(sort.StringSlice(files)))
for _, file := range files {
var buf bytes.Buffer
if err := convertMarkdownToHTML(md, file, &buf); err != nil {
return nil, fmt.Errorf("convert note %s: %w", file, err)
}
date := strings.TrimSuffix(filepath.Base(file), ".md")
notes = append(notes, &Note{Body: template.HTML(buf.String()), Date: date})
}
return notes, nil
}
// Group and sort notes by YYYY-MM
func groupAllNotes(notes []*Note) []GroupedNotes {
groups := lo.GroupBy(notes, func(n *Note) string {
return n.Date[0:7]
})
months := lo.MapToSlice(groups, func(key string, value []*Note) GroupedNotes {
return GroupedNotes{
Month: key,
Notes: value,
}
})
sort.Slice(months, func(i, j int) bool {
return months[i].Month > months[j].Month
})
return months
}
func generateDailyNotesPage(tmpl *template.Template, notes []*Note) error {
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "daily.html", struct{ Notes []*Note }{Notes: lo.Slice(notes, 0, 31)}); err != nil {
return fmt.Errorf("execute daily notes template: %w", err)
}
page := Page{
Title: "Deepak Jois · Daily Notes",
Body: template.HTML(buf.String()),
}
if err := renderPage(tmpl, "build/daily", page); err != nil {
return err
}
return renderDailyNotesFeed(notes)
}
func generateDailyNotesArchive(tmpl *template.Template, grouped []GroupedNotes, rebuild bool) error {
// prune to last two months unless rebuild is explicitly requested
if !rebuild {
grouped = lo.Slice(grouped, 0, 2)
}
for _, month := range grouped {
// Format month
t, _ := time.Parse("2006-01", month.Month)
s := t.Format("Jan 2006")
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "daily-archive.html", GroupedNotes{
Notes: month.Notes,
Month: s,
}); err != nil {
return fmt.Errorf("execute daily notes archive template: %w", err)
}
page := Page{
Title: "Deepak Jois · Daily Notes " + s,
Body: template.HTML(buf.String()),
}
if err := renderPage(tmpl, fmt.Sprintf("build/daily-archive-%s", month.Month), page); err != nil {
return err
}
}
return nil
}
func generateDailyNotesArchiveIndex(tmpl *template.Template, grouped []GroupedNotes) error {
type CalEntry struct {
Link bool
Day string
}
type Month struct {
Entries []CalEntry
DisplayName string
Slug string
}
var months []Month
for _, m := range grouped {
t, _ := time.Parse("2006-01", m.Month)
grid := make([]CalEntry, 42)
idx := t.Weekday()
currDay := t
for {
entry := CalEntry{Day: fmt.Sprintf("%02d", currDay.Day())}
// check if there is an entry for the day
entry.Link = lo.ContainsBy(m.Notes, func(n *Note) bool {
return n.Date == fmt.Sprintf("%s-%02d", m.Month, currDay.Day())
})
grid[idx] = entry
idx = idx + 1
currDay = currDay.AddDate(0, 0, 1)
if currDay.Year() > t.Year() || currDay.Month() > t.Month() {
break
}
}
months = append(months, Month{
Entries: grid,
DisplayName: t.Format("Jan 2006"),
Slug: m.Month,
})
}
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "daily-archive-index.html", struct{ Months []Month }{months}); err != nil {
return fmt.Errorf("execute daily notes archive index template: %w", err)
}
page := Page{
Title: "Deepak Jois · Daily Notes Archive Index",
Body: template.HTML(buf.String()),
}
return renderPage(tmpl, "build/daily-archive-index", page)
}
func generateScratchPage(md goldmark.Markdown, tmpl *template.Template) error {
var buf bytes.Buffer
if err := convertMarkdownToHTML(md, "content/scratch.md", &buf); err != nil {
return fmt.Errorf("convert scratch: %w", err)
}
page := Page{
Title: "Scratch",
Body: template.HTML(buf.String()),
}
return renderPage(tmpl, "build/scratch", page)
}
func renderDailyNotesFeed(notes []*Note) error {
feed := &feeds.AtomFeed{
Title: "Deepak Jois · Daily Log",
Subtitle: "Running log of links, code snippets and other miscellany.",
Link: &feeds.AtomLink{Href: "https://www.debugjois.dev/daily"},
Author: &feeds.AtomAuthor{AtomPerson: feeds.AtomPerson{Name: "Deepak Jois", Email: "[email protected]"}},
Logo: "https://www.debugjois.dev/android-chrome-512x512.png",
Icon: "https://www.debugjois.dev/favicon.ico",
Id: "https://www.debugjois.dev/daily",
Updated: time.Now().Format(time.RFC3339),
}
// Load the Asia/Kolkata time zone (IST)
ist, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
return err
}
for _, note := range notes {
if note.Date == time.Now().In(ist).Format("2006-01-02") { // skip today
continue
}
var updated time.Time
if date, err := time.ParseInLocation("2006-01-02", note.Date, ist); err != nil {
return err
} else {
date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, ist)
updated = date.Add(24 * time.Hour)
}
entry := feeds.AtomEntry{
Title: note.Date,
Links: []feeds.AtomLink{
{Href: fmt.Sprintf("https://www.debugjois.dev/daily#%s", note.Date)},
},
Updated: updated.Format(time.RFC3339),
Id: note.Date,
Author: &feeds.AtomAuthor{AtomPerson: feeds.AtomPerson{Name: "Deepak Jois"}},
Content: &feeds.AtomContent{Content: string(note.Body), Type: "html"},
}
feed.Entries = append(feed.Entries, &entry)
}
f, err := os.Create("build/daily.xml")
if err != nil {
return fmt.Errorf("create atom file: %w", err)
}
defer f.Close()
return feeds.WriteXML(feed, f)
}
func convertMarkdownToHTML(md goldmark.Markdown, filename string, w io.Writer) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("read markdown file: %w", err)
}
if err := md.Convert(content, w); err != nil {
return fmt.Errorf("convert markdown to HTML: %w", err)
}
return nil
}
func renderPage(tmpl *template.Template, outputPath string, page Page) error {
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("create output file: %w", err)
}
defer f.Close()
if err := tmpl.ExecuteTemplate(f, "shell.html", page); err != nil {
return fmt.Errorf("execute page template: %w", err)
}
return nil
}