package templates import ( "fmt" "net/url" g "alin.ovh/gomponents" . "alin.ovh/gomponents/html" "alin.ovh/homestead/domain/web/templates" "alin.ovh/homestead/shared/config" ) type GoPackageVars struct { Package string Domain config.URL Forge config.URL } func GoPackageListPage(site templates.SiteSettings, cfg *config.GoPackagesConfig) g.Node { return templates.Layout(site, templates.PageSettings{ Title: site.Title, TitleAttrs: templates.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 templates.SiteSettings, cfg *config.GoPackagesConfig, pkg string) g.Node { return templates.Layout(site, templates.PageSettings{ Title: site.Title, TitleAttrs: templates.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))), Link(Rel("canonical"), Href(canonical(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() } func canonical(cfg *config.GoPackagesConfig, pkg string) string { return must(url.JoinPath("https://"+cfg.Domain.String(), pkg)) }