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/page.templ | |
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/page.templ')
-rw-r--r-- | internal/builder/page.templ | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/builder/page.templ b/internal/builder/page.templ new file mode 100644 index 0000000..c99e315 --- /dev/null +++ b/internal/builder/page.templ @@ -0,0 +1,84 @@ +package builder + +import ( + "net/url" + "website/internal/config" +) + +type PageSettings struct { + Title string + Path string + TitleAttrs templ.Attributes + BodyAttrs templ.Attributes +} + +func extendClasses(cs string, attrs templ.Attributes) string { + if extras, exists := attrs["class"]; exists { + return templ.Classes(cs, extras).String() + } else { + return cs + } +} + +templ page(site config.Config, page PageSettings) { + <!DOCTYPE html> + <html lang={ site.DefaultLanguage }> + <head> + <meta charset="utf-8"/> + <meta name="viewport" content="width=device-width, initial-scale=1.0"/> + <title>{ page.Title }</title> + <link rel="alternate" type="application/atom+xml" title={ site.Title } href="/atom.xml"/> + @style(css) + </head> + <body { page.BodyAttrs... }> + <a class="skip" href="#main">Skip to main content</a> + <header> + <h2> + <a href="/" class={ extendClasses("title p-name", page.TitleAttrs) } { page.TitleAttrs... }>{ site.Title }</a> + </h2> + <nav> + for _, item := range site.Menus["main"] { + <a href={ templ.SafeURL(item.URL) }>{ item.Name }</a> + } + </nav> + </header> + <main id="main"> + { children... } + </main> + <footer> + Content is + <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>. + <a href="https://git.alanpearce.eu/website/">Site source code</a> is + <a href="https://opensource.org/licenses/MIT">MIT</a> + </footer> + @counter(page.Path, page.Title) + </body> + </html> +} + +func mkURL(path string, title string) string { + u, err := url.Parse("https://alanpearce-eu.goatcounter.com/count") + if err != nil { + panic(err) + } + q := u.Query() + q.Add("p", path) + q.Add("t", title) + u.RawQuery = q.Encode() + + return u.String() +} + +templ counter(path string, title string) { + <script data-goatcounter="https://alanpearce-eu.goatcounter.com/count" async src="https://gc.zgo.at/count.v4.js" crossorigin="anonymous" integrity="sha384-nRw6qfbWyJha9LhsOtSb2YJDyZdKvvCFh0fJYlkquSFjUxp9FVNugbfy8q1jdxI+"></script> + <noscript> + <img src={ string(templ.URL(mkURL(path, title))) }/> + </noscript> +} + +func style(css string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { + _, err = io.WriteString(w, "<style>\n"+css+"\n</style>") + return + }) +} |