all repos — website @ 1d247493e05cdc659e46cd3d8a01d5da1e893867

My website

switch to templ for rendering HTML templates

Alan Pearce
commit

1d247493e05cdc659e46cd3d8a01d5da1e893867

parent

a238c7e0889cbe7dfaa1a700dea30686a4e2139a

1 file changed, 27 insertions(+), 35 deletions(-)

changed files
M internal/builder/builder.gointernal/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 @@ return errors.Wrap(err, "could not create directory")
} 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 @@
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 @@ log.Debug("output", "dir", outDir)
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 @@ sm.Add(&sitemap.URL{
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 @@ if err := mkdirp(publicDir, "tags"); err != nil {
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 @@ return nil, errors.WithMessage(err, "could not create directory")
} 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 @@ }
} 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 @@ }
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 @@ // without a date, which could be newer
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 }