all repos — homestead @ b8a2bb93e2b2cc9cc609053d02b9cdd7d64de0c3

Code for my website

templates/gopkg.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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()
}