-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_toc.py
executable file
·92 lines (69 loc) · 1.95 KB
/
generate_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
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
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
from glob import glob
import yaml
import jinja2
import os
# All the posts, in reverse chronological order
posts = sorted(glob('posts/*'), reverse=True)
posts = [os.path.basename(p) for p in posts]
posts = [os.path.splitext(p)[0] for p in posts]
# Grouped by year
posts_by_year = {}
for p in posts:
year = p[:4]
if year not in posts_by_year:
posts_by_year[year] = []
posts_by_year[year].append(p)
# Get the tags from meta.yaml
with open('post_meta.yaml') as f:
meta = yaml.safe_load(f)
# Grouped by tags
posts_by_tag = {}
for p, m in meta.items():
if p in posts and 'tags' in m:
for t in m['tags']:
if t not in posts_by_tag:
posts_by_tag[t] = []
posts_by_tag[t].append(p)
if not os.path.exists('contents'):
os.mkdir('contents')
subtree_template = jinja2.Template("""
{{title}}
--------------------------------------------------------------------------------
.. toctree::
:caption: {{title}}
:maxdepth: 1
{% for p in posts -%}
/posts/{{p}}
{% endfor %}
""")
for y, ps in posts_by_year.items():
with open(f'contents/year-{y}.rst', 'w') as f:
f.write(subtree_template.render(title=y, posts=ps))
for t, ps in posts_by_tag.items():
with open(f'contents/tag-{t}.rst', 'w') as f:
f.write(subtree_template.render(title=t, posts=ps))
contents_template = jinja2.Template("""
.. toctree::
:caption: Latest
:maxdepth: 1
{% for p in posts[:5] -%}
/posts/{{p}}
{% endfor %}
.. toctree::
:caption: Tags
:maxdepth: 2
:hidden:
{% for t in tags -%}
/contents/tag-{{t}}
{% endfor %}
.. toctree::
:caption: Archive
:maxdepth: 2
:hidden:
{% for y in years -%}
/contents/year-{{y}}
{% endfor %}
""")
with open('contents/contents.rst', 'w') as f:
f.write(contents_template.render(posts=posts, tags=sorted(posts_by_tag.keys()), years=sorted(posts_by_year.keys(), reverse=True)))