-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_doc.go
155 lines (140 loc) · 3.16 KB
/
gen_doc.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
package main
import (
"flag"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
)
type Version string
type blockletField struct {
Name, DataType string
Doc string
Since Version
}
type BlockletDoc struct {
FileName string
Name string
Doc string
ConfigFields []blockletField
Since Version
}
type Template struct {
Blocklets []BlockletDoc
}
var pascalRe = regexp.MustCompile(`\B[A-Z]`)
func structNameToBlockletName(s string) string {
if strings.HasSuffix(s, "BlockConfig") {
s = strings.TrimSuffix(s, "BlockConfig")
} else {
s = strings.TrimSuffix(s, "Config")
}
s = pascalRe.ReplaceAllStringFunc(s, func(m string) string {
return "_" + m
})
return strings.ToLower(s)
}
func getFieldTypeName(e ast.Expr) string {
if ee, ok := e.(*ast.Ident); ok {
return ee.Name
}
if ee, ok := e.(*ast.StarExpr); ok {
return ee.X.(*ast.Ident).Name
}
return ""
}
func escapeTableDelimiters(s string) string {
return strings.ReplaceAll(s, "|", "\\|")
}
var tagRe = regexp.MustCompile(`yaml:"([^"]+)"`)
func tagToFieldName(tag string) string {
m := tagRe.FindStringSubmatch(tag)
if len(m) > 1 {
return m[1]
}
return ""
}
func main() {
tmplFileFlag := flag.String("t", "doc/template.md", "Markdown file with template")
outFileFlag := flag.String("o", "README.md", "Output file")
flag.Parse()
fSet := token.NewFileSet()
blocklets := make([]BlockletDoc, 0)
for _, p := range flag.Args() {
log.Printf("Processing %s\n", p)
astFile, err := parser.ParseFile(fSet, p, nil, parser.ParseComments)
if err != nil {
log.Print(err)
continue
}
pkg, err := doc.NewFromFiles(fSet, []*ast.File{astFile}, "")
if err != nil {
log.Print(err)
continue
}
var cfgTyp *doc.Type
for _, typ := range pkg.Types {
if strings.HasSuffix(typ.Name, "Config") {
cfgTyp = typ
break
}
}
if cfgTyp == nil {
continue
}
fields := make([]blockletField, 0)
st := cfgTyp.Decl.Specs[0].(*ast.TypeSpec).Type.(*ast.StructType)
for _, f := range st.Fields.List {
if f.Names == nil {
continue
}
var dataType string
if t, ok := f.Type.(*ast.StarExpr); ok {
dataType = t.X.(*ast.Ident).String()
} else if t, ok := f.Type.(*ast.Ident); ok {
dataType = t.Name
} else {
continue
}
fieldName := tagToFieldName(f.Tag.Value)
fields = append(fields, blockletField{
Name: fieldName,
DataType: dataType,
Doc: strings.TrimSpace(f.Doc.Text()),
})
}
blocklets = append(blocklets, BlockletDoc{
Name: structNameToBlockletName(cfgTyp.Name),
FileName: p,
Doc: cfgTyp.Doc,
ConfigFields: fields,
})
}
var outFile *os.File
if *outFileFlag == "-" {
outFile = os.Stdout
} else {
f, err := os.Create(*outFileFlag)
if err != nil {
panic(err)
}
defer f.Close()
outFile = f
}
tmplData := Template{blocklets}
funcMap := template.FuncMap{
"escapeTableDelimiters": escapeTableDelimiters,
}
t := template.Must(
template.New(filepath.Base(*tmplFileFlag)).Funcs(funcMap).ParseFiles(*tmplFileFlag),
)
if err := t.Execute(outFile, tmplData); err != nil {
panic(err)
}
}