From a2feb8c63c80a1f52830f562af2deb2c6065eaae Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 24 Jun 2024 21:48:36 +0200 Subject: move templ templates into separate package --- templates/404.templ | 13 ++++++ templates/homepage.templ | 42 +++++++++++++++++ templates/list.templ | 51 +++++++++++++++++++++ templates/page.templ | 115 +++++++++++++++++++++++++++++++++++++++++++++++ templates/post.templ | 51 +++++++++++++++++++++ templates/tags.templ | 23 ++++++++++ 6 files changed, 295 insertions(+) create mode 100644 templates/404.templ create mode 100644 templates/homepage.templ create mode 100644 templates/list.templ create mode 100644 templates/page.templ create mode 100644 templates/post.templ create mode 100644 templates/tags.templ (limited to 'templates') diff --git a/templates/404.templ b/templates/404.templ new file mode 100644 index 0000000..4b5e52a --- /dev/null +++ b/templates/404.templ @@ -0,0 +1,13 @@ +package templates + +import "website/internal/config" + +templ NotFound(config config.Config, path string) { + @Page(config, PageSettings{ + Title: "Not Found", + Path: path, + }) { +

404

+

ʕノ•ᴥ•ʔノ ︵ ┻━┻

+ } +} diff --git a/templates/homepage.templ b/templates/homepage.templ new file mode 100644 index 0000000..0afbb2f --- /dev/null +++ b/templates/homepage.templ @@ -0,0 +1,42 @@ +package templates + +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", + }, + }) { +
+ @Unsafe(content) +
+
+

Latest Posts

+ @list(posts[0:3]) +
+
+

Elsewhere on the Internet

+ +
+ } +} diff --git a/templates/list.templ b/templates/list.templ new file mode 100644 index 0000000..602c15c --- /dev/null +++ b/templates/list.templ @@ -0,0 +1,51 @@ +package templates + +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", + }, + }) { +
+

#{ tag }

+ + Remove filter + +
+ @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) { + +} diff --git a/templates/page.templ b/templates/page.templ new file mode 100644 index 0000000..08c17a0 --- /dev/null +++ b/templates/page.templ @@ -0,0 +1,115 @@ +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 + 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) { + { item.Name } +} + +templ Page(site config.Config, page PageSettings) { + + + + + + { page.Title } + + @style(css) + + + +
+

+ { site.Title } +

+ +
+
+ { children... } +
+ + @counter(page.Path, page.Title) + if site.InjectLiveReload { + + } + + +} + +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) { + + +} + +func style(css string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { + _, err = io.WriteString(w, "") + return + }) +} diff --git a/templates/post.templ b/templates/post.templ new file mode 100644 index 0000000..5b09d4b --- /dev/null +++ b/templates/post.templ @@ -0,0 +1,51 @@ +package templates + +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) { + +} + +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, + }) { +
+

{ post.Title }

+

+ @postDate(post.Date) +

+
+ @Unsafe(post.Content) +
+
+ Tags: + +
+
+ } +} diff --git a/templates/tags.templ b/templates/tags.templ new file mode 100644 index 0000000..7218ca1 --- /dev/null +++ b/templates/tags.templ @@ -0,0 +1,23 @@ +package templates + +import "website/internal/config" + +templ tagLink(tag string, attrs templ.Attributes) { + #{ tag } +} + +templ TagsPage(config config.Config, title string, tags []string, path string) { + @Page(config, PageSettings{ + Title: title, + Path: path, + }) { +

Tags

+ + } +} -- cgit 1.4.1