-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_navbar.py
119 lines (101 loc) · 4.22 KB
/
create_navbar.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# %% ######################################################################
import os
import json
# %% ######################################################################
def generate_navbar_html():
"""Function to generate the navbar from the structure specified
in the index.json file"""
indent = '\t\t'
collapse_button = \
f'\n{indent}\t' \
'<svg class="collapse-icon" ' \
'viewBox="0 0 16 16" ' \
'xmlns="http://www.w3.org/2000/svg" ' \
'viewBox="0 0 16 16"' \
'>' \
f'\n{indent}{indent}' \
'<path d="M9 9H4v1h5V9z"/>' \
f'\n{indent}{indent}' \
'<path fill-rule="evenodd" ' \
'clip-rule="evenodd" ' \
'd="M5 3l1-1h7l1 1v7l-1 1h-2v2l-1 1H3l-1-1V6l1-1h2V3zm1 2h4l1 ' \
'1v4h2V3H6v2zm4 1H3v7h7V6z"/>' \
f'\n{indent}\t</svg>' \
html = \
'\t<div id="mySidebar" class="sidebar">' + \
f'\n{indent}<div class="navbar-header">' + \
f'\n{indent}<a>' + \
f'\n\t{indent}Human Neocortical Neurosolver' + \
f'\n{indent}</a>\n{indent}<br>' + \
collapse_button + \
f'\n{indent}</div>'
# load page index .json file
index_path = os.getcwd() + "/index.json"
with open(index_path, 'r') as f:
json_page_index = json.load(f)
def get_absolute_paths(path=None):
"""Get paths to all .md pages to be converted to html"""
md_pages = {}
if path is None:
path = os.path.join(os.getcwd(), "content")
directories = os.listdir(path)
for item in directories:
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
# add items from new dict into md_pages
md_pages.update(get_absolute_paths(item_path))
else:
if not item == "README.md" and item.endswith(".md"):
# get the relative path for the web content only
location = item_path.split(os.getcwd()+os.sep)[1]
location = location.split(item)[0]
page = item.split("_", 1)[1]
page = page.split(".md")[0] + ".html"
md_pages[item] = '/website_redesign/' + location + page
return md_pages
def create_page_link(file, label, page_paths, indent):
file_path = page_paths[file]
return f'\n{indent}<a href="{file_path}">{label}</a>'
def create_toggle_section(toggle_label):
section = f'\n{indent}<div class="sidebar-list">' + \
f'\n{indent}\t<a id="sidebar-header"' + \
' onclick="toggleSubmenu(event)">' + \
f'\n{indent}<span class="toggle-icon">+</span>' + \
f'\n{indent}{indent}{toggle_label}' + \
f'\n{indent}\t</a>' + \
f'\n{indent}\t<div class="submenu">'
return section
def build_navbar(json_page_index):
navbar_html = ''
page_paths = get_absolute_paths()
for section, contents in json_page_index.items():
# For pages that are not nested in a toggle
if isinstance(contents, str):
navbar_html += create_page_link(
section,
contents,
page_paths,
indent,
)
# For pages that are nested in a toggle
elif isinstance(contents, list):
toggle_label = contents[0]
toggle_contents = contents[1]
# Add toggle <div> sections and link
navbar_html += create_toggle_section(toggle_label)
# Add pages under toggle
for sub_page, sub_name in toggle_contents.items():
navbar_html += create_page_link(
sub_page,
sub_name,
page_paths,
indent+indent,
)
# Close toggle <div> sections
navbar_html += f'\n{indent}\t</div>'
navbar_html += f'\n{indent}</div>'
return navbar_html
html += build_navbar(json_page_index)
html += '\n\t</div>'
return html
# print(generate_navbar_html())