From b27c96688785372787eb5c3c71a32767fab71ac4 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 18 Jun 2024 20:13:26 +0200 Subject: split content and sitemap code from builder --- internal/builder/builder.go | 35 ++++------- internal/builder/homepage.templ | 5 +- internal/builder/list.templ | 11 ++-- internal/builder/post.templ | 3 +- internal/builder/posts.go | 129 ---------------------------------------- internal/builder/sitemap.go | 29 --------- internal/builder/template.go | 3 +- 7 files changed, 25 insertions(+), 190 deletions(-) delete mode 100644 internal/builder/posts.go delete mode 100644 internal/builder/sitemap.go (limited to 'internal/builder') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 7b6af0b..141bd6f 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -11,13 +11,14 @@ import ( "time" "website/internal/config" + "website/internal/content" "website/internal/log" + "website/internal/sitemap" "github.com/a-h/templ" mapset "github.com/deckarep/golang-set/v2" cp "github.com/otiai10/copy" "github.com/pkg/errors" - "github.com/snabb/sitemap" ) type IOConfig struct { @@ -108,12 +109,12 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create post output directory") } log.Debug("reading posts") - posts, tags, err := readPosts("content", "post", publicDir) + posts, tags, err := content.ReadPosts("content", "post", publicDir) if err != nil { return nil, err } - sm := NewSitemap(config) + sitemap := sitemap.New(config) lastMod := time.Now() if len(posts) > 0 { lastMod = posts[0].Date @@ -124,10 +125,7 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create directory for post") } log.Debug("rendering post", "post", post.Basename) - sm.Add(&sitemap.URL{ - Loc: post.URL, - LastMod: &post.Date, - }) + sitemap.AddPath(post.URL, post.Date) if err := renderToFile(postPage(config, post), post.Output); err != nil { return nil, err } @@ -140,13 +138,10 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(tagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: "/tags/", - LastMod: &lastMod, - }) + sitemap.AddPath("/tags/", lastMod) for _, tag := range tags.ToSlice() { - matchingPosts := []Post{} + matchingPosts := []content.Post{} for _, post := range posts { if slices.Contains(post.Taxonomies.Tags, tag) { matchingPosts = append(matchingPosts, post) @@ -160,10 +155,7 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(tagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: url, - LastMod: &matchingPosts[0].Date, - }) + sitemap.AddPath(url, matchingPosts[0].Date) log.Debug("rendering tags feed", "tag", tag) feed, err := renderFeed( @@ -184,10 +176,7 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(listPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: "/post/", - LastMod: &lastMod, - }) + sitemap.AddPath("/post/", lastMod) log.Debug("rendering feed") feed, err := renderFeed(config.Title, config, posts, "feed") @@ -223,9 +212,7 @@ func build(outDir string, config config.Config) (*Result, error) { // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer - sm.Add(&sitemap.URL{ - Loc: "/", - }) + sitemap.AddPath("/", time.Time{}) h, _ = getHTMLStyleHash(publicDir, "index.html") r.Hashes = append(r.Hashes, h) @@ -235,7 +222,7 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering sitemap") - if err := writerToFile(sm, publicDir, "sitemap.xml"); err != nil { + if err := writerToFile(sitemap, publicDir, "sitemap.xml"); err != nil { return nil, err } diff --git a/internal/builder/homepage.templ b/internal/builder/homepage.templ index 9897b5d..1df401b 100644 --- a/internal/builder/homepage.templ +++ b/internal/builder/homepage.templ @@ -3,11 +3,12 @@ package builder import ( "website/internal/config" "path" + "website/internal/content" ) func getContent(filename string) templ.Component { return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { - _, index, err := getPost(path.Join("content", filename)) + _, index, err := content.GetPost(path.Join("content", filename)) if err != nil { return err } @@ -17,7 +18,7 @@ func getContent(filename string) templ.Component { }) } -templ homepage(config config.Config, posts []Post) { +templ homepage(config config.Config, posts []content.Post) { @page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ diff --git a/internal/builder/list.templ b/internal/builder/list.templ index 48563ed..24cafe8 100644 --- a/internal/builder/list.templ +++ b/internal/builder/list.templ @@ -1,8 +1,11 @@ package builder -import "website/internal/config" +import ( + "website/internal/config" + "website/internal/content" +) -templ tagPage(config config.Config, tag string, posts []Post, path string) { +templ tagPage(config config.Config, tag string, posts []content.Post, path string) { @page(config, PageSettings{ Title: tag, Path: path, @@ -21,7 +24,7 @@ templ tagPage(config config.Config, tag string, posts []Post, path string) { } } -templ listPage(config config.Config, posts []Post, path string) { +templ listPage(config config.Config, posts []content.Post, path string) { @page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ @@ -34,7 +37,7 @@ templ listPage(config config.Config, posts []Post, path string) { } } -templ list(posts []Post) { +templ list(posts []content.Post) {