-
-
Notifications
You must be signed in to change notification settings - Fork 462
/
overview.go
199 lines (172 loc) Β· 5.46 KB
/
overview.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
titleRegex = regexp.MustCompile(`(?m)^title: (.+)`)
keywordsRegex = regexp.MustCompile(`(?m)^keywords: \[(.+)\]`)
descriptionRegex = regexp.MustCompile(`(?m)^description: (.+)`)
headerRegex = regexp.MustCompile(`(?m)^# .+`)
)
//go:generate go run overview.go
func main() {
// fetch current file directory
root, _ := os.Getwd()
toc := ""
var missingReadmeDirs []string
var missingTitleDirs []string
var missingKeywordsDirs []string
var missingDescriptionDirs []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != root && filepath.Dir(path) == root && !strings.HasPrefix(info.Name(), ".") {
readmePath := filepath.Join(path, "README.md")
relativePath, err := filepath.Rel(root, path)
if err != nil {
return err
}
if _, err := os.Stat(readmePath); err == nil {
title, keywords, description, err := extractMetadata(readmePath)
if err != nil {
return err
}
if title == "" {
missingTitleDirs = append(missingTitleDirs, relativePath)
}
if len(keywords) == 0 {
missingKeywordsDirs = append(missingKeywordsDirs, relativePath)
}
if description == "" {
missingDescriptionDirs = append(missingDescriptionDirs, relativePath)
}
if title == "" {
title = "No title"
}
if description == "" {
description = "No description"
}
toc += fmt.Sprintf("- [%s](./%s/README.md) - %s\n", title, relativePath, description)
err = addLinksToReadme(readmePath, info.Name())
if err != nil {
return err
}
} else {
missingReadmeDirs = append(missingReadmeDirs, relativePath)
}
}
return nil
})
if err != nil {
fmt.Println("Error:", err)
return
}
readmePath := filepath.Join(root, "README.md")
content, err := os.ReadFile(readmePath)
if err != nil {
fmt.Println("Error:", err)
return
}
re := regexp.MustCompile(`(?s)<!-- AUTO-GENERATED-CONTENT:START -->(.*?)<!-- AUTO-GENERATED-CONTENT:END -->`)
newContent := re.ReplaceAllString(string(content), fmt.Sprintf("<!-- AUTO-GENERATED-CONTENT:START -->\n%s<!-- AUTO-GENERATED-CONTENT:END -->", toc))
err = os.WriteFile(readmePath, []byte(newContent), 0o644)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Table of contents updated successfully.")
if len(missingReadmeDirs) > 0 {
fmt.Println("Directories without README.md:")
for _, dir := range missingReadmeDirs {
fmt.Println("-", dir)
}
}
if len(missingTitleDirs) > 0 {
fmt.Println("Directories without Docusaurus title:")
for _, dir := range missingTitleDirs {
fmt.Println("-", dir)
}
}
if len(missingKeywordsDirs) > 0 {
fmt.Println("Directories without Docusaurus keywords:")
for _, dir := range missingKeywordsDirs {
fmt.Println("-", dir)
}
}
if len(missingDescriptionDirs) > 0 {
fmt.Println("Directories without Docusaurus description:")
for _, dir := range missingDescriptionDirs {
fmt.Println("-", dir)
}
}
if len(missingReadmeDirs) > 0 || len(missingTitleDirs) > 0 || len(missingKeywordsDirs) > 0 || len(missingDescriptionDirs) > 0 {
fmt.Println("Error: Some directories are missing README.md files, Docusaurus title, keywords, or description.")
os.Exit(1)
}
}
func extractMetadata(readmePath string) (string, []string, string, error) {
content, err := os.ReadFile(readmePath)
if err != nil {
return "", nil, "", err
}
titleMatches := titleRegex.FindSubmatch(content)
keywordsMatches := keywordsRegex.FindSubmatch(content)
descriptionMatches := descriptionRegex.FindSubmatch(content)
var title string
if len(titleMatches) > 1 {
title = strings.TrimSpace(string(titleMatches[1]))
}
var keywords []string
if len(keywordsMatches) > 1 {
keywords = strings.Split(string(keywordsMatches[1]), ",")
for i := range keywords {
keywords[i] = strings.TrimSpace(keywords[i])
}
}
var description string
if len(descriptionMatches) > 1 {
description = strings.TrimSpace(string(descriptionMatches[1]))
}
return title, keywords, description, nil
}
func addLinksToReadme(readmePath, dirName string) error {
content, err := os.ReadFile(readmePath)
if err != nil {
return err
}
links := fmt.Sprintf(
"[![Github](https://img.shields.io/static/v1?label=&message=Github&color=2ea44f&style=for-the-badge&logo=github)](https://github.com/gofiber/recipes/tree/master/%s) "+
"[![StackBlitz](https://img.shields.io/static/v1?label=&message=StackBlitz&color=2ea44f&style=for-the-badge&logo=StackBlitz)](https://stackblitz.com/github/gofiber/recipes/tree/master/%s)",
dirName, dirName)
lines := strings.Split(string(content), "\n")
headerIndex := -1
// Find the first headline
for i, line := range lines {
if headerRegex.MatchString(line) {
headerIndex = i
break
}
}
if headerIndex == -1 {
return fmt.Errorf("no headline found in %s", readmePath)
}
// Find the start and end of the section to be replaced
start := headerIndex + 1
end := len(lines)
for i := start; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) == "" || strings.Contains(lines[i], "![Github]") || strings.Contains(lines[i], "![StackBlitz]") {
continue
}
end = i
break
}
// Replace the section with the new links and surrounding empty lines
cleanedLines := append(lines[:start], append([]string{"", links, ""}, lines[end:]...)...)
content = []byte(strings.Join(cleanedLines, "\n"))
return os.WriteFile(readmePath, content, 0o644)
}