-
Notifications
You must be signed in to change notification settings - Fork 457
/
gentoc.py
81 lines (67 loc) · 2.51 KB
/
gentoc.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Generate table of contents with links to nbviewer."""
import re
import urlparse
import json
import os
import sys
import os.path as op
from util import get_recipe_number, get_recipe_name
CHAPTER_NAMES = [
'A Tour of Interactive Computing with IPython',
'Best practices in Interactive Computing',
'Master the Notebook',
'Profiling and Optimization',
'High-Performance Computing',
'Advanced Visualization',
'Statistical Data Analysis',
'Machine Learning',
'Numerical Optimization',
'Signal Processing',
'Image and Audio Processing',
'Deterministic Dynamical Systems',
'Stochastic Dynamical Systems',
'Graphs and Geometry',
'Symbolic and Numerical Mathematics',
]
def get_chapter_number(dir):
return int(op.basename(dir)[7:9])
def get_chapter_name(number):
return CHAPTER_NAMES[number-1]
def iter_chapters(root):
for chapter in sorted([_ for _ in os.listdir(root)
if _.startswith('chapter')]):
yield chapter
def iter_recipes(root, dir):
files = sorted([_ for _ in os.listdir(op.join(root, dir))
if re.match(r'\d{2}\_[^.]+\.ipynb', _)])
for file in files:
yield file
def yield_chapter_toc(root):
for chapter in iter_chapters(root):
number = get_chapter_number(chapter)
chapter_name = get_chapter_name(number)
yield chapter, '### Chapter {number}: {name}\n\n'.format(number=number,
name=chapter_name)
def yield_recipe_toc(root, chapter):
for recipe in iter_recipes(root, chapter):
file = op.join(root, chapter, recipe)
recipe_name = get_recipe_name(file)
url = urlparse.urljoin(urlbase, chapter + '/' + recipe)
yield '* [{recipe_name}]({url})\n'.format(
recipe_name=recipe_name,
url=url,
)
curdir = op.realpath(op.dirname(os.path.abspath(__file__)))
root = op.realpath(op.join(curdir, '../notebooks'))
urlbase = "http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/"
def write_toc(root, f=sys.stdout):
with f:
for chapter, chapter_toc in yield_chapter_toc(root):
f.write(chapter_toc)
for recipe_toc in yield_recipe_toc(root, chapter):
f.write(recipe_toc)
f.write('\n\n')
if __name__ == '__main__':
write_toc(root, open('toc.md', 'w'))
# for chapter, chapter_toc in yield_chapter_toc(root):
# print(chapter_toc)