-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmless.py
35 lines (28 loc) · 1.14 KB
/
cmless.py
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
import re
from os import path
def parse_cmless(md):
md = md.replace('’', "'") # Replace curly apostrophes with straight ones
md = md.replace('\\_', '_') # Replace escaped underscores with plain ones
md = re.sub(
r"(?<!\n)\n(?!\\n)", " ", md
) # Replace newlines that aren't escaped with spaces
pattern = r"## (.*?)\n(.*?)(?=## |\Z)"
matches = re.findall(pattern, md, re.DOTALL)
results = {key.strip(): value.strip() for key, value in matches}
results['Name'] = re.search(r"^# (.*)", md).group(1).strip()
return results
if __name__ == "__main__":
directory = 'app/views/organizations/md'
from os import listdir
results = {}
files = listdir(directory)
for path in files:
with open(f"{directory}/{path}", 'r') as f:
markdown_text = f.read()
parsed = parse_cmless(markdown_text)
org_id = path.split('.')[0]
results[org_id] = parsed
with open('orgs.json', 'w', encoding='utf8') as f:
from json import dump
# f.write(dumps(results, indent=2, ensure_ascii=False).encode('utf-8'))
dump(results, f, indent=2, ensure_ascii=False)