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
|
package blog
import (
"errors"
"fmt"
"html/template"
"io/fs"
"net/url"
"os"
"time"
"github.com/bmatcuk/doublestar/v4"
)
// Blog is a collection of [Article]
type Blog struct {
indexTemplate *template.Template
articleTemplate *template.Template
Articles []Article
}
// Article is a blog post/article
type Article struct {
Path string
Title string
Subtitle string
Summary string
Content string
Time time.Time
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(os.DirFS(dir))
if err != nil {
return nil, fmt.Errorf("could not parse templates in %q: %w", dir, err)
}
index := tmpl.Lookup("index")
if index == nil {
return nil, errors.New("`index` template is required but was not found")
}
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 {
}
}
|