-
Notifications
You must be signed in to change notification settings - Fork 157
/
md2json
executable file
·58 lines (52 loc) · 1.66 KB
/
md2json
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
#!/usr/bin/env python3
import json
import re
import sys
def markdown_to_json(filename, anchor):
"""Convert a Markdown file into a JSON string"""
category = ""
entries = []
link_re = re.compile('\[(.+)\]\((http.*)\)')
with open(filename) as fp:
lines = (line.rstrip() for line in fp)
lines = list(line for line in lines if line and
line.startswith(anchor) or line.startswith('| '))
for line in lines:
if line.startswith(anchor):
category = line.split(anchor)[1].strip()
continue
chunks = [x.strip() for x in line.split('|')[1:-1]]
raw_title = chunks[0]
title_re_match = link_re.match(raw_title)
if not title_re_match:
print("could not match {} to Link RegEx".format(raw_title))
sys.exit(1)
title = title_re_match.group(1)
link = title_re_match.group(2)
entry = {
'API': title,
'Description': chunks[1],
'Auth': None if chunks[2].upper() == 'NO' else chunks[2].strip('`'),
'HTTPS': True if chunks[3].upper() == 'YES' else False,
'CORS': chunks[4].strip('`').lower(),
'Link': link,
'Category': category,
}
entries.append(entry)
final = {
'count': len(entries),
'entries': entries,
}
return json.dumps(final)
def main():
num_args = len(sys.argv)
if num_args < 2:
print("No .md file passed")
sys.exit(1)
if num_args < 3:
anchor = '###'
else:
anchor = sys.argv[2]
print(markdown_to_json(sys.argv[1], anchor))
if __name__ == "__main__":
main()