all repos — searchix @ be1d2e4688abc9637a4a9926291510a4b65f91c5

Search engine for NixOS, nix-darwin, home-manager and NUR users

internal/components/page.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package components

import (
	"net/url"

	"alin.ovh/searchix/frontend"
	"alin.ovh/searchix/internal/config"

	g "alin.ovh/gomponents"
	c "alin.ovh/gomponents/components"
	. "alin.ovh/gomponents/html"
)

func Page(tdata TemplateData, children ...g.Node) g.Node {
	return Doctype(
		HTML(
			Lang("en-GB"),
			Head(
				Meta(Charset("utf-8")),
				Meta(Name("viewport"), Content("width=device-width, initial-scale=1")),
				TitleEl(
					g.Raw(tdata.Title),
					g.Text("Searchix"), g.If(config.DevMode, g.Text(" (Dev)")),
				),
				g.Map(tdata.Assets.Stylesheets, css),
				g.Raw(tdata.ExtraHeadHTML),
				g.If(len(tdata.Sources) > 0,
					Link(
						Rel("search"),
						Type("application/opensearchdescription+xml"),
						TitleAttr("Searchix "+tdata.Source.String()),
						Href(joinPath("opensearch.xml")),
					),
				),
				g.Map(tdata.Sources, func(source config.Source) g.Node {
					return Link(
						Rel("search"),
						Type("application/opensearchdescription+xml"),
						TitleAttr("Searchix "+source.String()),
						Href(joinPath("/", source.Importer.String(), source.Key, "opensearch.xml")),
					)
				}),
			),
			Body(
				Header(
					Nav(
						H1(A(Href("/"), g.Text("Searchix"))),
						g.If(len(tdata.Sources) > 0,
							A(
								c.Classes{
									"current": tdata.Source.Importer == config.All,
								},
								g.If(
									tdata.Source.Importer == config.All,
									Href("/"),
									Href(joinPathQuery("/", tdata.Query)),
								),
								g.Text("All"),
							),
						),
						g.Map(tdata.Sources, func(source config.Source) g.Node {
							if tdata.Source.Importer != config.All && tdata.Source.Name == source.Name {
								return A(
									Class("current"),
									Href(
										joinPath(
											"/",
											source.Importer.String(),
											source.Key,
											"search",
										),
									),
									g.Text(source.Name),
								)
							}

							return A(
								Href(
									joinPathQuery(
										joinPath(
											"/",
											source.Importer.String(),
											source.Key,
											"search",
										),
										tdata.Query,
									),
								),
								g.Text(source.Name),
							)
						}),
					),
				),
				Main(children...),
				Footer(
					P(
						g.If(config.Version != "",
							g.Group([]g.Node{
								g.Text("Searchix "),
								A(
									Href(
										"https://codeberg.org/alinnow/searchix/src/tag/v"+config.Version+"/CHANGELOG.md",
									),
									TitleAttr("View changelog"),
									g.Textf("v%s", config.Version),
								),
								g.Text(" "),
							}),
						),
					),
					P(
						A(Href("https://codeberg.org/alinnow/searchix"), g.Text("Source code")),
						g.Text(" "),
						A(Href("https://status.searchix.ovh"), g.Text("Status page")),
						g.Text(" "),
						A(
							Href("https://codeberg.org/alinnow/searchix/issues"),
							g.Text("Report issues"),
						),
					),
					P(
						g.Text("Made by "),
						A(Href("https://alanpearce.eu"), g.Text("Alin (Alan Pearce)")),
					),
				),
			),
		),
	)
}

func css(css *frontend.Asset) g.Node {
	return Link(Href(css.ImmutablePath), Rel("stylesheet"))
}

func script(s *frontend.Asset) g.Node {
	return Script(Src(s.ImmutablePath), Defer())
}

func joinPath(base string, parts ...string) string {
	u, err := url.JoinPath(base, parts...)
	if err != nil {
		panic(err)
	}

	return u
}

func joinPathQuery(path string, query string) string {
	if query == "" {
		return path
	}

	return path + "?query=" + url.QueryEscape(query)
}