replace templ with gomponents
8 files changed, 163 insertions(+), 161 deletions(-)
M go.sum → go.sum
@@ -1,14 +1,12 @@ -github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU= -github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.alanpearce.eu/gomponents v1.4.0 h1:Ibvoce+U0rnPKlDOE+wXDbNniNQL8mYO667+qS5J1Go= +go.alanpearce.eu/gomponents v1.4.0/go.mod h1:WxD+6FRSvwThQOzV0r6zPAA9CRb41lutUZMSC7r6BRc= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
M main.go → main.go
@@ -1,26 +1,25 @@ package main import ( - "context" "fmt" "log" "net/url" "os" "path/filepath" - "github.com/a-h/templ" "github.com/pelletier/go-toml/v2" + "go.alanpearce.eu/gomponents" ) type Config struct { Title string Domain string Forge string - Menu []*Link + Menu []*MenuLink Packages []string } -type Link struct { +type MenuLink struct { Name string URL string }@@ -83,7 +82,7 @@ } const destDir = "dist" -func renderToFile(c templ.Component, filename string) error { +func renderToFile(c gomponents.Node, filename string) error { pathname := filepath.Join(destDir, filename) err := os.MkdirAll(filepath.Dir(pathname), 0755) if err != nil {@@ -95,7 +94,7 @@ return err } defer file.Close() - return c.Render(context.Background(), file) + return c.Render(file) } func main() {
D page.templ
@@ -1,146 +0,0 @@ -package main - -templ Page(config *Config, pkg string) { - <!DOCTYPE html> - <html lang="en"> - <head> - <meta charset="utf-8"/> - <meta name="viewport" content="width=device-width, initial-scale=1.0"/> - if pkg != "" { - <meta name="go-import" content={ importString(config, pkg) }/> - <meta name="go-source" content={ sourceString(config, pkg) }/> - } - <style> - :root { - --width: 800px; - --font-main: Verdana, sans-serif; - --font-secondary: Verdana, sans-serif; - --font-scale: 1em; - --background-color: #fff; - --heading-color: #222; - --text-color: #444; - --link-color: #3273dc; - --visited-color: #8b6fcb; - --code-background-color: #f2f2f2; - --code-color: #222; - --blockquote-color: #222; - --icon-external-link: url("/external-link.svg"); - } - - @media (prefers-color-scheme: dark) { - :root { - --background-color: #01242e; - --heading-color: #eee; - --text-color: #ddd; - --link-color: #8cc2dd; - --visited-color: #8b6fcb; - --code-background-color: #000; - --code-color: #ddd; - --blockquote-color: #ccc; - } - } - - body { - font-family: var(--font-secondary); - font-size: var(--font-scale); - margin: auto; - padding: 20px; - max-width: var(--width); - text-align: left; - background-color: var(--background-color); - word-wrap: break-word; - overflow-wrap: break-word; - line-height: 1.5; - color: var(--text-color); - } - - h1, - h2, - h3, - h4, - h5, - h6 { - font-family: var(--font-main); - color: var(--heading-color); - & > a { - color: var(--heading-color); - } - } - - a { - color: var(--link-color); - cursor: pointer; - } - - nav a { - margin-right: 8px; - } - - strong, - b { - color: var(--heading-color); - } - - main { - margin-top: 2ex; - line-height: 1.6; - } - - table { - width: 100%; - } - </style> - <title>{ config.Title }</title> - </head> - <body> - <header> - <h1>{ config.Title }</h1> - <nav> - for _, link := range config.Menu { - <a href={ templ.SafeURL(link.URL) }>{ link.Name }</a> - } - </nav> - </header> - <main> - { children... } - </main> - </body> - </html> -} - -templ ListPage(config *Config) { - @Page(config, "") { - <table> - <thead> - <tr> - <th>Name</th> - <th>Source</th> - <th>Documentation</th> - </tr> - </thead> - <tbody> - for _, pkg := range config.Packages { - <tr> - <td>{ packageImportPath(config, pkg) }</td> - <td><a href={ templ.SafeURL(packageForgeURL(config, pkg)) }>Source </a></td> - <td> - @GodocBadge(config, pkg) - </td> - </tr> - } - </tbody> - </table> - } -} - -templ PackagePage(config *Config, pkg string) { - @Page(config, pkg) { - <p>You're probably looking for the <a href={ templ.SafeURL(godocURL(config, pkg)) }>documentation</a>.</p> - } -} - -templ GodocBadge(config *Config, pkg string) { - <a href={ templ.SafeURL(godocURL(config, pkg)) }> - Go Reference - </a> -}
A templates.go
@@ -0,0 +1,154 @@ +package main + +import ( + g "go.alanpearce.eu/gomponents" + c "go.alanpearce.eu/gomponents/components" + . "go.alanpearce.eu/gomponents/html" +) + +const style = ` +:root { + --width: 800px; + --font-main: Verdana, sans-serif; + --font-secondary: Verdana, sans-serif; + --font-scale: 1em; + --background-color: #fff; + --heading-color: #222; + --text-color: #444; + --link-color: #3273dc; + --visited-color: #8b6fcb; + --code-background-color: #f2f2f2; + --code-color: #222; + --blockquote-color: #222; +} + +@media (prefers-color-scheme: dark) { + :root { + --background-color: #01242e; + --heading-color: #eee; + --text-color: #ddd; + --link-color: #8cc2dd; + --visited-color: #8b6fcb; + --code-background-color: #000; + --code-color: #ddd; + --blockquote-color: #ccc; + } +} + +body { + font-family: var(--font-secondary); + font-size: var(--font-scale); + margin: auto; + padding: 20px; + max-width: var(--width); + text-align: left; + background-color: var(--background-color); + word-wrap: break-word; + overflow-wrap: break-word; + line-height: 1.5; + color: var(--text-color); +} + +h1, +h2, +h3, +h4, +h5, +h6 { + font-family: var(--font-main); + color: var(--heading-color); + & > a { + color: var(--heading-color); + } +} + +a { + color: var(--link-color); + cursor: pointer; +} + +nav a { + margin-right: 8px; +} + +strong, +b { + color: var(--heading-color); +} + +main { + margin-top: 2ex; + line-height: 1.6; +} + +table { + width: 100%; +} +` + +func Page(config *Config, pkg string, children ...g.Node) g.Node { + return c.HTML5(c.HTML5Props{ + Title: config.Title, + Language: "en-GB", + Head: []g.Node{ + g.If(pkg != "", + g.Group{ + Meta(Name("go-import"), Content(importString(config, pkg))), + Meta(Name("go-source"), Content(sourceString(config, pkg))), + }, + ), + StyleEl(g.Raw(style)), + }, + Body: []g.Node{ + Header( + H1(g.Text(config.Title)), + Nav( + g.Map(config.Menu, func(link *MenuLink) g.Node { + return A(Href(link.URL), g.Text(link.Name)) + }), + ), + ), + Main( + children..., + ), + }, + HTMLAttrs: []g.Node{}, + }) +} + +func ListPage(config *Config) g.Node { + return Page(config, "", + Table( + THead( + Tr( + Th(g.Text("Name")), + Th(g.Text("Source")), + Th(g.Text("Documentation")), + ), + ), + TBody( + g.Map(config.Packages, func(pkg string) g.Node { + return Tr( + Td(g.Text(packageImportPath(config, pkg))), + Td(A(Href(packageForgeURL(config, pkg)), g.Text("Source"))), + Td(GodocBadge(config, pkg)), + ) + }), + ), + ), + ) +} + +func PackagePage(config *Config, pkg string) g.Node { + return Page(config, pkg, + P( + g.Text("You're probably looking for the "), + A(Href(godocURL(config, pkg)), g.Text("documentation")), + g.Text("."), + ), + ) +} + +func GodocBadge(config *Config, pkg string) g.Node { + return A(Href(godocURL(config, pkg)), g.Text("Go Reference")) +}