/
markup.go
250 lines (227 loc) · 8.19 KB
/
markup.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
package markup
import (
"bytes"
"fmt"
"os"
"path"
"sort"
"strings"
"github.com/316014408/swagger/parser"
)
const (
color_NORMAL_TEXT = "black"
color_API_SECTION_HEADER_TEXT = "red"
color_MODEL_TEXT = "orange"
color_NORMAL_BACKGROUND = "white"
color_GET = "cyan"
color_POST = "green"
color_PUT = "orange"
color_DELETE = "cyan"
color_PATCH = "purple"
color_DEFAULT = "yellow"
)
type Markup interface {
sectionHeader(level int, text string) string
bulletedItem(level int, text string) string
numberedItem(level int, text string) string
anchor(anchorName string) string
link(anchorName, linkText string) string
tableHeader(tableTitle string) string
tableHeaderRow(args ...string) string
tableRow(args ...string) string
tableFooter() string
colorSpan(content, foregroundColor, backgroundColor string) string
}
func GenerateMarkup(parser *parser.Parser, markup Markup, outputSpec *string, defaultFileExtension string, tableContents bool, models bool) error {
var filename string
if *outputSpec == "" {
filename = path.Join("./", "API") + defaultFileExtension
} else {
filename = path.Join(*outputSpec)
}
fd, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Can not create document file: %v\n", err)
}
defer fd.Close()
var buf bytes.Buffer
/***************************************************************
* Overall API
***************************************************************/
buf.WriteString(markup.sectionHeader(1, parser.Listing.Infos.Title))
buf.WriteString(fmt.Sprintf("%s\n\n", parser.Listing.Infos.Description))
/***************************************************************
* Table of Contents (List of Sub-APIs)
***************************************************************/
if tableContents {
buf.WriteString("Table of Contents\n\n")
subApiKeys, subApiKeyIndex := alphabeticalKeysOfSubApis(parser.Listing.Apis)
for _, subApiKey := range subApiKeys {
buf.WriteString(markup.numberedItem(1, markup.link(subApiKey, parser.Listing.Apis[subApiKeyIndex[subApiKey]].Description)))
}
buf.WriteString("\n")
}
for _, apiKey := range alphabeticalKeysOfApiDeclaration(parser.TopLevelApis) {
apiDescription := parser.TopLevelApis[apiKey]
/***************************************************************
* Sub-API Specifications
***************************************************************/
if tableContents {
buf.WriteString(markup.anchor(apiKey))
}
buf.WriteString(markup.sectionHeader(2, markup.colorSpan(apiKey, color_API_SECTION_HEADER_TEXT, color_NORMAL_BACKGROUND)))
buf.WriteString(markup.tableHeader(""))
buf.WriteString(markup.tableHeaderRow("Specification", "Value"))
buf.WriteString(markup.tableRow("Resource Path", apiDescription.ResourcePath))
buf.WriteString(markup.tableRow("API Version", apiDescription.ApiVersion))
buf.WriteString(markup.tableRow("BasePath for the API", apiDescription.BasePath))
buf.WriteString(markup.tableRow("Consumes", strings.Join(apiDescription.Consumes, ", ")))
buf.WriteString(markup.tableRow("Produces", strings.Join(apiDescription.Produces, ", ")))
buf.WriteString(markup.tableFooter())
/***************************************************************
* Sub-API Operations (Summary)
***************************************************************/
buf.WriteString("\n")
buf.WriteString(markup.sectionHeader(3, "Operations"))
buf.WriteString("\n")
buf.WriteString(markup.tableHeader(""))
buf.WriteString(markup.tableHeaderRow("Resource Path", "Operation", "Description"))
for _, subapi := range apiDescription.Apis {
for _, op := range subapi.Operations {
pathString := strings.Replace(strings.Replace(subapi.Path, "{", "\\{", -1), "}", "\\}", -1)
buf.WriteString(markup.tableRow(pathString, markup.link(op.Nickname, op.HttpMethod), op.Summary))
}
}
buf.WriteString(markup.tableFooter())
buf.WriteString("\n")
/***************************************************************
* Sub-API Operations (Details)
***************************************************************/
for _, subapi := range apiDescription.Apis {
for _, op := range subapi.Operations {
buf.WriteString("\n")
operationString := fmt.Sprintf("%s (%s)", strings.Replace(strings.Replace(subapi.Path, "{", "\\{", -1), "}", "\\}", -1), op.HttpMethod)
if tableContents {
buf.WriteString(markup.anchor(op.Nickname))
}
buf.WriteString(markup.sectionHeader(4, markup.colorSpan("API: "+operationString, color_NORMAL_TEXT, operationColor(op.HttpMethod))))
buf.WriteString("\n\n" + op.Summary + "\n\n\n")
if len(op.Parameters) > 0 {
buf.WriteString(markup.tableHeader(""))
buf.WriteString(markup.tableHeaderRow("Param Name", "Param Type", "Data Type", "Description", "Required?"))
for _, param := range op.Parameters {
isRequired := ""
if param.Required {
isRequired = "Yes"
}
buf.WriteString(markup.tableRow(param.Name, param.ParamType, modelText(markup, param.DataType), param.Description, isRequired))
}
buf.WriteString(markup.tableFooter())
}
if len(op.ResponseMessages) > 0 {
buf.WriteString(markup.tableHeader(""))
buf.WriteString(markup.tableHeaderRow("Code", "Type", "Model", "Message"))
for _, msg := range op.ResponseMessages {
buf.WriteString(markup.tableRow(fmt.Sprintf("%v", msg.Code), msg.ResponseType, modelText(markup, msg.ResponseModel), msg.Message))
}
buf.WriteString(markup.tableFooter())
}
}
}
buf.WriteString("\n")
/***************************************************************
* Models
***************************************************************/
if len(apiDescription.Models) > 0 && models {
buf.WriteString("\n")
buf.WriteString(markup.sectionHeader(3, "Models"))
buf.WriteString("\n")
for _, modelKey := range alphabeticalKeysOfModels(apiDescription.Models) {
model := apiDescription.Models[modelKey]
if tableContents {
buf.WriteString(markup.anchor(modelKey))
}
buf.WriteString(markup.sectionHeader(4, markup.colorSpan(shortModelName(modelKey), color_MODEL_TEXT, color_NORMAL_BACKGROUND)))
buf.WriteString(markup.tableHeader(""))
buf.WriteString(markup.tableHeaderRow("Field Name (alphabetical)", "Field Type", "Description"))
for _, fieldName := range alphabeticalKeysOfFields(model.Properties) {
fieldProps := model.Properties[fieldName]
buf.WriteString(markup.tableRow(fieldName, fieldProps.Type, fieldProps.Description))
}
buf.WriteString(markup.tableFooter())
}
buf.WriteString("\n")
}
}
fd.WriteString(buf.String())
return nil
}
func shortModelName(longModelName string) string {
parts := strings.Split(longModelName, ".")
return parts[len(parts)-1]
}
func modelText(markup Markup, fullyQualifiedModelName string) string {
shortName := shortModelName(fullyQualifiedModelName)
result := shortName
if fullyQualifiedModelName != shortName {
result = markup.link(fullyQualifiedModelName, shortName)
}
return result
}
func alphabeticalKeysOfSubApis(refs []*parser.ApiRef) ([]string, map[string]int) {
index := map[string]int{}
keys := make([]string, len(refs))
for i, ref := range refs {
subApiKey := ref.Path[1:]
keys[i] = subApiKey
index[subApiKey] = i
}
sort.Strings(keys)
return keys, index
}
func alphabeticalKeysOfApiDeclaration(m map[string]*parser.ApiDeclaration) []string {
keys := make([]string, len(m))
i := 0
for key, _ := range m {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
func alphabeticalKeysOfModels(m map[string]*parser.Model) []string {
keys := make([]string, len(m))
i := 0
for key, _ := range m {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
func alphabeticalKeysOfFields(m map[string]*parser.ModelProperty) []string {
keys := make([]string, len(m))
i := 0
for key, _ := range m {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
func operationColor(methodName string) string {
switch methodName {
case "GET":
return color_GET
case "POST":
return color_POST
case "PUT":
return color_PUT
case "DELETE":
return color_DELETE
case "PATCH":
return color_PATCH
default:
return color_DEFAULT
}
}