-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.go
188 lines (144 loc) · 3.34 KB
/
readme.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
package main
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"
)
const title = `
# Stadio
Compendium of functions, data structures, monadic wrappers and more which, hopefully, will be
included as a standard library of the language
## Modules
`
type (
fun struct {
name string
comment string
body *bytes.Buffer
}
mod struct {
title string
description string
funs []fun
}
)
func (m *mod) CutreSort() {
for i := len(m.funs) - 1; i > 0; i-- {
jmax := i
for j := 0; j < i; j++ {
f2 := m.funs[j]
if strings.Compare(m.funs[jmax].name, f2.name) == -1 {
jmax = j
}
}
tmp := m.funs[i]
m.funs[i] = m.funs[jmax]
m.funs[jmax] = tmp
}
}
func (m mod) String() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("### " + strings.ToUpper(m.title[:1]) + m.title[1:])
buf.WriteString("\n\n" + m.description)
buf.WriteString("\n")
// index
buf.WriteString("Table of contents\n")
for _, fn := range m.funs {
buf.WriteString(fmt.Sprintf("\n- [%s](####%s)", fn.name, fn.name))
}
buf.WriteString("\n\n")
for _, fn := range m.funs {
buf.WriteString(fn.String())
}
return buf.String()
}
func (f fun) String() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("#### " + f.name)
buf.WriteString("\n\n" + f.comment)
buf.WriteString("\n\n")
buf.WriteString("<details><summary>Code</summary>")
buf.WriteString("\n\n```go\n\n" + f.body.String() + "\n```\n\n</details>\n\n")
return buf.String()
}
func cleanComment(c string) string {
return strings.ReplaceAll(c, "// ", "")
}
func readme() {
modules := []string{
"slices", "maps", "fp",
}
buf := bufio.NewWriter(os.Stderr)
_, _ = buf.WriteString(title)
mods := make([]*mod, 0)
for _, modName := range modules {
_ = filepath.WalkDir(modName, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
if strings.Contains(path, "_test") {
return nil
}
fset := token.NewFileSet()
fmt.Printf("parsing '%s'...\n", path)
defer fmt.Println("...done")
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return nil
}
m := new(mod)
mods = append(mods, m)
// Remove the first variable declaration from the list of declarations.
for _, decl := range f.Decls {
m.title = f.Name.Name
m.description = cleanComment(f.Doc.Text())
if fn, ok := decl.(*ast.FuncDecl); ok {
if fn.Doc == nil {
// skip non-documented functions
continue
}
if fn.Recv != nil {
// skip methods
continue
}
if !fn.Name.IsExported() {
// skip local module functions
continue
}
modFun := fun{
name: fn.Name.String(),
comment: cleanComment(fn.Doc.Text()),
body: new(bytes.Buffer),
}
// remove comments from node in order to make go/format print the body without comments
fn.Doc = nil
if err = format.Node(modFun.body, fset, fn); err != nil {
fmt.Println(err)
continue
}
m.funs = append(m.funs, modFun)
}
}
return nil
})
}
for _, m := range mods {
m.CutreSort()
_, _ = buf.WriteString(m.String())
_, _ = buf.WriteString("\n\n<br/>\n\n")
}
_ = buf.Flush()
}
func main() {
readme()
}