package templates import ( "fmt" "net/url" g "go.alanpearce.eu/gomponents" . "go.alanpearce.eu/gomponents/html" "go.alanpearce.eu/homestead/internal/config" ) type GoPackageVars struct { Package string Domain config.URL Forge config.URL } func GoPackageListPage(site SiteSettings, cfg *config.GoPackagesConfig) g.Node { return Layout(site, PageSettings{ Title: site.Title, TitleAttrs: Attrs{"class": "p-author h-card"}, }, Table( THead( Tr( Th(g.Text("Name")), Th(g.Text("Source")), Th(g.Text("Documentation")), ), ), TBody( g.Map(cfg.Packages, func(pkg string) g.Node { return Tr( Td(g.Text(packageImportPath(cfg, pkg))), Td(A(Href(packageForgeURL(cfg, pkg)), g.Text("Source"))), Td(GodocBadge(cfg, pkg)), ) }), ), ), ) } func GoPackagePage(site SiteSettings, cfg *config.GoPackagesConfig, pkg string) g.Node { return Layout(site, PageSettings{ Title: site.Title, TitleAttrs: Attrs{"class": "p-author h-card"}, HeadExtra: g.Group{ Meta(Name("go-import"), Content(importString(cfg, pkg))), Meta(Name("go-source"), Content(sourceString(cfg, pkg))), }, }, Div( P( g.Text("You're probably looking for the "), A(Href(godocURL(cfg, pkg)), g.Text("documentation")), g.Text(" or the "), A(Href(packageForgeURL(cfg, pkg)), g.Text("source")), g.Text("."), ), GodocBadge(cfg, pkg), ), ) } func GodocBadge(cfg *config.GoPackagesConfig, pkg string) g.Node { return A(Href(godocURL(cfg, pkg)), Img( Alt("Go Reference"), Src(badgeURL(cfg, pkg)+".svg"), ), ) } func must[T any](t T, err error) T { if err != nil { panic(err) } return t } func importString(cfg *config.GoPackagesConfig, pkg string) string { return fmt.Sprintf("%s git %s", cfg.Domain.JoinPath(pkg), cfg.Forge.JoinPath(pkg), ) } func sourceString(cfg *config.GoPackagesConfig, pkg string) string { return fmt.Sprintf("%s _ %s %s", cfg.Domain.JoinPath(pkg), cfg.Forge.JoinPath(pkg).String()+"/tree/main/{dir}", cfg.Forge.JoinPath(pkg).String()+"/tree/main/{dir}/{file}#n{line}", ) } func godocURL(cfg *config.GoPackagesConfig, pkg string) string { return must(url.JoinPath("https://pkg.go.dev", packageImportPath(cfg, pkg))) } func badgeURL(cfg *config.GoPackagesConfig, pkg string) string { return must(url.JoinPath("https://pkg.go.dev/badge/", packageImportPath(cfg, pkg))) } func packageImportPath(cfg *config.GoPackagesConfig, pkg string) string { return cfg.Domain.JoinPath(pkg).String() } func packageForgeURL(cfg *config.GoPackagesConfig, pkg string) string { return cfg.Forge.JoinPath(pkg).String() }