|
1 | 1 | import glob |
2 | 2 | import os |
| 3 | +import re |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def update_toctree_yaml(): |
| 8 | + input_file = "source/_toctree.yml" |
7 | 9 | output_file = "source/_toctree.yml" |
8 | 10 | dirnames = ["sagemaker-sdk"] |
9 | 11 |
|
10 | | - with open(output_file, "a") as f: |
11 | | - f.write("# GENERATED CONTENT DO NOT EDIT!\n") |
12 | | - f.write("- title: Examples\n") |
13 | | - f.write(" sections:\n") |
14 | | - |
15 | | - for dirname in dirnames: |
16 | | - # Get sorted files excluding index |
17 | | - files = sorted(glob.glob(f"source/examples/{dirname}-*.mdx")) |
18 | | - files = [f for f in files if not f.endswith(f"{dirname}-index.mdx")] |
19 | | - |
20 | | - file_entries = [] |
21 | | - for file_path in files: |
22 | | - with open(file_path, "r") as mdx_file: |
23 | | - first_line = mdx_file.readline().strip() |
24 | | - if first_line.startswith("# "): |
25 | | - title = first_line[2:].strip() |
26 | | - base_name = Path(file_path).stem |
27 | | - file_entries.append((base_name, title)) |
28 | | - else: |
29 | | - print(f"⚠️ Skipping {Path(file_path).name} - missing H1 title") |
30 | | - continue |
31 | | - |
32 | | - # Write directory section |
33 | | - f.write(" - title: SageMaker SDK\n") |
34 | | - # f.write(f" local: examples/{dirname}-index\n") |
35 | | - f.write(" isExpanded: true\n") |
36 | | - |
37 | | - for idx, (base, title) in enumerate(file_entries): |
38 | | - if idx == 0: |
39 | | - f.write(" sections:\n") |
40 | | - f.write(f" - local: examples/{base}\n") |
41 | | - f.write(f' title: "{title}"\n') |
42 | | - |
43 | | - f.write("# END GENERATED CONTENT\n") |
| 12 | + # Read the existing content |
| 13 | + with open(input_file, "r") as f: |
| 14 | + content = f.read() |
| 15 | + |
| 16 | + # Find the position between tutorials and reference sections |
| 17 | + tutorials_end = content.find("- sections:\n - local: reference/inference-toolkit") |
| 18 | + if tutorials_end == -1: |
| 19 | + print("Error: Could not find the reference section in the file") |
| 20 | + return |
| 21 | + |
| 22 | + # Generate the new content |
| 23 | + new_content = [] |
| 24 | + new_content.append("# GENERATED CONTENT DO NOT EDIT!") |
| 25 | + new_content.append("- title: Examples") |
| 26 | + new_content.append(" sections:") |
| 27 | + |
| 28 | + for dirname in dirnames: |
| 29 | + # Get sorted files excluding index |
| 30 | + files = sorted(glob.glob(f"source/examples/{dirname}-*.mdx")) |
| 31 | + files = [f for f in files if not f.endswith(f"{dirname}-index.mdx")] |
| 32 | + |
| 33 | + file_entries = [] |
| 34 | + for file_path in files: |
| 35 | + with open(file_path, "r") as mdx_file: |
| 36 | + first_line = mdx_file.readline().strip() |
| 37 | + if first_line.startswith("# "): |
| 38 | + title = first_line[2:].strip() |
| 39 | + base_name = Path(file_path).stem |
| 40 | + file_entries.append((base_name, title)) |
| 41 | + else: |
| 42 | + print(f"⚠️ Skipping {Path(file_path).name} - missing H1 title") |
| 43 | + continue |
| 44 | + |
| 45 | + # Write directory section |
| 46 | + new_content.append(" - title: SageMaker SDK") |
| 47 | + new_content.append(" isExpanded: false") |
| 48 | + |
| 49 | + for idx, (base, title) in enumerate(file_entries): |
| 50 | + if idx == 0: |
| 51 | + new_content.append(" sections:") |
| 52 | + new_content.append(f" - local: examples/{base}") |
| 53 | + new_content.append(f' title: "{title}"') |
| 54 | + |
| 55 | + new_content.append("# END GENERATED CONTENT") |
| 56 | + |
| 57 | + # Insert the new content |
| 58 | + updated_content = content[:tutorials_end] + "\n" + "\n".join(new_content) + "\n" + content[tutorials_end:] |
| 59 | + |
| 60 | + # Write the updated content back to the file |
| 61 | + with open(output_file, "w") as f: |
| 62 | + f.write(updated_content) |
44 | 63 |
|
45 | 64 |
|
46 | 65 | if __name__ == "__main__": |
|
0 commit comments