forked from MarcelRaschke/http-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatter.go
30 lines (27 loc) · 1.11 KB
/
formatter.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
package docs
import "bytes"
// Formatter allows to implement generation of docs in different formats.
type Formatter interface {
GenerateIntro() string
GenerateIndex(endp []*Endpoint) string
GenerateEndpointBlock(endp *Endpoint) string
GenerateArgumentsBlock(args []*Argument, opts []*Argument) string
GenerateBodyBlock(args []*Argument) string
GenerateResponseBlock(response string) string
GenerateExampleBlock(endp *Endpoint) string
}
// GenerateDocs uses a formatter to generate documentation for every endpoint
func GenerateDocs(api []*Endpoint, formatter Formatter) string {
buf := new(bytes.Buffer)
buf.WriteString(formatter.GenerateIntro())
// In docs.ipfs.io this is handled by the TOC.
// buf.WriteString(formatter.GenerateIndex(api))
for _, endp := range api {
buf.WriteString(formatter.GenerateEndpointBlock(endp))
buf.WriteString(formatter.GenerateArgumentsBlock(endp.Arguments, endp.Options))
buf.WriteString(formatter.GenerateBodyBlock(endp.Arguments))
buf.WriteString(formatter.GenerateResponseBlock(endp.Response))
buf.WriteString(formatter.GenerateExampleBlock(endp))
}
return buf.String()
}