forked from jeffmikels/ProPresenter-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml2md
executable file
·158 lines (129 loc) · 3.85 KB
/
yaml2md
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
#!/usr/bin/env python3
import sys
import yaml
output = ''
def e(s='', end='\n'):
global output
output += s + end
def format_type(ttype):
if ttype not in ['bool', 'int', 'float', 'string', 'object', 'list']:
if type(ttype) is list:
retval = []
for t in ttype:
retval.append(format_type(t))
return '|'.join(retval)
if ttype in types:
link_to = ttype.lower()
ttype = f'[`{ttype}`](#{link_to})'
if 'list[' in ttype:
inner_type = ttype.replace('list[', '')[:-1]
link_to = inner_type.lower()
ttype = f'list[[`{inner_type}`](#{link_to})]'
else:
ttype = f'`{ttype}`'
return ttype
if len(sys.argv) == 1:
print(f'''
USAGE:
{sys.argv[0]} yaml_file output_name
Will parse an API yaml file to Markdown.
''')
exit()
source = sys.argv[1]
target = source + '.md'
target = target.replace('.yaml.md', '.md')
target = target.replace('.yml.md', '.md')
if len(sys.argv) == 3:
target = sys.argv[2]
# read input yaml file
with open(source, 'r') as f:
t = f.read()
data = yaml.load(t, Loader=yaml.CLoader)
# handle metadata
e(f'# {data["application"]}')
e()
e(f'{data["description"]}')
# handle channels
for channelkey in data['channels']:
channel = data['channels'][channelkey]
name = channel.get('name', channelkey).strip()
desc = channel['description'].strip()
e(f'\n## {name}')
e()
e(desc)
types = channel.get('types', {})
messages = channel.get('messages', {})
actions = channel.get('actions', {})
undocumented = channel.get('undocumented', {})
# output the typed data definitions
e(f'\n### {name} Types')
for typekey in types:
t = types[typekey]
tname = t.get('name', typekey).strip()
ttype = t['type']
tdesc = t.get('description', '').strip()
texample = t.get('example', '').strip()
formatted_type = format_type(ttype)
e(f'\n#### <a name="{tname.lower()}">{tname}</a>')
e(f'Base Type: {formatted_type}')
e()
if tdesc != '':
e(tdesc)
e()
if ttype == 'object':
# print(t)
e('##### PARAMETERS:\n')
for paramname in t['parameters']:
param = t['parameters'][paramname]
pdesc = f'always `{param}`'
pexample = ''
poptions = None
if type(param) is str:
ptype = 'string'
elif type(param) is int:
ptype = 'int'
elif type(param) is float:
ptype = 'float'
elif type(param) is bool:
ptype = 'bool'
elif type(param) is list:
ptype = 'list'
poptions = param
else:
ptype = param.get('type', 'string')
pdesc = param.get('description', '')
poptions = param.get('options', None)
pexample = param.get('example', '')
if ptype == 'union':
ptype = param.get('types', 'union')
pformatted_type = format_type(ptype)
e(f'- **{paramname}** ({pformatted_type})')
extras = []
if (poptions is not None):
poptions = [f'`{x}`' for x in poptions]
extras.append(f' Must be one of these: {poptions}')
if (pdesc != ''):
# need to indent every description line
for line in pdesc.split('\n'):
extras.append(f' {line}')
if (pexample != ''):
if ptype == 'string':
pexample = f'"{pexample}"'
if '`' not in pexample:
pexample = f'```{pexample}```'
extras.append(f' {pexample}')
if len(extras) > 0:
e()
e('\n'.join(extras))
e()
if texample != '':
if '`' not in texample:
texample = f'```{texample}```'
e(f'\n##### EXAMPLE:\n')
e(texample)
e()
e('********************************\n')
# output the list of pushy messages (with anchor links)
# output the action messages with their responses
# output the response-only messages
print(output)