diff --git a/blog.go b/blog.go index 5f29fbbd4e2e070101295601d62d5c76e0a1b995..c5811ba335cd3b8f28175fe9dacb5b133dd36c0c 100644 --- a/blog.go +++ b/blog.go @@ -4,12 +4,7 @@ import ( "errors" "fmt" "html/template" - "io/fs" - "net/url" - "os" "time" - - "github.com/bmatcuk/doublestar/v4" ) // Blog is a collection of [Article] @@ -22,77 +17,30 @@ // Article is a blog post/article type Article struct { -package blog - Subtitle string Summary string - "fmt" + "html/template" - "html/template" - Authors []Author - Tags []string -} - -// Author is an author of a blog post -type Author struct { - Name string - Job string - Email string -import ( "errors" -} -// Link is a link name and URL -type Link struct { - Name string -import ( "net/url" } func NewBlog(dir string) (*Blog, error) { - tmpl, err := parseTemplates(os.DirFS(dir)) + tmpl, err := parseTemplates() if err != nil { return nil, fmt.Errorf("could not parse templates in %q: %w", dir, err) } - index := tmpl.Lookup("index") - if index == nil { - "errors" "html/template" - } - article := tmpl.Lookup("article") - if article == nil { + "html/template" "errors" - "os" + "html/template" } - 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 { - "fmt" "html/template" - } - "fmt" "io/fs" "errors" -package blog - return nil, fmt.Errorf("could not parse templates: %w", err) - } - "fmt" "os" -} - -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 { } } diff --git a/template.go b/template.go new file mode 100644 index 0000000000000000000000000000000000000000..a23ac53526bd6dc9b8cc9fc8388bc67dd40b8348 --- /dev/null +++ b/template.go @@ -0,0 +1,21 @@ +package blog + +import ( + "fmt" + "html/template" + "io/fs" + + "github.com/bmatcuk/doublestar/v4" +) + +func parseTemplates(fs fs.FS) (*template.Template, error) { + matches, err := doublestar.Glob(fs, "**/*.html") + 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 +}