-
Notifications
You must be signed in to change notification settings - Fork 18.3k
/
Copy pathgenerate_contents.py
56 lines (41 loc) · 1.56 KB
/
generate_contents.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import re
import itertools
PREV_TEMPLATE = " <[{title}]({url}) "
CONTENTS = "| [Contents](Index.ipynb)| "
NEXT_TEMPLATE = " [{title}](url) >"
NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), '..', 'notebooks')
CHAPTERS = {"00": "Preface",
"01": "IPython: Beyond Normal Python",
"02": "NumPy",
"03": "Pandas",
"04": "Matplotlib",
"05": "Machine Learning"}
REG = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')
notebooks = sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))
def prev_this_next(it):
a, b, c = itertools.tee(it,3)
next(c)
return zip(itertools.chain([None], a), b, itertools.chain(c, [None]))
def iter_navbars(notebooks):
for prev_nb, nb, next_nb in prev_this_next(notebooks):
navbar = ""
if prev_nb:
navbar += PREV_TEMPLATE.format(title=REG.match(prev_nb).groups()[2],
url=prev_nb)
navbar += CONTENTS
if next_nb:
navbar += NEXT_TEMPLATE.format(title=REG.match(next_nb).groups()[2],
url=next_nb)
yield navbar
def gen_contents(notebooks):
def get_chapter(nb):
return REG.match(nb).groups()[0]
for nb in notebooks:
chapter, section, title = REG.match(nb).groups()
title = title.replace('-', ' ')
if section == '00':
yield '\n### [{0}]({1})'.format(title, nb)
else:
yield "- [{0}]({1})".format(title, nb)
print('\n'.join(gen_contents(notebooks)))