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
|
diff --git a/blog.go b/blog.go
index c5811ba335cd3b8f28175fe9dacb5b133dd36c0c..5f29fbbd4e2e070101295601d62d5c76e0a1b995 100644
--- a/blog.go
+++ b/blog.go
@@ -4,7 +4,12 @@ import (
"errors"
"fmt"
"html/template"
+ "io/fs"
+ "net/url"
+ "os"
"time"
+
+ "github.com/bmatcuk/doublestar/v4"
)
// Blog is a collection of [Article]
@@ -16,23 +21,66 @@ }
// Article is a blog post/article
type Article struct {
+ Path string
Title string
Subtitle string
Summary string
+ Content string
Time time.Time
- Authors []string
+ Authors []Author
Tags []string
}
+// Author is an author of a blog post
+type Author struct {
+ Name string
+ Job string
+ Email string
+ Links []Link
+}
+
+// Link is a link name and URL
+type Link struct {
+ Name string
+ URL *url.URL
+}
+
func NewBlog(dir string) (*Blog, error) {
- tmpl, err := parseTemplates()
+ tmpl, err := parseTemplates(os.DirFS(dir))
if err != nil {
return nil, fmt.Errorf("could not parse templates in %q: %w", dir, err)
}
- if tmpl.Lookup("index") == nil {
+ index := tmpl.Lookup("index")
+ if index == nil {
return nil, errors.New("`index` template is required but was not found")
}
- if tmpl.Lookup("article") == nil {
+ article := tmpl.Lookup("article")
+ if article == nil {
return nil, errors.New("`article` template is required but was not found")
+ }
+ return &Blog{
+ indexTemplate: index,
+ articleTemplate: article,
+ }, nil
+}
+
+func parseTemplates(fs fs.FS) (*template.Template, error) {
+ matches, err := doublestar.Glob(fs, "**/*.{tmpl,gohtml}")
+ if err != nil {
+ return nil, fmt.Errorf("could not glob templates: %w", err)
+ }
+ tmpl, err := template.New("").ParseFiles(matches...)
+ if err != nil {
+ return nil, fmt.Errorf("could not parse templates: %w", err)
+ }
+ return tmpl, nil
+}
+
+func parseArticles(fs fs.FS) ([]Article, error) {
+ matches, err := doublestar.Glob(fs, "**/*.md")
+ if err != nil {
+ return nil, fmt.Errorf("could not glob articles: %w", err)
+ }
+ for _, match := range matches {
}
}
|