forked from fastai/fastai
/
make_sidebar.py
executable file
·47 lines (39 loc) · 1.55 KB
/
make_sidebar.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
#!/usr/bin/env python3
# create docs/_data/sidebars/home_sidebar.yml
# from docs_src/sidebar/sidebar_data.py
#
# usage: tools/make_sidebar.py
import yaml, os, sys
from pathlib import Path
# make sure we are under the root of the project
cur_dir = Path(".").resolve().name
if (cur_dir == "tools"): os.chdir("..")
sys.path.append(str(Path("docs_src")/"sidebar"))
from sidebar_data import sidebar_d
def _leaf(k,v):
url = 'external_url' if "http" in v else 'url'
if url=='url': v=v+'.html'
return {'title':k, url:v, 'output':'web,pdf'}
_k_names = ['folders', 'folderitems', 'subfolders', 'subfolderitems']
def _side_dict(title, data, level=0):
k_name = _k_names[level]
level += 1
res = [(_side_dict(k, v, level) if isinstance(v,dict) else _leaf(k,v))
for k,v in data.items()]
return ({k_name:res} if not title
else res if title.startswith('empty')
else {'title': title, 'output':'web', k_name: res})
res = _side_dict('Sidebar', sidebar_d)
res = {'entries': [res]}
res_s = yaml.dump(res, default_flow_style=False)
res_s = res_s.replace('- subfolders:', ' subfolders:').replace(' - - ', ' - ')
res_s = """
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# Instead edit sidebar_d inside docs_src/tools/make_sidebar.ipynb,
# Then execute that notebook from the beginning to the end
# Finally commit the modified notebook and this autogenerated file
#
"""+res_s
open('docs/_data/sidebars/home_sidebar.yml', 'w').write(res_s)