-
Notifications
You must be signed in to change notification settings - Fork 15
/
generate_index.go
91 lines (76 loc) · 1.86 KB
/
generate_index.go
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
package main
import (
"fmt"
"html/template"
"io"
)
func generate_index(w io.Writer, domain string, r []repository) error {
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{.Domain}} Go Modules</title>
<style>
* { font-family: sans-serif; }
body { margin-top: 0; }
.content { display: inline-block; }
</style>
</head>
<body>
<div class="content">
<h2>{{.Domain}} Go Modules</h2>
<h3>Tools:</h3>
<ul>
{{range $_, $r := .MainRepositories -}}
<li>
<a href="/{{$r.Prefix}}">{{$r.Prefix}}</a>
{{if .Subs -}}<ul>{{end -}}
{{range $_, $s := .Subs -}}{{if not $s.Hidden -}}<li><a href="/{{$r.Prefix}}/{{$s.Name}}">{{$s.Name}}</a></li>{{end -}}{{end -}}
{{if .Subs -}}</ul>{{end -}}
</li>
{{end -}}
</ul>
<h3>Libraries:</h3>
<ul>
{{range $_, $r := .PackageRepositories -}}{{if not $r.Hidden -}}
<li>
<a href="/{{$r.Prefix}}">{{$r.Prefix}}</a>
{{if .Subs -}}<ul>{{end -}}
{{range $_, $s := .Subs -}}{{if not $s.Hidden -}}<li><a href="/{{$r.Prefix}}/{{$s.Name}}">{{$s.Name}}</a></li>{{end -}}{{end -}}
{{if .Subs -}}</ul>{{end -}}
</li>
{{end }}{{end -}}
</ul>
<hr/>
Generated by <a href="https://4d63.com/vangen">vangen</a>.
</div>
</body>
</html>`
tmpl, err := template.New("").Parse(html)
if err != nil {
return fmt.Errorf("error loading template: %v", err)
}
mainRepositories := []repository{}
packageRepositories := []repository{}
for _, r := range r {
if r.Main {
mainRepositories = append(mainRepositories, r)
} else {
packageRepositories = append(packageRepositories, r)
}
}
data := struct {
Domain string
MainRepositories []repository
PackageRepositories []repository
}{
Domain: domain,
MainRepositories: mainRepositories,
PackageRepositories: packageRepositories,
}
err = tmpl.ExecuteTemplate(w, "", data)
if err != nil {
return fmt.Errorf("generating template: %v", err)
}
return nil
}