page.go 5.99 KB
Newer Older
Trevor Slocum's avatar
Trevor Slocum committed
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
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path"
	"strconv"
	"strings"
	"syscall"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"

	"github.com/PuerkitoBio/goquery"
)

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">&#9661;</span></a>-->
<div id="menu">
<a href="` + basePath + index + `" style="margin-right: 10px;">Packages</a>
</div>
</div>`
}

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))

Trevor Slocum's avatar
Trevor Slocum committed
56 57 58 59 60 61 62 63 64 65
	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]
			importPathDisplay.SetHtml(fmt.Sprintf(`import "<a href="https://` + importPath + `" target="_blank">` + importPath + `</a>"`))
			log.Println(importPathDisplay.Text())
		}
	}

Trevor Slocum's avatar
Trevor Slocum committed
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
	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().Remove()
}

func writeIndex(buf *bytes.Buffer, outDir string, basePath string, siteName string, pkgs []string, filterPkgs []string) {
	var index string
	if linkIndex {
		index = "/index.html"
	}

Trevor Slocum's avatar
Trevor Slocum committed
138 139
	buf.Reset()
	buf.WriteString(`<!DOCTYPE html>
Trevor Slocum's avatar
Trevor Slocum committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<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="` + basePath + `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(basePath, siteName) + `</div>
<div id="page" class="wide">
<div class="container">
`)

	if siteDescription != "" {
Trevor Slocum's avatar
Trevor Slocum committed
160
		buf.WriteString(siteDescription)
Trevor Slocum's avatar
Trevor Slocum committed
161 162
	}

Trevor Slocum's avatar
Trevor Slocum committed
163
	buf.WriteString(`
Trevor Slocum's avatar
Trevor Slocum committed
164 165 166 167 168 169 170 171 172 173 174 175 176
<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
Trevor Slocum's avatar
Trevor Slocum committed
177
	var pkgBuf bytes.Buffer
Trevor Slocum's avatar
Trevor Slocum committed
178
	for _, pkg := range pkgs {
Trevor Slocum's avatar
Trevor Slocum committed
179
		pkgBuf.Reset()
Trevor Slocum's avatar
Trevor Slocum committed
180 181 182 183 184
		listCmd := exec.Command("go", "list", "-find", "-f", `{{ .Doc }}`, pkg)
		listCmd.Dir = os.TempDir()
		listCmd.SysProcAttr = &syscall.SysProcAttr{
			Pdeathsig: syscall.SIGKILL,
		}
Trevor Slocum's avatar
Trevor Slocum committed
185
		listCmd.Stdout = &pkgBuf
Trevor Slocum's avatar
Trevor Slocum committed
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

		_ = listCmd.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
			}
		}
Trevor Slocum's avatar
Trevor Slocum committed
212
		buf.WriteString(`
Trevor Slocum's avatar
Trevor Slocum committed
213 214 215
		<tr>
			<td class="pkg-name" style="padding-left: ` + strconv.Itoa(padding) + `px;">`)
		if !linkPackage {
Trevor Slocum's avatar
Trevor Slocum committed
216
			buf.WriteString(pkgLabel)
Trevor Slocum's avatar
Trevor Slocum committed
217
		} else {
Trevor Slocum's avatar
Trevor Slocum committed
218
			buf.WriteString(`<a href="` + pkg + index + `">` + pkgLabel + `</a>`)
Trevor Slocum's avatar
Trevor Slocum committed
219
		}
Trevor Slocum's avatar
Trevor Slocum committed
220
		buf.WriteString(`</td>
Trevor Slocum's avatar
Trevor Slocum committed
221
			<td class="pkg-synopsis">
Trevor Slocum's avatar
Trevor Slocum committed
222
				` + pkgBuf.String() + `
Trevor Slocum's avatar
Trevor Slocum committed
223 224 225 226
			</td>
		</tr>
`)
	}
Trevor Slocum's avatar
Trevor Slocum committed
227
	buf.WriteString(`
Trevor Slocum's avatar
Trevor Slocum committed
228 229 230 231 232 233 234 235
	</table>
</div>
</div>
</div>
</body>
</html>
`)

Trevor Slocum's avatar
Trevor Slocum committed
236
	err := ioutil.WriteFile(path.Join(outDir, "index.html"), buf.Bytes(), 0755)
Trevor Slocum's avatar
Trevor Slocum committed
237 238 239 240
	if err != nil {
		log.Fatalf("failed to write index: %s", err)
	}
}