diff options
author | Alan Pearce | 2024-06-18 16:46:22 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-18 16:46:22 +0200 |
commit | 1d247493e05cdc659e46cd3d8a01d5da1e893867 (patch) | |
tree | 221e9ee2f5e3f171dfd937f04fae7ad6a33588d8 /internal/builder/builder.go | |
parent | a238c7e0889cbe7dfaa1a700dea30686a4e2139a (diff) | |
download | website-1d247493e05cdc659e46cd3d8a01d5da1e893867.tar.lz website-1d247493e05cdc659e46cd3d8a01d5da1e893867.tar.zst website-1d247493e05cdc659e46cd3d8a01d5da1e893867.zip |
switch to templ for rendering HTML templates
Diffstat (limited to 'internal/builder/builder.go')
-rw-r--r-- | internal/builder/builder.go | 62 |
1 files changed, 27 insertions, 35 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go index bb6f40d..7b6af0b 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -1,18 +1,20 @@ package builder import ( + "context" "fmt" "io" "net/url" "os" "path" "slices" - "sync" "time" "website/internal/config" "website/internal/log" + "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" @@ -36,7 +38,7 @@ func mkdirp(dirs ...string) error { } func outputToFile(output io.Reader, filename ...string) error { - log.Debug("outputting file", "filename", path.Join(filename...)) + // log.Debug("outputting file", "filename", path.Join(filename...)) file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return errors.WithMessage(err, "could not open output file") @@ -50,8 +52,23 @@ func outputToFile(output io.Reader, filename ...string) error { return nil } +func renderToFile(component templ.Component, filename ...string) error { + // log.Debug("outputting file", "filename", path.Join(filename...)) + file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return errors.WithMessage(err, "could not open output file") + } + defer file.Close() + + if err := component.Render(context.TODO(), file); err != nil { + return errors.WithMessage(err, "could not write output file") + } + + return nil +} + func writerToFile(writer io.WriterTo, filename ...string) error { - log.Debug("outputting file", "filename", path.Join(filename...)) + // log.Debug("outputting file", "filename", path.Join(filename...)) file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return errors.WithMessage(err, "could not open output file") @@ -70,7 +87,6 @@ func build(outDir string, config config.Config) (*Result, error) { r := &Result{ Hashes: make([]string, 0), } - assetsOnce = sync.Once{} privateDir := path.Join(outDir, "private") if err := mkdirp(privateDir); err != nil { return nil, errors.WithMessage(err, "could not create private directory") @@ -112,11 +128,7 @@ func build(outDir string, config config.Config) (*Result, error) { Loc: post.URL, LastMod: &post.Date, }) - output, err := renderPost(post, config) - if err != nil { - return nil, errors.WithMessagef(err, "could not render post %s", post.Input) - } - if err := outputToFile(output, post.Output); err != nil { + if err := renderToFile(postPage(config, post), post.Output); err != nil { return nil, err } } @@ -125,11 +137,7 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create directory for tags") } log.Debug("rendering tags list") - output, err := renderTags(tags, config, "/tags") - if err != nil { - return nil, errors.WithMessage(err, "could not render tags") - } - if err := outputToFile(output, publicDir, "tags", "index.html"); err != nil { + if err := renderToFile(tagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil { return nil, err } sm.Add(&sitemap.URL{ @@ -149,11 +157,7 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag - output, err := renderListPage(tag, config, matchingPosts, url) - if err != nil { - return nil, errors.WithMessage(err, "could not render tag page") - } - if err := outputToFile(output, publicDir, "tags", tag, "index.html"); err != nil { + if err := renderToFile(tagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil { return nil, err } sm.Add(&sitemap.URL{ @@ -177,11 +181,7 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering list page") - listPage, err := renderListPage("", config, posts, "/post") - if err != nil { - return nil, errors.WithMessage(err, "could not render list page") - } - if err := outputToFile(listPage, publicDir, "post", "index.html"); err != nil { + if err := renderToFile(listPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil { return nil, err } sm.Add(&sitemap.URL{ @@ -217,11 +217,7 @@ func build(outDir string, config config.Config) (*Result, error) { r.Hashes = append(r.Hashes, h) log.Debug("rendering homepage") - homePage, err := renderHomepage(config, posts, "/") - if err != nil { - return nil, errors.WithMessage(err, "could not render homepage") - } - if err := outputToFile(homePage, publicDir, "index.html"); err != nil { + if err := renderToFile(homepage(config, posts), publicDir, "index.html"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post @@ -230,15 +226,11 @@ func build(outDir string, config config.Config) (*Result, error) { sm.Add(&sitemap.URL{ Loc: "/", }) - h, err = getHTMLStyleHash(publicDir, "index.html") + h, _ = getHTMLStyleHash(publicDir, "index.html") r.Hashes = append(r.Hashes, h) log.Debug("rendering 404 page") - notFound, err := render404(config, "/404.html") - if err != nil { - return nil, errors.WithMessage(err, "could not render 404 page") - } - if err := outputToFile(notFound, publicDir, "404.html"); err != nil { + if err := renderToFile(notFound(config, "/404.html"), publicDir, "404.html"); err != nil { return nil, err } |