forked from leighmcculloch/vangen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_package.go
105 lines (95 loc) · 2.62 KB
/
generate_package.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"html/template"
"io"
"strings"
)
func generate_package(w io.Writer, domain, docsDomain, pkg string, r repository) error {
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{.Domain}}{{.Package}}</title>
<meta name="go-import" content="{{.Domain}}{{.Repository.Prefix}} {{.Repository.Type}} {{.Repository.URL}}">
<meta name="go-source" content="{{.Domain}}{{.Repository.Prefix}} {{.Repository.SourceURLs.Home}} {{.Repository.SourceURLs.Dir}} {{.Repository.SourceURLs.File}}">
<style>
* { font-family: sans-serif; }
body { margin-top: 0; }
.content { display: inline-block; }
code { display: block; font-family: monospace; font-size: 1em; background-color: #d5d5d5; padding: 1em; margin-bottom: 16px; }
ul { margin-top: 16px; margin-bottom: 16px; }
</style>
</head>
<body>
<div class="content">
<h2>{{.Domain}}{{.Package}}</h2>
<code>go get {{.Domain}}{{.Package}}</code>
<code>import "{{.Domain}}{{.Package}}"</code>
Home: <a href="{{.HomeURL}}">{{.HomeURL}}</a><br/>
Source: <a href="{{.Repository.URL}}">{{.Repository.URL}}</a><br/>
{{if .Repository.Subs -}}Sub-packages:<ul>{{end -}}
{{range $i, $s := .Repository.Subs -}}{{if not $s.Hidden -}}<li><a href="/{{$.Repository.SubPath $i}}">{{$.Domain}}/{{$.Repository.SubPath $i}}</a></li>{{end -}}{{end -}}
{{if .Repository.Subs -}}</ul>{{end -}}
</div>
</body>
</html>`
tmpl, err := template.New("").Parse(html)
if err != nil {
return fmt.Errorf("error loading template: %v", err)
}
if pkg != "" {
pkg = "/" + pkg
}
var homeURL string
if r.Website.URL != "" {
homeURL = r.Website.URL
} else {
if docsDomain == "" {
docsDomain = "pkg.go.dev"
}
homeURL = fmt.Sprintf("https://%s/%s", docsDomain, domain)
if pkg != "" {
homeURL = homeURL + pkg
}
}
if strings.HasPrefix(r.URL, "https://github.com") || strings.HasPrefix(r.URL, "https://gitlab.com") {
if r.Type == "" {
r.Type = "git"
}
if r.SourceURLs.Home == "" {
r.SourceURLs.Home = r.URL
}
if r.SourceURLs.Dir == "" {
r.SourceURLs.Dir = r.URL + "/tree/main{/dir}"
}
if r.SourceURLs.File == "" {
r.SourceURLs.File = r.URL + "/blob/main{/dir}/{file}#L{line}"
}
}
if r.SourceURLs.Home == "" {
r.SourceURLs.Home = "_"
}
if r.SourceURLs.Dir == "" {
r.SourceURLs.Dir = "_"
}
if r.SourceURLs.File == "" {
r.SourceURLs.File = "_"
}
data := struct {
Domain string
Package string
Repository repository
HomeURL string
}{
Domain: domain,
Package: pkg,
Repository: r,
HomeURL: homeURL,
}
err = tmpl.ExecuteTemplate(w, "", data)
if err != nil {
return fmt.Errorf("generating template: %v", err)
}
return nil
}