-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
95 lines (87 loc) · 2.16 KB
/
generate.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
package main
import (
"fmt"
"html/template"
"os"
"path/filepath"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
"go.bobheadxi.dev/twist/internal"
)
func generate(source string, canon canonical) {
b, err := internal.ReadFile("pkg.html")
if err != nil {
panic(err)
}
t, err := template.New("index.html").Parse(string(b))
if err != nil {
panic(err)
}
var (
parts = strings.Split(canon.Path, "/")
packageName = parts[len(parts)-1]
)
for _, p := range append(canon.Subpackages, "") {
target := filepath.Join(*outDir, packageName, p)
os.MkdirAll(target, os.ModePerm)
output := filepath.Join(target, "index.html")
fmt.Printf("generating template in '%s' (for '%s' => '%s')\n", output, source, canon.Path)
os.Remove(output)
f, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
if err := t.Execute(f, pkg{
Source: source,
Canonical: canon.Path,
}); err != nil {
panic(err)
}
f.Sync()
f.Close()
}
}
func generateREADME(cfg *config) {
// set up file
target := filepath.Join(*outDir, "README.md")
fmt.Printf("generating README in '%s'\n", target)
os.Remove(target)
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
// sort packages
keys := make([]string, len(cfg.Packages))
var i int
for s := range cfg.Packages {
keys[i] = s
i++
}
sort.Strings(keys)
// render table
table := tablewriter.NewWriter(f)
table.SetHeader([]string{"Package", "Reference", "Source"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for _, k := range keys {
if cfg.Packages[k].NoREADME {
continue
}
pkgPath := cfg.Packages[k].Path
docsSource := pkgPath
if cfg.GodocFromSource {
docsSource = k
}
table.Append([]string{
fmt.Sprintf("`%s`", pkgPath),
fmt.Sprintf("[![Reference](https://pkg.go.dev/badge/%s.svg)](https://pkg.go.dev/%s)", docsSource, docsSource),
fmt.Sprintf("[%s](https://%s)", k, k),
})
}
table.Render()
f.WriteString("\n---\n")
f.WriteString("\ngenerated using [twist](https://go.bobheadxi.dev/twist)\n")
f.Sync()
f.Close()
}