docs: stdlib index sorted alphabetically

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba
2021-06-23 10:49:19 +02:00
parent b5550d0766
commit b03e695970
2 changed files with 41 additions and 28 deletions

View File

@@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"unicode/utf8"
@@ -365,6 +366,17 @@ func walkStdlib(ctx context.Context, output, format string) {
return false
}
// get filename from a package name
getFileName := func(p string) string {
filename := fmt.Sprintf("%s.%s", p, format)
// If this package has sub-packages (e.g. `aws`), create
// `aws/README.md` instead of `aws.md`.
if hasSubPackages(p) {
filename = fmt.Sprintf("%s/README.%s", p, format)
}
return filename
}
// Create main index
index, err := os.Create(path.Join(output, "README.md"))
if err != nil {
@@ -372,14 +384,10 @@ func walkStdlib(ctx context.Context, output, format string) {
}
defer index.Close()
fmt.Fprintf(index, "# Index\n\n")
indexKeys := []string{}
for p, pkg := range packages {
filename := fmt.Sprintf("%s.%s", p, format)
// If this package has sub-packages (e.g. `aws`), create
// `aws/README.md` instead of `aws.md`.
if hasSubPackages(p) {
filename = fmt.Sprintf("%s/README.%s", p, format)
}
filename := getFileName(p)
filepath := path.Join(output, filename)
if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
@@ -392,8 +400,13 @@ func walkStdlib(ctx context.Context, output, format string) {
}
defer f.Close()
// FIXME: sort packages by name
fmt.Fprintf(index, "- [%s](./%s)\n", p, filename)
indexKeys = append(indexKeys, p)
fmt.Fprintf(f, "%s", pkg.Format(format))
}
// Generate index from sorted list of packages
sort.Strings(indexKeys)
for _, p := range indexKeys {
fmt.Fprintf(index, "- [%s](./%s)\n", p, getFileName(p))
}
}