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 `
` + siteName + `
` + siteName + `
` } 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] importPathDisplay.SetHtml(fmt.Sprintf(`import "` + importPath + `"`)) log.Println(importPathDisplay.Text()) } } 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("%s", 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" } buf.Reset() buf.WriteString(` ` + siteName + `
...
` + topBar(basePath, siteName) + `
`) if siteDescription != "" { buf.WriteString(siteDescription) } buf.WriteString(`

Packages

`) var padding int var lastPkg string var pkgBuf bytes.Buffer for _, pkg := range pkgs { pkgBuf.Reset() listCmd := exec.Command("go", "list", "-find", "-f", `{{ .Doc }}`, pkg) listCmd.Dir = os.TempDir() listCmd.SysProcAttr = &syscall.SysProcAttr{ Pdeathsig: syscall.SIGKILL, } listCmd.Stdout = &pkgBuf _ = 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 } } buf.WriteString(` `) } buf.WriteString(`
Name Synopsis
`) if !linkPackage { buf.WriteString(pkgLabel) } else { buf.WriteString(`` + pkgLabel + ``) } buf.WriteString(` ` + pkgBuf.String() + `
`) err := ioutil.WriteFile(path.Join(outDir, "index.html"), buf.Bytes(), 0755) if err != nil { log.Fatalf("failed to write index: %s", err) } }