about summary refs log tree commit diff stats
path: root/internal/builder
diff options
context:
space:
mode:
authorAlan Pearce2024-06-24 21:48:36 +0200
committerAlan Pearce2024-06-24 23:09:15 +0200
commita2feb8c63c80a1f52830f562af2deb2c6065eaae (patch)
tree448ed15c5fb91099ef29528c529a35d6151d98ee /internal/builder
parent8623948592fa14958d340b006653fc57861c4fc4 (diff)
downloadwebsite-a2feb8c63c80a1f52830f562af2deb2c6065eaae.tar.lz
website-a2feb8c63c80a1f52830f562af2deb2c6065eaae.tar.zst
website-a2feb8c63c80a1f52830f562af2deb2c6065eaae.zip
move templ templates into separate package
Diffstat (limited to 'internal/builder')
-rw-r--r--internal/builder/404.templ13
-rw-r--r--internal/builder/builder.go13
-rw-r--r--internal/builder/homepage.templ42
-rw-r--r--internal/builder/list.templ51
-rw-r--r--internal/builder/page.templ101
-rw-r--r--internal/builder/post.templ51
-rw-r--r--internal/builder/tags.templ23
-rw-r--r--internal/builder/template.go12
8 files changed, 13 insertions, 293 deletions
diff --git a/internal/builder/404.templ b/internal/builder/404.templ
deleted file mode 100644
index 049e67d..0000000
--- a/internal/builder/404.templ
+++ /dev/null
@@ -1,13 +0,0 @@
-package builder
-
-import "website/internal/config"
-
-templ notFound(config config.Config, path string) {
-	@page(config, PageSettings{
-		Title: "Not Found",
-		Path:  path,
-	}) {
-		<h1>404</h1>
-		<h2>ʕノ•ᴥ•ʔノ ︵ ┻━┻</h2>
-	}
-}
diff --git a/internal/builder/builder.go b/internal/builder/builder.go
index 20714b9..933679d 100644
--- a/internal/builder/builder.go
+++ b/internal/builder/builder.go
@@ -14,6 +14,7 @@ import (
 	"website/internal/content"
 	"website/internal/log"
 	"website/internal/sitemap"
+	"website/templates"
 
 	"github.com/a-h/templ"
 	mapset "github.com/deckarep/golang-set/v2"
@@ -127,7 +128,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 		}
 		log.Debug("rendering post", "post", post.Basename)
 		sitemap.AddPath(post.URL, post.Date)
-		if err := renderToFile(postPage(config, post), post.Output); err != nil {
+		if err := renderToFile(templates.PostPage(config, post), post.Output); err != nil {
 			return nil, err
 		}
 	}
