about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-06-24 21:48:36 +0200
committerAlan Pearce2024-06-24 23:09:15 +0200
commita2feb8c63c80a1f52830f562af2deb2c6065eaae (patch)
tree448ed15c5fb91099ef29528c529a35d6151d98ee
parent8623948592fa14958d340b006653fc57861c4fc4 (diff)
downloadwebsite-a2feb8c63c80a1f52830f562af2deb2c6065eaae.tar.lz
website-a2feb8c63c80a1f52830f562af2deb2c6065eaae.tar.zst
website-a2feb8c63c80a1f52830f562af2deb2c6065eaae.zip
move templ templates into separate package
-rw-r--r--default.nix3
-rw-r--r--internal/builder/builder.go13
-rw-r--r--internal/builder/template.go12
-rw-r--r--templates/404.templ (renamed from internal/builder/404.templ)6
-rw-r--r--templates/homepage.templ (renamed from internal/builder/homepage.templ)6
-rw-r--r--templates/list.templ (renamed from internal/builder/list.templ)10
-rw-r--r--templates/page.templ (renamed from internal/builder/page.templ)18
-rw-r--r--templates/post.templ (renamed from internal/builder/post.templ)6
-rw-r--r--templates/tags.templ (renamed from internal/builder/tags.templ)6
9 files changed, 49 insertions, 31 deletions
diff --git a/default.nix b/default.nix
index 778f790..e3a47f7 100644
--- a/default.nix
+++ b/default.nix
@@ -72,6 +72,7 @@ rec {
         ./go.sum
         ./cmd/build
         ./internal
+        ./templates
       ];
     };
     modules = ./nix/gomod2nix.toml;
@@ -92,6 +93,7 @@ rec {
         ];
       };
     } ''
+    cd $src
     ${builder}/bin/build -s $src -d $out
     cp $src/config.toml $out/
   '';
@@ -106,6 +108,7 @@ rec {
         ./go.sum
         ./cmd/server
         ./internal
+        ./templates
       ];
     };
     modules = ./nix/gomod2nix.toml;
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/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
 }
diff --git a/internal/builder/404.templ b/templates/404.templ
index 049e67d..4b5e52a 100644
--- a/internal/builder/404.templ
+++ b/templates/404.templ
@@ -1,9 +1,9 @@
-package builder
+package templates
 
 import "website/internal/config"
 
-templ notFound(config config.Config, path string) {
-	@page(config, PageSettings{
+templ NotFound(config config.Config, path string) {
+	@Page(config, PageSettings{
 		Title: "Not Found",
 		Path:  path,
 	}) {
diff --git a/internal/builder/homepage.templ b/templates/homepage.templ
index 848b9ed..0afbb2f 100644
--- a/internal/builder/homepage.templ
+++ b/templates/homepage.templ
@@ -1,12 +1,12 @@
-package builder
+package templates
 
 import (
 	"website/internal/config"
 	"website/internal/content"
 )
 
-templ homepage(config config.Config, posts []content.Post, content string) {
-	@page(config, PageSettings{
+templ Homepage(config config.Config, posts []content.Post, content string) {
+	@Page(config, PageSettings{
 		Title: config.Title,
 		TitleAttrs: templ.Attributes{
 			"class": "p-name u-url",
diff --git a/internal/builder/list.templ b/templates/list.templ
index 24cafe8..602c15c 100644
--- a/internal/builder/list.templ
+++ b/templates/list.templ
@@ -1,12 +1,12 @@
-package builder
+package templates
 
 import (
 	"website/internal/config"
 	"website/internal/content"
 )
 
-templ tagPage(config config.Config, tag string, posts []content.Post, path string) {
-	@page(config, PageSettings{
+templ TagPage(config config.Config, tag string, posts []content.Post, path string) {
+	@Page(config, PageSettings{
 		Title: tag,
 		Path:  path,
 		TitleAttrs: templ.Attributes{
@@ -24,8 +24,8 @@ templ tagPage(config config.Config, tag string, posts []content.Post, path strin
 	}
 }
 
-templ listPage(config config.Config, posts []content.Post, path string) {
-	@page(config, PageSettings{
+templ ListPage(config config.Config, posts []content.Post, path string) {
+	@Page(config, PageSettings{
 		Title: config.Title,
 		TitleAttrs: templ.Attributes{
 			"class": "p-author h-card",
diff --git a/internal/builder/page.templ b/templates/page.templ
index 9a2be2f..08c17a0 100644
--- a/internal/builder/page.templ
+++ b/templates/page.templ
@@ -1,10 +1,24 @@
-package builder
+package templates
 
 import (
+	"os"
 	"net/url"
+
 	"website/internal/config"
 )
 
+var (
+	css string
+)
+
+func init() {
+	bytes, err := os.ReadFile("templates/style.css")
+	if err != nil {
+		panic(err)
+	}
+	css = string(bytes)
+}
+
 type PageSettings struct {
 	Title      string
 	Path       string
@@ -29,7 +43,7 @@ templ menuItem(item config.MenuItem) {
 	>{ item.Name }</a>
 }
 
-templ page(site config.Config, page PageSettings) {
+templ Page(site config.Config, page PageSettings) {
 	<!DOCTYPE html>
 	<html lang={ site.DefaultLanguage }>
 		<head>
diff --git a/internal/builder/post.templ b/templates/post.templ
index 6f12e5c..5b09d4b 100644
--- a/internal/builder/post.templ
+++ b/templates/post.templ
@@ -1,4 +1,4 @@
-package builder
+package templates
 
 import (
 	"time"
@@ -19,8 +19,8 @@ templ postDate(d time.Time) {
 	</time>
 }
 
-templ postPage(config config.Config, post content.Post) {
-	@page(config, PageSettings{
+templ PostPage(config config.Config, post content.Post) {
+	@Page(config, PageSettings{
 		Title: post.Title,
 		TitleAttrs: templ.Attributes{
 			"class": "p-author h-card",
diff --git a/internal/builder/tags.templ b/templates/tags.templ
index 14abca4..7218ca1 100644
--- a/internal/builder/tags.templ
+++ b/templates/tags.templ
@@ -1,4 +1,4 @@
-package builder
+package templates
 
 import "website/internal/config"
 
@@ -6,8 +6,8 @@ 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{
+templ TagsPage(config config.Config, title string, tags []string, path string) {
+	@Page(config, PageSettings{
 		Title: title,
 		Path:  path,
 	}) {