all repos — elgit @ fed19ae329323cca0f7b49624486966fbce8096b

fork of legit: web frontend for git, written in go

templates/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
package templates

import (
	"strings"

	"go.alanpearce.eu/elgit/config"
	"go.alanpearce.eu/elgit/git"
	g "go.alanpearce.eu/gomponents"
	c "go.alanpearce.eu/gomponents/components"
	. "go.alanpearce.eu/gomponents/html"
)

type PageData struct {
	Meta        config.Meta
	DisplayName string
	Name        string
	Ref         string
	Readme      string
	Description string
	Path        string
	Parent      string
	Files       any
	Diff        []git.Diff
	Stat        git.Stat
	Commit      *git.Commit
	Commits     []*git.Commit
	LineCount   []int
	Content     string
	Branches    any
	Log         bool
	Servername  string
	Gomod       bool
}

func RenderHead(data PageData) []g.Node {
	var title string
	metaTitle := data.Meta.Title

	if data.Parent != "" {
		title = metaTitle + " — " + data.Name + " (" + data.Ref + "): " + data.Parent + "/"
	} else if data.Path != "" {
		title = metaTitle + " — " + data.Name + " (" + data.Ref + "): " + data.Path
	} else if data.Files != nil {
		title = metaTitle + " — " + data.Name + " (" + data.Ref + ")"
	} else if data.Commit != nil {
		title = metaTitle + " — " + data.Name + ": " + data.Commit.This
	} else if data.Branches != nil {
		title = metaTitle + " — " + data.Name + ": refs"
	} else if data.Commits != nil {
		if data.Log {
			title = metaTitle + " — " + data.Name + ": log"
		} else {
			title = metaTitle + " — " + data.Name
		}
	} else {
		title = metaTitle
	}

	return []g.Node{
		Link(Rel("stylesheet"), Href("/static/style.css"), Type("text/css")),
		Link(Rel("icon"), Type("image/png"), g.Attr("sizes", "32x32"), Href("/static/elgit.png")),
		TitleEl(g.Text(title)),
		g.If(data.Servername != "" && data.Gomod,
			Meta(
				Name("go-import"),
				Content(data.Servername+"/"+data.Name+" git https://"+data.Servername+"/"+data.Name),
			),
		),
	}
}

func RenderNav(data PageData) g.Node {
	var items []g.Node

	if data.Name != "" {
		items = append(items, Li(A(Href(joinPaths(data.Name)), g.Text("summary"))))
		items = append(items, Li(A(Href(joinPaths(data.Name, "refs", "")), g.Text("refs"))))

		if data.Ref != "" {
			items = append(items, Li(A(Href(joinPaths(data.Name, "tree", data.Ref, "")), g.Text("tree"))))
			items = append(items, Li(A(Href(joinPaths(data.Name, "log", data.Ref)), g.Text("log"))))
		}
	}

	return Nav(Ul(items...))
}

func RepoHeader(data PageData) g.Node {
	return Header(
		H2(
			A(
				Href("/"),
				g.Text("all repos"),
			),
			g.Text(" — "),
			g.Text(data.DisplayName),
			g.Text(" "),
			g.If(data.Ref != "", Span(Class("ref"), g.Textf("@ %s", data.Ref))),
		),
		H3(Class("desc"), g.Text(data.Description)),
	)
}

func Page(data PageData, body []g.Node) g.Node {
	return c.HTML5(c.HTML5Props{
		Language: "en",
		Head:     RenderHead(data),
		Body:     body,
	})
}

func joinPaths(paths ...string) string {
	return "/" + strings.Join(paths, "/")
}