@@ -136,7 +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")
-	if err := renderToFile(tagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil {
+	if err := renderToFile(templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil {
 		return nil, err
 	}
 	sitemap.AddPath("/tags/", lastMod)
@@ -153,7 +154,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 		}
 		log.Debug("rendering tags page", "tag", tag)
 		url := "/tags/" + tag
-		if err := renderToFile(tagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil {
+		if err := renderToFile(templates.TagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil {
 			return nil, err
 		}
 		sitemap.AddPath(url, matchingPosts[0].Date)
@@ -174,7 +175,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 	}
 
 	log.Debug("rendering list page")
-	if err := renderToFile(listPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil {
+	if err := renderToFile(templates.ListPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil {
 		return nil, err
 	}
 	sitemap.AddPath("/post/", lastMod)
@@ -215,7 +216,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 	if err != nil {
 		return nil, err
 	}
-	if err := renderToFile(homepage(config, posts, content), publicDir, "index.html"); err != nil {
+	if err := renderToFile(templates.Homepage(config, posts, content), publicDir, "index.html"); err != nil {
 		return nil, err
 	}
 	// it would be nice to set LastMod here, but using the latest post
@@ -226,7 +227,7 @@ func build(outDir string, config config.Config) (*Result, error) {
 	r.Hashes = append(r.Hashes, h)
 
 	log.Debug("rendering 404 page")
-	if err := renderToFile(notFound(config, "/404.html"), privateDir, "404.html"); err != nil {
+	if err := renderToFile(templates.NotFound(config, "/404.html"), privateDir, "404.html"); err != nil {
 		return nil, err
 	}
 
diff --git a/internal/builder/homepage.templ b/internal/builder/homepage.templ
deleted file mode 100644
index 848b9ed..0000000
--- a/internal/builder/homepage.templ
+++ /dev/null
@@ -1,42 +0,0 @@
-package builder
-
-import (
-	"website/internal/config"
-	"website/internal/content"
-)
-
-templ homepage(config config.Config, posts []content.Post, content string) {
-	@page(config, PageSettings{
-		Title: config.Title,
-		TitleAttrs: templ.Attributes{
-			"class": "p-name u-url",
-		},
-		Path: "/",
-		BodyAttrs: templ.Attributes{
-			"class": "h-card",
-		},
-	}) {
-		<div id="content">
-			@Unsafe(content)
-		</div>
-		<section>
-			<h2>Latest Posts</h2>
-			@list(posts[0:3])
-		</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>
-				</li>
-				for _, link := range config.Menus["me"] {
-					<li>
-						<a class="u-url" rel="me" href={ templ.SafeURL(link.URL.String()) }>{ link.Name }</a>
-					</li>
-				}
-			</ul>
-		</section>
-	}
-}
diff --git a/internal/builder/list.templ b/internal/builder/list.templ
deleted file mode 100644
index 24cafe8..0000000
--- a/internal/builder/list.templ
+++ /dev/null
@@ -1,51 +0,0 @@
-package builder
-
-import (
-	"website/internal/config"
-	"website/internal/content"
-)
-
-templ tagPage(config config.Config, tag string, posts []content.Post, path string) {
-	@page(config, PageSettings{
-		Title: tag,
-		Path:  path,
-		TitleAttrs: templ.Attributes{
-			"class": "p-author h-card",
-			"rel":   "author",
-		},
-	}) {
-		<div class="filter">
-			<h3 class="filter">#{ tag }</h3>
-			<small>
-				<a href="../">Remove filter</a>
-			</small>
-		</div>
-		@list(posts)
-	}
-}
-
-templ listPage(config config.Config, posts []content.Post, path string) {
-	@page(config, PageSettings{
-		Title: config.Title,
-		TitleAttrs: templ.Attributes{
-			"class": "p-author h-card",
-			"rel":   "author",
-		},
-		Path: path,
-	}) {
-		@list(posts)
-	}
-}
-
-templ list(posts []content.Post) {
-	<ul class="h-feed">
-		for _, post := range posts {
-			<li class="h-entry">
-				<span>
-					@postDate(post.Date)
-				</span>
-				<a class="p-name u-url" href={ templ.SafeURL(post.URL) }>{ post.Title }</a>
-			</li>
-		}
-	</ul>
-}
diff --git a/internal/builder/page.templ b/internal/builder/page.templ
deleted file mode 100644
index 9a2be2f..0000000
--- a/internal/builder/page.templ
+++ /dev/null
@@ -1,101 +0,0 @@
-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 menuItem(item config.MenuItem) {
-	<a
-		href={ templ.SafeURL(item.URL.String()) }
-		if item.URL.IsAbs() {
-			target="_blank"
-		}
-	>{ item.Name }</a>
-}
-
-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"] {
-						@menuItem(item)
-					}
-				</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)
-			if site.InjectLiveReload {
-				<script defer>
-					new EventSource("/_/reload").onmessage = event => {
-						console.log("got message", event)
-						window.location.reload()
-					};
-				</script>
-			}
-		</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
-	})
-}
diff --git a/internal/builder/post.templ b/internal/builder/post.templ
deleted file mode 100644
index 6f12e5c..0000000
--- a/internal/builder/post.templ
+++ /dev/null
@@ -1,51 +0,0 @@
-package builder
-
-import (
-	"time"
-	"website/internal/config"
-	"website/internal/content"
-)
-
-func Unsafe(html string) templ.Component {
-	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
-		_, err = io.WriteString(w, html)
-		return
-	})
-}
-
-templ postDate(d time.Time) {
-	<time class="dt-published" datetime={ d.UTC().Format(time.RFC3339) }>
-		{ d.Format("2006-01-02") }
-	</time>
-}
-
-templ postPage(config config.Config, post content.Post) {
-	@page(config, PageSettings{
-		Title: post.Title,
-		TitleAttrs: templ.Attributes{
-			"class": "p-author h-card",
-			"rel":   "author",
-		},
-		Path: post.URL,
-	}) {
-		<article class="h-entry">
-			<h1 class="p-name">{ post.Title }</h1>
-			<p>
-				@postDate(post.Date)
-			</p>
-			<div class="e-content">
-				@Unsafe(post.Content)
-			</div>
-			<div class="tags">
-				Tags:
-				<ul class="p-categories tags">
-					for _, tag := range post.Taxonomies.Tags {
-						<li>
-							@tagLink(tag, templ.Attributes{"class": "p-category"})
-						</li>
-					}
-				</ul>
-			</div>
-		</article>
-	}
-}
diff --git a/internal/builder/tags.templ b/internal/builder/tags.templ
deleted file mode 100644
index 14abca4..0000000
--- a/internal/builder/tags.templ
+++ /dev/null
@@ -1,23 +0,0 @@
-package builder
-
-import "website/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) {
-	@page(config, PageSettings{
-		Title: title,
-		Path:  path,
-	}) {
-		<h3 class="filter">Tags</h3>
-		<ul class="tags">
-			for _, tag := range tags {
-				<li class="h-feed">
-					@tagLink(tag, templ.Attributes{})
-				</li>
-			}
-		</ul>
-	}
-}
diff --git a/internal/builder/template.go b/internal/builder/template.go
index 53199fc..5ec963a 100644
--- a/internal/builder/template.go
+++ b/internal/builder/template.go
@@ -21,9 +21,9 @@ import (
 )
 
 var (
-	css       string
-	templates = make(map[string]*os.File)
-	nsMap     = map[string]string{
+	css           string
+	templateFiles = make(map[string]*os.File)
+	nsMap         = map[string]string{
 		"xsl":   "http://www.w3.org/1999/XSL/Transform",
 		"atom":  "http://www.w3.org/2005/Atom",
 		"xhtml": "http://www.w3.org/1999/xhtml",
@@ -39,14 +39,14 @@ func loadCSS() {
 }
 
 func loadTemplate(path string) (file *os.File, err error) {
-	if templates[path] == nil {
+	if templateFiles[path] == nil {
 		file, err = os.OpenFile(path, os.O_RDONLY, 0)
 		if err != nil {
 			return nil, errors.Wrapf(err, "could not load template at path %s", path)
 		}
-		templates[path] = file
+		templateFiles[path] = file
 	}
-	file = templates[path]
+	file = templateFiles[path]
 
 	return
 }