-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_mkdocs.py
More file actions
80 lines (66 loc) · 3.36 KB
/
Copy pathupdate_mkdocs.py
File metadata and controls
80 lines (66 loc) · 3.36 KB
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
import os
import yaml
# Path to the mkdocs.yml file
MKDOCS_YML_PATH = "mkdocs.yml"
# Directories to scan for markdown files
EXPERIMENTS_DIR = "docs/experiments"
MEETINGS_DIR = "docs/meetings"
def get_markdown_files(directory):
"""Get a sorted list of markdown files in a directory."""
if not os.path.exists(directory):
return []
return sorted(
[os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(".md")]
)
def format_nav_entry(filepath):
"""Format the nav entry to include the filename as the display name."""
filename = os.path.basename(filepath)
name = os.path.splitext(filename)[0] # Remove the .md extension
return {name: os.path.relpath(filepath, "docs")}
def update_mkdocs():
"""Update the mkdocs.yml file to include all markdown files dynamically."""
# Load the existing mkdocs.yml
with open(MKDOCS_YML_PATH, "r") as f:
mkdocs_config = yaml.safe_load(f)
# Ensure 'nav' exists and is a list
if "nav" not in mkdocs_config or not isinstance(mkdocs_config["nav"], list):
mkdocs_config["nav"] = []
# Find the Project Resources and Project Management sections
project_resources_index = None
project_management_index = None
for i, item in enumerate(mkdocs_config["nav"]):
if isinstance(item, dict) and "Project Resources" in item:
project_resources_index = i
if isinstance(item, dict) and "Project Management" in item:
project_management_index = i
# Remove existing Experiments section from Project Resources
if project_resources_index is not None:
project_resources = mkdocs_config["nav"][project_resources_index]["Project Resources"]
project_resources = [item for item in project_resources if not (isinstance(item, dict) and "Experiments" in item)]
mkdocs_config["nav"][project_resources_index]["Project Resources"] = project_resources
# Remove existing Meetings section from Project Management
if project_management_index is not None:
project_management = mkdocs_config["nav"][project_management_index]["Project Management"]
project_management = [item for item in project_management if not (isinstance(item, dict) and "Meetings" in item)]
mkdocs_config["nav"][project_management_index]["Project Management"] = project_management
# Add updated Experiments section to Project Resources
experiments_files = get_markdown_files(EXPERIMENTS_DIR)
if experiments_files:
experiments_entries = [format_nav_entry(f) for f in experiments_files]
if project_resources_index is not None:
mkdocs_config["nav"][project_resources_index]["Project Resources"].append(
{"Experiments": experiments_entries}
)
# Add updated Meetings section to Project Management
meetings_files = get_markdown_files(MEETINGS_DIR)
if meetings_files:
meetings_entries = [format_nav_entry(f) for f in meetings_files]
if project_management_index is not None:
mkdocs_config["nav"][project_management_index]["Project Management"].append(
{"Meetings": meetings_entries}
)
# Save the updated mkdocs.yml
with open(MKDOCS_YML_PATH, "w") as f:
yaml.dump(mkdocs_config, f, default_flow_style=False, sort_keys=False)
if __name__ == "__main__":
update_mkdocs()