all repos — homestead @ e4e8173ab6124daa84ef012e73c111f111d4a8cf

Code for my website

refactor: make templates take a more specific struct

Alan Pearce
commit

e4e8173ab6124daa84ef012e73c111f111d4a8cf

parent

a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

M internal/builder/builder.gointernal/builder/builder.go
@@ -68,6 +68,12 @@ buf := new(buffer.Buffer)
joinSource := joinSourcePath(options.Source) storage := options.Storage postDir := "post" + siteSettings := templates.SiteSettings{ + Title: config.Title, + DefaultLanguage: config.DefaultLanguage, + Menu: config.Menus["main"], + InjectLiveReload: config.InjectLiveReload, + } log.Debug("reading posts", "source", options.Source) cc, err := content.NewContentCollection(&content.Config{
@@ -89,7 +95,7 @@ for _, post := range cc.Posts {
log.Debug("rendering post", "post", post.Basename) sitemap.AddPath(post.URL, post.Date) buf.Reset() - if err := templates.PostPage(config, post).Render(ctx, buf); err != nil { + if err := templates.PostPage(siteSettings, post).Render(ctx, buf); err != nil { return errors.WithMessage(err, "could not render post") }
@@ -100,7 +106,7 @@ }
log.Debug("rendering tags list") buf.Reset() - if err := templates.TagsPage(config, "tags", mapset.Sorted(cc.Tags), "/tags").Render(ctx, buf); err != nil { + if err := templates.TagsPage(siteSettings, "tags", mapset.Sorted(cc.Tags), "/tags").Render(ctx, buf); err != nil { return errors.WithStack(err) } if err := storage.Write("/tags/", "Tags", buf); err != nil {
@@ -118,7 +124,7 @@ }
log.Debug("rendering tags page", "tag", tag) url := path.Join("/tags", tag) + "/" buf.Reset() - if err := templates.TagPage(config, tag, matchingPosts, url).Render(ctx, buf); err != nil { + if err := templates.TagPage(siteSettings, tag, matchingPosts, url).Render(ctx, buf); err != nil { return errors.WithStack(err) } if err = storage.Write(url, tag, buf); err != nil {
@@ -148,7 +154,7 @@ }
log.Debug("rendering list page") buf.Reset() - if err := templates.ListPage(config, cc.Posts, path.Join("/", postDir)).Render(ctx, buf); err != nil { + if err := templates.ListPage(siteSettings, cc.Posts, path.Join("/", postDir)).Render(ctx, buf); err != nil { return errors.WithStack(err) } if err := storage.Write(path.Join("/", postDir)+"/", "Posts", buf); err != nil {
@@ -184,11 +190,15 @@ for _, post := range cc.Pages {
buf.Reset() log.Debug("rendering page", "source", post.Input, "path", post.URL) if post.URL == "/" { - if err := templates.Homepage(config, cc.Posts, post).Render(ctx, buf); err != nil { + if err := templates.Homepage(siteSettings, templates.HomepageVars{ + Email: config.Email, + Me: config.Menus["me"], + Posts: cc.Posts, + }, post).Render(ctx, buf); err != nil { return errors.WithStack(err) } } else { - if err := templates.Page(config, post).Render(ctx, buf); err != nil { + if err := templates.Page(siteSettings, post).Render(ctx, buf); err != nil { return errors.WithStack(err) } }
M internal/website/mux.gointernal/website/mux.go
@@ -32,7 +32,7 @@
func (website *Website) ErrorHandler(err *ihttp.Error, w http.ResponseWriter, r *http.Request) { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - err := templates.Error(website.config, r.URL.Path, err).Render(r.Context(), w) + err := templates.Error(*website.siteSettings, r.URL.Path, err).Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }
M internal/website/website.gointernal/website/website.go
@@ -18,6 +18,7 @@ "go.alanpearce.eu/homestead/internal/stats/goatcounter"
"go.alanpearce.eu/homestead/internal/stats/nullcounter" "go.alanpearce.eu/homestead/internal/storage" "go.alanpearce.eu/homestead/internal/storage/sqlite" + "go.alanpearce.eu/homestead/templates" "go.alanpearce.eu/x/log" "github.com/benpate/digit"
@@ -39,6 +40,7 @@ }
type Website struct { config *config.Config + siteSettings *templates.SiteSettings counter stats.Counter log *log.Logger reader storage.Reader
@@ -100,6 +102,12 @@ if err != nil {
log.Panic("could not load configuration", "error", err) } website.config = cfg + website.siteSettings = &templates.SiteSettings{ + Title: cfg.Title, + DefaultLanguage: cfg.DefaultLanguage, + Menu: cfg.Menus["main"], + InjectLiveReload: false, + } if opts.Development { cfg.CSP.ScriptSrc = slices.Insert(cfg.CSP.ScriptSrc, 0, "'unsafe-inline'")
M templates/error.templtemplates/error.templ
@@ -1,13 +1,12 @@
package templates import ( - "go.alanpearce.eu/homestead/internal/config" "go.alanpearce.eu/homestead/internal/http" "strconv" ) -templ Error(config *config.Config, path string, err *http.Error) { - @Layout(config, PageSettings{ +templ Error(site SiteSettings, path string, err *http.Error) { + @Layout(site, PageSettings{ Title: "Error", Path: path, }) {
M templates/homepage.templtemplates/homepage.templ
@@ -5,9 +5,15 @@ "go.alanpearce.eu/homestead/internal/config"
"go.alanpearce.eu/homestead/internal/content" ) -templ Homepage(config *config.Config, posts []*content.Post, content templ.Component) { - @Layout(config, PageSettings{ - Title: config.Title, +type HomepageVars struct { + Email string + Me []config.MenuItem + Posts []*content.Post +} + +templ Homepage(site SiteSettings, vars HomepageVars, content templ.Component) { + @Layout(site, PageSettings{ + Title: site.Title, TitleAttrs: templ.Attributes{ "class": "p-name u-url", },
@@ -21,17 +27,17 @@ @content
</div> <section> <h2>Latest Posts</h2> - @list(posts[0:5]) + @list(vars.Posts[0:5]) </section> <section> <h2>Elsewhere on the Internet</h2> <ul class="elsewhere"> <li> - <a class="u-email" rel="me" href={ templ.SafeURL("mailto:" + config.Email) }> - { config.Email } + <a class="u-email" rel="me" href={ templ.SafeURL("mailto:" + vars.Email) }> + { vars.Email } </a> </li> - for _, link := range config.Menus["me"] { + for _, link := range vars.Me { <li> <a class="u-url" rel="me" href={ templ.SafeURL(link.URL.String()) }>{ link.Name }</a> </li>
M templates/layout.templtemplates/layout.templ
@@ -7,6 +7,13 @@ "io"
"net/url" ) +type SiteSettings struct { + Title string + DefaultLanguage string + Menu []config.MenuItem + InjectLiveReload bool +} + type PageSettings struct { Title string Path string
@@ -31,7 +38,7 @@ }
>{ item.Name }</a> } -templ Layout(site *config.Config, page PageSettings) { +templ Layout(site SiteSettings, page PageSettings) { <!DOCTYPE html> <html lang={ site.DefaultLanguage }> <head>
@@ -48,7 +55,7 @@ <h2>
<a href="/" class={ extendClasses("title p-name", page.TitleAttrs) } { page.TitleAttrs... }>{ site.Title }</a> </h2> <nav> - for _, item := range site.Menus["main"] { + for _, item := range site.Menu { @menuItem(item) } </nav>
M templates/list.templtemplates/list.templ
@@ -1,12 +1,9 @@
package templates -import ( - "go.alanpearce.eu/homestead/internal/config" - "go.alanpearce.eu/homestead/internal/content" -) +import "go.alanpearce.eu/homestead/internal/content" -templ TagPage(config *config.Config, tag string, posts []*content.Post, path string) { - @Layout(config, PageSettings{ +templ TagPage(site SiteSettings, tag string, posts []*content.Post, path string) { + @Layout(site, PageSettings{ Title: tag, Path: path, TitleAttrs: templ.Attributes{
@@ -24,9 +21,9 @@ @list(posts)
} } -templ ListPage(config *config.Config, posts []*content.Post, path string) { - @Layout(config, PageSettings{ - Title: config.Title, +templ ListPage(site SiteSettings, posts []*content.Post, path string) { + @Layout(site, PageSettings{ + Title: site.Title, TitleAttrs: templ.Attributes{ "class": "p-author h-card", "rel": "author",
M templates/page.templtemplates/page.templ
@@ -1,12 +1,9 @@
package templates -import ( - "go.alanpearce.eu/homestead/internal/config" - "go.alanpearce.eu/homestead/internal/content" -) +import "go.alanpearce.eu/homestead/internal/content" -templ Page(config *config.Config, page *content.Post) { - @Layout(config, PageSettings{ +templ Page(site SiteSettings, page *content.Post) { + @Layout(site, PageSettings{ Title: page.Title, TitleAttrs: templ.Attributes{ "class": "h-card",
M templates/post.templtemplates/post.templ
@@ -5,7 +5,6 @@ "context"
"io" "time" - "go.alanpearce.eu/homestead/internal/config" "go.alanpearce.eu/homestead/internal/content" )
@@ -22,8 +21,8 @@ { d.Format("2006-01-02") }
</time> } -templ PostPage(config *config.Config, post *content.Post) { - @Layout(config, PageSettings{ +templ PostPage(site SiteSettings, post *content.Post) { + @Layout(site, PageSettings{ Title: post.Title, TitleAttrs: templ.Attributes{ "class": "p-author h-card",
M templates/tags.templtemplates/tags.templ
@@ -1,13 +1,11 @@
package templates -import "go.alanpearce.eu/homestead/internal/config" - templ tagLink(tag string, attrs templ.Attributes) { <a { attrs... } href={ templ.SafeURL("/tags/" + tag + "/") }>#{ tag }</a> } -templ TagsPage(config *config.Config, title string, tags []string, path string) { - @Layout(config, PageSettings{ +templ TagsPage(site SiteSettings, title string, tags []string, path string) { + @Layout(site, PageSettings{ Title: title, Path: path, }) {