/
markdown.go
198 lines (153 loc) · 7.07 KB
/
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
package doc
import (
"text/template"
)
var markdownTemplate *template.Template
func init() {
markdownTemplate = template.Must(template.New("MarkdownTemplate").Funcs(templateFuncMap).Parse(markdownTemplateText))
}
const markdownTemplateText = `
{{ define "CRDSummary" -}}
{{ $input := . -}}
# Custom Resources
{{ range $group := $input.Groups -}}
## {{ $group }}
{{ $groupVersions := index $input.GroupVersions $group -}}
{{ range $gv := $groupVersions -}}
### {{ $gv.Version }}
{{ $kinds := index $input.Kinds $gv -}}
{{ range $kind := $kinds -}}
{{ $schema := index $input.Schemas $kind -}}
[{{ $kind.Kind }}]({{ fileForKind $kind "md" }})
: {{ $schema.Description }}
{{ end }}
{{- end }}
{{- end }}
{{- end }}
{{- define "Type" }}
# {{ .Name }}
Used by [{{ .Kind.Kind }}({{ .Kind.Group }}/{{ .Kind.Version }})]({{ fileForKind .Kind "md" }})
{{ template "SchemaDetails" dict "Root" .Root "Schema" .Schema "Name" .Name "Kind" .Kind "Locations" .Locations "Seen" .Seen "Level" 1 "Depth" .Depth }}
{{- end }}
{{- define "Kind" }}
# {{ .Kind.Kind }} - {{ .Kind.Group }}/{{ .Kind.Version }}
{{ template "SchemaDetails" dict "Root" .Root "Schema" .Schema "Name" .Kind.Kind "Kind" .Kind "Locations" .Locations "Seen" .Seen "Level" 1 "Depth" .Depth }}
{{- end }}
{{ define "SchemaDetails" -}}
{{ packageForSchema .Schema }}
{{ if .Schema.Description -}}
{{ .Schema.Description }}
{{ end -}}
{{ $input := . -}}
{{ if (or .Schema.Properties .Schema.AllOf) -}}
| Field | Description | Type |
|------ | ----------- | ---- |
{{ template "TableRows" dict "Schema" $input.Schema "Root" $input.Root "Kind" $input.Kind "Locations" $input.Locations "External" (eq $input.Level $input.Depth) -}}
{{ else -}}
Type: {{ template "FieldType" dict "Field" .Schema "Kind" $input.Kind "Root" $input.Root "Locations" $input.Locations "External" (eq $input.Level $input.Depth) -}}
{{- end }}
{{ template "NestedProperties" dict "Root" $input.Root "Schema" $input.Schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $input.Level "Depth" $input.Depth -}}
{{ end -}}
{{ define "NestedProperties" -}}
{{ $input := . -}}
{{ if or (eq .Depth 0) (lt .Level .Depth) -}}
{{ range $embeddedSchema := .Schema.AllOf -}}
{{ template "NestedProperties" dict "Root" $input.Root "Schema" (resolveSchema $input.Root $embeddedSchema) "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $input.Level "Depth" $input.Depth -}}
{{ end -}}
{{ $level := incInt .Level -}}
{{ range $name, $schema := .Schema.Properties -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" $schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $level "Depth" $input.Depth -}}
{{ end -}}
{{ if .Schema.AdditionalProperties -}}
{{ if .Schema.AdditionalProperties.Schema -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" .Schema.AdditionalProperties.Schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $level "Depth" $input.Depth -}}
{{ end -}}
{{ end -}}
{{ if .Schema.Items -}}
{{ if .Schema.Items.Schema -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" .Schema.Items.Schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $level "Depth" $input.Depth -}}
{{ else -}}
{{ range $itemType := .Schema.Items.JSONSchemas -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" $itemType "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $level "Depth" $input.Depth -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ define "NestedProperty" -}}
{{ $input := . -}}
{{ if .Schema.Ref -}}
{{ if not ($input.Seen.Has .Schema.Ref) -}}
{{ $unused := $input.Seen.Insert .Schema.Ref -}}
{{ if eq (packageForSchema $input.Root) (packageForSchema (resolveSchema $input.Root .Schema)) -}}
## {{ refName .Schema.Ref }}
{{ template "SchemaDetails" dict "Root" $input.Root "Schema" (resolveSchema $input.Root .Schema) "Name" (refName .Schema.Ref) "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" .Level "Depth" $input.Depth -}}
{{ end -}}
{{ end -}}
{{ else -}}
{{ if .Schema.AdditionalProperties -}}
{{ if .Schema.AdditionalProperties.Schema -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" .Schema.AdditionalProperties.Schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $input.Level "Depth" $input.Depth -}}
{{ end -}}
{{ end -}}
{{ if .Schema.Items -}}
{{ if .Schema.Items.Schema -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" .Schema.Items.Schema "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $input.Level "Depth" $input.Depth -}}
{{ else -}}
{{ range $itemType := .Schema.Items.JSONSchemas -}}
{{ template "NestedProperty" dict "Root" $input.Root "Schema" $itemType "Kind" $input.Kind "Locations" $input.Locations "Seen" $input.Seen "Level" $input.Level "Depth" $input.Depth -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ define "TableRows" -}}
{{ $input := . -}}
{{ range $inline := .Schema.AllOf -}}
{{ template "TableRows" dict "Schema" (resolveSchema $input.Root $inline) "Root" $input.Root "Kind" $input.Kind "Locations" $input.Locations "External" $input.External -}}
{{ end -}}
{{ range $name, $property := .Schema.Properties -}}
| {{ $name }} | {{ $property.Description }} | {{ template "FieldType" dict "Field" $property "Kind" $input.Kind "Root" $input.Root "Locations" $input.Locations "External" $input.External }} |
{{ end -}}
{{ end -}}
{{ define "FieldType" -}}
{{ $input := . -}}
{{ if .Field.Ref -}}
{{ if .External -}}
{{ $location := index .Locations (resolveSchema .Root .Field).ID -}}
{{ if $location -}}
[{{ refName .Field.Ref }}]({{ $location }})
{{- else -}}
{{ refName .Field.Ref -}}
{{ end -}}
{{- else -}}
[{{ refName .Field.Ref }}](#{{ refName .Field.Ref }})
{{- end -}}
{{ else -}}
{{ if eq .Field.Type "array" -}}
{{ if .Field.Items.Schema -}}
[]{{ template "FieldType" dict "Field" .Field.Items.Schema "Kind" $input.Kind "Root" $input.Root "Locations" $input.Locations "External" .External -}}
{{ else -}}
{{ $input := . -}}
{{ range $itemType := .Field.Items.JSONSchemas -}}
[]{{ template "FieldType" dict "Field" $itemType "Kind" $input.Kind "Root" $input.Root "Locations" $input.Locations "External" $input.External -}}
{{ end -}}
{{ end -}}
{{ else -}}
{{ if eq .Field.Type "object" -}}
{{ if .Field.AdditionalProperties -}}
{{ if .Field.AdditionalProperties.Schema -}}
map[string]{{ template "FieldType" dict "Field" .Field.AdditionalProperties.Schema "Kind" $input.Kind "Root" $input.Root "Locations" $input.Locations "External" .External -}}
{{- else -}}
object
{{- end -}}
{{ else -}}
object
{{- end -}}
{{ else -}}
{{ .Field.Type -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end }}
`