-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.go
102 lines (89 loc) · 2.14 KB
/
pages.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
package generate
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"github.com/robloxapi/roar/index"
)
const PageTemplate = `+++
title = %[1]q
+++
`
type Pages map[string]struct{}
func (p Pages) Add(page string) {
p[page] = struct{}{}
}
func (p Pages) Remove(page string) {
delete(p, page)
}
func (p Pages) Print() {
var keys []string
for k := range p {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("PAGES", len(keys))
for _, k := range keys {
fmt.Println("PAGE", k)
}
}
func scanPages(paths ...string) Pages {
pages := make(Pages, len(paths))
for _, base := range paths {
fs.WalkDir(os.DirFS(base), ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
pages.Add(filepath.Join(base, path))
return nil
})
}
return pages
}
func generatePageType[T any](rootPath, typ string, pages Pages, m map[string]T) {
basePath := filepath.Join(rootPath, typ)
os.MkdirAll(basePath, 0755)
pages.Remove(basePath)
var buf bytes.Buffer
for name := range m {
buf.Reset()
fmt.Fprintf(&buf, PageTemplate, name)
filePath := filepath.Join(basePath, name+".md")
if err := os.WriteFile(filePath, buf.Bytes(), 0666); err != nil {
fmt.Printf("generate page %q: %s\n", name, err)
continue
}
pages.Remove(filePath)
}
}
func GeneratePages(index *index.Root, rootPath string) {
// Set of existing pages.
pages := scanPages(
filepath.Join(rootPath, "class"),
filepath.Join(rootPath, "enum"),
filepath.Join(rootPath, "type"),
filepath.Join(rootPath, "updates"),
)
// Generate for each type of primary entity.
generatePageType(rootPath, "class", pages, index.Class)
generatePageType(rootPath, "enum", pages, index.Enum)
generatePageType(rootPath, "type", pages, index.Type)
// Generate pages for each year of changes.
years := map[string]struct{}{}
for i := index.MinYear; i <= index.MaxYear; i++ {
years[strconv.Itoa(i)] = struct{}{}
}
generatePageType(rootPath, "updates", pages, years)
// Remove untouched pages.
for path := range pages {
if err := os.RemoveAll(path); err != nil {
fmt.Println(err)
continue
}
fmt.Println("remove page", path)
}
}