forked from jackdoe/programming-for-kids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.py
37 lines (29 loc) · 937 Bytes
/
toc.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
36
37
from os import listdir
from os.path import isfile
import re
toc = open("toc.md", "w")
day = 0
dir = listdir('.')
dir.sort()
def sanitize(s):
return s.replace(',','').replace('/','').replace('; ','-').replace(' ','-')
for fn in dir:
if isfile(fn) and fn.endswith('.md') and fn.startswith('week-0'):
m = re.match("week-(\d+)\.md", fn)
if m:
week = m.group(1)
else:
print("skipping ", fn)
continue
print("extracting",fn)
f = open(fn, encoding="utf8", mode="r")
lines = f.readlines()
f.close()
toc.write("## week - " + week + "\n")
toc.write('\n\n')
for line in lines:
line = line[:-1]
if line.startswith('## [DAY-'):
clear = line.lower().replace('## ','').replace('[','').replace(']','')
toc.write('\n['+clear+'](#'+sanitize(clear)+')\n')
toc.close()