Skip to content

Commit 7ed353d

Browse files
committed
Add tools for generating contents and navigation
1 parent b9142b7 commit 7ed353d

File tree

3 files changed

+83
-31
lines changed

3 files changed

+83
-31
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
92+
# Emacs
93+
*~

tools/add_navigation.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
import itertools
3+
4+
from ipykernel import kernelspec as ks
5+
import nbformat
6+
from nbformat.v4.nbbase import new_markdown_cell
7+
8+
from generate_contents import NOTEBOOK_DIR, REG, iter_notebooks, get_notebook_title
9+
10+
11+
def prev_this_next(it):
12+
a, b, c = itertools.tee(it,3)
13+
next(c)
14+
return zip(itertools.chain([None], a), b, itertools.chain(c, [None]))
15+
16+
17+
PREV_TEMPLATE = "< [{title}]({url}) "
18+
CONTENTS = "| [Contents](Index.ipynb) |"
19+
NEXT_TEMPLATE = " [{title}]({url}) >"
20+
NAV_COMMENT = "<!--NAVIGATION-->\n"
21+
22+
23+
def iter_navbars():
24+
for prev_nb, nb, next_nb in prev_this_next(iter_notebooks()):
25+
navbar = NAV_COMMENT
26+
if prev_nb:
27+
navbar += PREV_TEMPLATE.format(title=get_notebook_title(prev_nb),
28+
url=prev_nb)
29+
navbar += CONTENTS
30+
if next_nb:
31+
navbar += NEXT_TEMPLATE.format(title=get_notebook_title(next_nb),
32+
url=next_nb)
33+
yield os.path.join(NOTEBOOK_DIR, nb), navbar
34+
35+
36+
def write_navbars():
37+
for nb_name, navbar in iter_navbars():
38+
nb = nbformat.read(nb_name, as_version=4)
39+
is_comment = lambda cell: cell.source.startswith(NAV_COMMENT)
40+
41+
if is_comment(nb.cells[1]):
42+
nb.cells[1].source = navbar
43+
else:
44+
nb.cells.insert(1, new_markdown_cell(source=navbar))
45+
46+
if is_comment(nb.cells[-1]):
47+
nb.cells[-1].source = navbar
48+
else:
49+
nb.cells.append(new_markdown_cell(source=navbar))
50+
nbformat.write(nb, nb_name)
51+
52+
53+
if __name__ == '__main__':
54+
write_navbars()

tools/generate_contents.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import os
22
import re
33
import itertools
4-
5-
PREV_TEMPLATE = " <[{title}]({url}) "
6-
CONTENTS = "| [Contents](Index.ipynb)| "
7-
NEXT_TEMPLATE = " [{title}](url) >"
4+
import nbformat
85

96
NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), '..', 'notebooks')
107

@@ -17,40 +14,37 @@
1714

1815
REG = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')
1916

20-
notebooks = sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))
21-
22-
def prev_this_next(it):
23-
a, b, c = itertools.tee(it,3)
24-
next(c)
25-
return zip(itertools.chain([None], a), b, itertools.chain(c, [None]))
26-
2717

18+
def iter_notebooks():
19+
return sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))
2820

2921

30-
def iter_navbars(notebooks):
31-
for prev_nb, nb, next_nb in prev_this_next(notebooks):
32-
navbar = ""
33-
if prev_nb:
34-
navbar += PREV_TEMPLATE.format(title=REG.match(prev_nb).groups()[2],
35-
url=prev_nb)
36-
navbar += CONTENTS
37-
if next_nb:
38-
navbar += NEXT_TEMPLATE.format(title=REG.match(next_nb).groups()[2],
39-
url=next_nb)
40-
yield navbar
22+
def get_notebook_title(nb_file):
23+
nb = nbformat.read(os.path.join(NOTEBOOK_DIR, nb_file), as_version=4)
24+
for cell in nb.cells:
25+
if cell.source.startswith('#'):
26+
return cell.source[1:].splitlines()[0].strip()
4127

4228

43-
def gen_contents(notebooks):
44-
def get_chapter(nb):
45-
return REG.match(nb).groups()[0]
46-
47-
for nb in notebooks:
29+
def gen_contents(directory=None):
30+
for nb in iter_notebooks():
31+
if directory:
32+
nb_url = os.path.join(directory, nb)
33+
else:
34+
nb_url = nb
4835
chapter, section, title = REG.match(nb).groups()
49-
title = title.replace('-', ' ')
36+
title = get_notebook_title(nb)
5037
if section == '00':
51-
yield '\n### [{0}]({1})'.format(title, nb)
38+
yield '\n### [{0}]({1})'.format(title, nb_url)
5239
else:
53-
yield "- [{0}]({1})".format(title, nb)
40+
yield "- [{0}]({1})".format(title, nb_url)
41+
42+
43+
def print_contents(directory=None):
44+
print('\n'.join(gen_contents(directory)))
5445

5546

56-
print('\n'.join(gen_contents(notebooks)))
47+
if __name__ == '__main__':
48+
print_contents()
49+
print(70 * '#')
50+
print_contents('notebooks')

0 commit comments

Comments
 (0)