-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-markdown.go
258 lines (223 loc) · 5.64 KB
/
api-markdown.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package docs
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
json "github.com/pydio/cells/x/jsonx"
)
var tocTemplate = `
{{define "toc"}}
Table of Contents
{{range .Services}}
- **{{if .Title}}{{.Title}}{{else}}{{.Name}}{{end}}** {{.Short}}
{{end}}
{{end}}
`
var paramTemplate = `
{{define "parameter"}}
Name | Description | Type | Required
---|---|---|---
{{range .FirstLevel}}**{{.Name}}** | {{.Description}} | _{{.TypeName}}_ | {{if .Required}}Yes{{end}}
{{end}}
{{if .TypeExample}}
### Body Example
{{toJsonPre .TypeExample}}
{{end}}
{{end}}
`
var opTemplate = `
{{define "operation"}}
{{.Description}}
{{if .BodyParameter}}
### Body Parameters
{{template "parameter" .BodyParameter}}
{{else if .Parameters}}
### Path Parameters
{{range .Parameters}}
- **{{.Name}}** (_{{.TypeName}}{{if .Required}}, required{{end}}_) {{.Description}}
{{end}}
{{else}}
No Parameters
{{end}}
{{if .Response200}}
### Response Example (200)
Response Type {{.Response200.TypeDescription}}
{{toJsonPre .Response200.TypeExample}}
{{end}}
{{end}}
`
var oneTemplate = `
# Cells API
{{template "toc" .}}
{{range .Services}}
## {{if .Title}}{{.Title}}{{else}}{{.Name}}{{end}}
{{.Long}}
{{range .Operations}}
### [{{.Type}}] {{.Path}}
{{template "operation" .}}
{{end}}
{{end}}
###### Auto generated by {{.GeneratedBy}} on {{.GeneratedOn}}
`
var multiMain = `
[:summary]
###### Auto generated by {{.GeneratedBy}} on {{.GeneratedOn}}
`
var multiService = `
{{.Long}}
[:summary]
###### Auto generated by {{.GeneratedBy}} on {{.GeneratedOn}}
`
var multiOperation = `{{template "operation" .}}
###### Auto generated by {{.GeneratedBy}} on {{.GeneratedOn}}
`
type TplData struct {
Services []Service
GeneratedBy string
GeneratedOn string
}
type Parameter struct {
Name string
Description string
Required bool
Position string
TypeName string
TypeDescription string
FirstLevel map[string]*Parameter
TypeExample interface{}
}
type Op struct {
Type string
Path string
Description string
Parameters []*Parameter
BodyParameter *Parameter
Response200 *Parameter
GeneratedBy string
GeneratedOn string
}
type Service struct {
Name string
Title string
Short string
Long string
Operations []Op
GeneratedBy string
GeneratedOn string
}
var fMap = template.FuncMap{
"toJsonPre": func(data interface{}) string {
d, _ := json.MarshalIndent(data, "", " ")
return "```\n" + string(d) + "\n```\n"
},
}
var ApiDocsOutputRootTitle = "Rest API"
var ApiDocsOutputRootId = "2_cells_api"
var ApiDocsOutputRootWeight = 1
var ApiDocsMenuName = ""
var ApiDocsGeneratedBy = ""
var ApiDocsGeneratedOn = time.Now().Format("2-Jan-2006")
// GenOpenAPIDocs generates docs for OpenAPI in markdown format,
// ready to be pushed to pydio docs git repositories
func GenOpenAPIDocs(output string) error {
// Load Json Spec into a readable format
services := loadJsonSpec()
// Convert to TplData
tplData := &TplData{}
for name, ops := range services {
s := Service{Name: name}
if ks, ok := KnownServices[name]; ok {
s = *ks
s.Name = name
}
s.Operations = ops
sort.Slice(s.Operations, func(i, j int) bool {
return s.Operations[i].Path < s.Operations[j].Path
})
tplData.Services = append(tplData.Services, s)
}
sort.Slice(tplData.Services, func(i, j int) bool {
return tplData.Services[i].Title < tplData.Services[j].Title
})
// Feed Json Data
//return writeOnePagerMd(output, tplData)
return writeMultiPageMd(output, tplData)
}
func writeOnePagerMd(folder string, tplData *TplData) error {
tplData.GeneratedBy = ApiDocsGeneratedBy
tplData.GeneratedOn = ApiDocsGeneratedOn
return toMd(folder, "API", "api", oneTemplate, tplData)
}
func writeMultiPageMd(folder string, data *TplData) error {
if e := toMd(folder, ApiDocsOutputRootTitle, ApiDocsOutputRootId, multiMain, map[string]string{
"GeneratedBy": ApiDocsGeneratedBy,
"GeneratedOn": ApiDocsGeneratedOn,
}); e != nil {
return e
}
srvF := filepath.Join(folder, ApiDocsOutputRootId)
for _, s := range data.Services {
s.GeneratedBy = ApiDocsGeneratedBy
s.GeneratedOn = ApiDocsGeneratedOn
if e := toMd(srvF, s.Title, s.Name, multiService, s); e != nil {
return e
}
opF := filepath.Join(srvF, s.Name)
for _, o := range s.Operations {
id := o.Type + "-" + strings.Replace(o.Path, "/", "_", -1)
o.GeneratedBy = ApiDocsGeneratedBy
o.GeneratedOn = ApiDocsGeneratedOn
if e := toMd(opF, o.Type+" "+o.Path, id, multiOperation, o); e != nil {
return e
}
}
}
return nil
}
func toMd(folder, title, id, page string, data interface{}) error {
os.MkdirAll(folder, 0777)
md := filepath.Join(folder, id+".md")
wr, e := os.OpenFile(md, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if e != nil {
return e
}
defer wr.Close()
tpl := template.New("services").Funcs(fMap)
t, e := tpl.Parse(opTemplate + tocTemplate + paramTemplate + page)
if e != nil {
return e
}
e = t.Execute(wr, data)
if e != nil {
return e
}
yml := filepath.Join(folder, id+".yaml")
w, e := os.OpenFile(yml, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if e != nil {
return e
}
defer w.Close()
weight := 0
if id == ApiDocsOutputRootId {
weight = ApiDocsOutputRootWeight
}
return writeYaml(title, ApiDocsMenuName, weight, w)
}
func writeYaml(title, menu string, position int, w io.Writer) error {
buf := new(bytes.Buffer)
buf.WriteString("title: \"" + title + "\"\n")
buf.WriteString("menu: \"" + title + "\"\n")
buf.WriteString("language: und\n")
buf.WriteString("menu_name: " + menu + "\n")
if position > 0 {
buf.WriteString(fmt.Sprintf("weight: %d\n", position))
}
_, err := buf.WriteTo(w)
return err
}