-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
286 lines (247 loc) · 7.23 KB
/
page.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
const additionalCSS = `
details { margin-top: 20px; }
summary { margin-left: 20px; cursor: pointer; }
#footer > p, #footer > li { max-width: none; word-wrap: normal; }
`
const footerText = `Generated by <a href="https://godoc.org/golang.org/x/tools/godoc" target="_blank">godoc</a> + <a href="https://code.rocketnine.space/tslocum/godoc-static" target="_blank">godoc-static</a>`
func topBar(basePath string, siteName string) string {
var index string
if linkIndex {
index = "index.html"
}
return `<div class="container">
<div class="top-heading" id="heading-wide"><a href="` + basePath + index + `">` + siteName + `</a></div>
<div class="top-heading" id="heading-narrow"><a href="` + basePath + index + `">` + siteName + `</a></div>
<!--<a href="#" id="menu-button"><span id="menu-button-arrow">▽</span></a>-->
<div id="menu">
<a href="` + basePath + index + `" style="margin-right: 10px;">Package Index</a>
</div>
</div>`
}
func siteFooterText(basePath string) string {
footer := siteFooter
addP := footer != ""
if addP {
footer += "<p>"
}
if siteZip != "" {
footer += `<a href="` + basePath + siteZip + `">Download ` + siteZip + `</a> to browse offline - `
}
footer += footerText
if addP {
footer += "</p>"
}
return footer
}
func updatePage(doc *goquery.Document, basePath string, siteName string) {
doc.Find("link").Remove()
doc.Find("script").Remove()
linkTag := &html.Node{
Type: html.ElementNode,
DataAtom: atom.Link,
Data: "link",
Attr: []html.Attribute{
{Key: "type", Val: "text/css"},
{Key: "rel", Val: "stylesheet"},
{Key: "href", Val: basePath + "lib/style.css"},
},
}
doc.Find("head").AppendNodes(linkTag)
doc.Find("#topbar").First().SetHtml(topBar(basePath, siteName))
importPathDisplay := doc.Find("#short-nav").First().Find("code").First()
if importPathDisplay.Length() > 0 {
importPathDisplayText := importPathDisplay.Text()
if strings.ContainsRune(importPathDisplayText, '.') && strings.HasPrefix(importPathDisplayText, `import "`) && strings.HasSuffix(importPathDisplayText, `"`) {
importPath := importPathDisplayText[8 : len(importPathDisplayText)-1]
browseImportPath := importPath
var browseInsert string
if strings.HasPrefix(importPath, "gitlab.com/") {
browseInsert = "/-/tree/master"
} else if strings.HasPrefix(importPath, "github.com/") || strings.HasPrefix(importPath, "git.sr.ht/") {
browseInsert = "/tree/master"
} else if strings.HasPrefix(importPath, "bitbucket.org/") || strings.HasPrefix(importPath, "code.rocketnine.space/") {
browseInsert = "/src/master"
}
if browseInsert != "" {
var insertPos int
var found int
for i, c := range importPath {
if c == '/' {
found++
if found == 3 {
insertPos = i
break
}
}
}
if insertPos > 0 {
browseImportPath = importPath[0:insertPos] + browseInsert + importPath[insertPos:]
}
}
importPathDisplay.SetHtml(fmt.Sprintf(`import "<a href="https://` + browseImportPath + `" target="_blank">` + importPath + `</a>"`))
}
}
doc.Find("a").Each(func(_ int, selection *goquery.Selection) {
href := selection.AttrOr("href", "")
if strings.HasPrefix(href, "/src/") || strings.HasPrefix(href, "/pkg/") {
if strings.ContainsRune(path.Base(href), '.') {
queryPos := strings.IndexRune(href, '?')
if queryPos >= 0 {
href = href[0:queryPos] + ".html" + href[queryPos:]
} else {
hashPos := strings.IndexRune(href, '#')
if hashPos >= 0 {
href = href[0:hashPos] + ".html" + href[hashPos:]
} else {
href += ".html"
}
}
} else if linkIndex {
queryPos := strings.IndexRune(href, '?')
if queryPos >= 0 {
href = href[0:queryPos] + "/index.html" + href[queryPos:]
} else {
hashPos := strings.IndexRune(href, '#')
if hashPos >= 0 {
href = href[0:hashPos] + "/index.html" + href[hashPos:]
} else {
href += "/index.html"
}
}
}
if strings.HasPrefix(href, "/pkg/") {
href = href[4:]
}
selection.SetAttr("href", basePath+href[1:])
}
})
doc.Find("div").Each(func(_ int, selection *goquery.Selection) {
if selection.HasClass("toggle") {
var summary string
var err error
selection.Find("div").Each(func(_ int, subSelection *goquery.Selection) {
if subSelection.HasClass("collapsed") {
summary, err = subSelection.Find("span.text").First().Html()
if err != nil {
summary = "Summary not available"
}
subSelection.Remove()
}
})
selection.Find(".toggleButton").Remove()
selection.PrependHtml(fmt.Sprintf("<summary>%s</summary>", summary))
selection.RemoveClass("toggle")
selection.Nodes[0].Data = "details"
selection.Nodes[0].DataAtom = atom.Details
}
})
doc.Find("#footer").Last().SetHtml(siteFooterText(basePath))
}
func writeIndex(buf *bytes.Buffer, pkgs []string, filterPkgs []string) error {
var index string
if linkIndex {
index = "/index.html"
}
buf.Reset()
buf.WriteString(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">
<title>` + siteName + `</title>
<link type="text/css" rel="stylesheet" href="lib/style.css">
</head>
<body>
<div id="lowframe" style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->
<div id="topbar" class="wide">` + topBar("", siteName) + `</div>
<div id="page" class="wide">
<div class="container">
`)
if siteDescription != "" {
buf.WriteString(siteDescription)
}
buf.WriteString(`
<h1>
Packages
</h1>
<div class="pkg-dir">
<table>
<tr>
<th class="pkg-name">Name</th>
<th class="pkg-synopsis">Synopsis</th>
</tr>
`)
var padding int
var lastPkg string
var pkgBuf bytes.Buffer
for _, pkg := range pkgs {
pkgBuf.Reset()
cmd := exec.Command("go", "list", "-find", "-f", `{{ .Doc }}`, pkg)
cmd.Env = godocEnv
cmd.Dir = os.TempDir()
cmd.Stdout = &pkgBuf
setDeathSignal(cmd)
cmd.Run() // Ignore error
pkgLabel := pkg
if lastPkg != "" {
lastPkgSplit := strings.Split(lastPkg, "/")
pkgSplit := strings.Split(pkg, "/")
shared := 0
for i := range pkgSplit {
if i < len(lastPkgSplit) && strings.ToLower(lastPkgSplit[i]) == strings.ToLower(pkgSplit[i]) {
shared++
}
}
padding = shared * 20
pkgLabel = strings.Join(pkgSplit[shared:], "/")
}
lastPkg = pkg
var linkPackage bool
for _, filterPkg := range filterPkgs {
if pkg == filterPkg {
linkPackage = true
break
}
}
buf.WriteString(`
<tr>
<td class="pkg-name" style="padding-left: ` + strconv.Itoa(padding) + `px;">`)
if !linkPackage {
buf.WriteString(pkgLabel)
} else {
buf.WriteString(`<a href="` + pkg + index + `">` + pkgLabel + `</a>`)
}
buf.WriteString(`</td>
<td class="pkg-synopsis">
` + pkgBuf.String() + `
</td>
</tr>
`)
}
buf.WriteString(`
</table>
</div>
<div id="footer">` + siteFooterText("") + `</div>
</div>
</div>
</body>
</html>
`)
return writeFile(buf, "", "index.html")
}