-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathformatter.go
More file actions
54 lines (48 loc) · 2.22 KB
/
formatter.go
File metadata and controls
54 lines (48 loc) · 2.22 KB
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
package docs
import (
"bytes"
cmds "github.com/ipfs/go-ipfs-cmds"
)
// Formatter allows to implement generation of docs in different formats.
type Formatter interface {
GenerateIntro() string
// GenerateGlobalOptionsBlock documents global options that apply to all
// endpoints. queryOpts are passed as URL query parameters; authOpt (if
// non-nil) is the authentication option sent via HTTP header instead.
GenerateGlobalOptionsBlock(queryOpts []*Argument, authOpt *Argument) string
GenerateStatusIntro(status cmds.Status) string
GenerateIndex(endp []*Endpoint) string
GenerateEndpointBlock(endp *Endpoint) string
GenerateArgumentsBlock(args []*Argument, opts []*Argument) string
GenerateBodyBlock(args []*Argument) string
GenerateResponseBlock(response string, contentType string) string
GenerateExampleBlock(endp *Endpoint) string
}
// GenerateDocs uses a formatter to generate documentation for every endpoint.
// It first emits the intro and global options sections, then iterates through
// endpoints grouped by status (active, experimental, deprecated, removed).
func GenerateDocs(api []*Endpoint, formatter Formatter) string {
buf := new(bytes.Buffer)
buf.WriteString(formatter.GenerateIntro())
// Document global options from the root command before per-endpoint docs.
// These are flags like --offline, --timeout, --encoding that apply to
// every RPC endpoint but were previously missing from the reference.
// See https://github.com/ipfs/ipfs-docs/issues/1084
queryOpts, authOpt := GlobalOptions()
buf.WriteString(formatter.GenerateGlobalOptionsBlock(queryOpts, authOpt))
for _, status := range []cmds.Status{cmds.Active, cmds.Experimental, cmds.Deprecated, cmds.Removed} {
endpoints := InStatus(api, status)
if len(endpoints) == 0 {
continue
}
buf.WriteString(formatter.GenerateStatusIntro(status))
for _, endp := range endpoints {
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, endp.ResponseContentType))
buf.WriteString(formatter.GenerateExampleBlock(endp))
}
}
return buf.String()
}