|
| 1 | +# Generates a new download page and adds it to navigation |
| 2 | +# Usage: python scripts/new_download_page.py <parent> <title> |
| 3 | +# where <parent> is one of "agent_development_tools", "agents", "domains", "examples_and_unsupported" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +import sys |
| 7 | +from string import Template |
| 8 | + |
| 9 | +PROJECT_DIR = Path(__file__).parent.parent |
| 10 | + |
| 11 | +DOWNLOADS_DIR = PROJECT_DIR / "docs" / "downloads" |
| 12 | + |
| 13 | +MKDOCS_YML = PROJECT_DIR / "mkdocs.yml" |
| 14 | + |
| 15 | +SECTIONS = ["agent_development_tools", "agents", "domains", "examples_and_unsupported"] |
| 16 | + |
| 17 | +DOWNLOAD_TEMPLATE = Template( |
| 18 | + """--- |
| 19 | +Tags: |
| 20 | + - TODO |
| 21 | +--- |
| 22 | +
|
| 23 | +# $title |
| 24 | +
|
| 25 | +TODO: description |
| 26 | +
|
| 27 | +## Environment Properties |
| 28 | +
|
| 29 | +* TODO |
| 30 | +
|
| 31 | +## Download Links |
| 32 | +
|
| 33 | +* [TODO: name](TODO: link) |
| 34 | +
|
| 35 | +## Associated Agents |
| 36 | +
|
| 37 | +TODO (not for agents) |
| 38 | +
|
| 39 | +## Documentation |
| 40 | +
|
| 41 | +TODO |
| 42 | +
|
| 43 | +## Associated Publications |
| 44 | +
|
| 45 | +* TODO |
| 46 | +
|
| 47 | +## Developers |
| 48 | +
|
| 49 | +* TODO |
| 50 | +
|
| 51 | +## Soar Versions |
| 52 | +
|
| 53 | +* TODO |
| 54 | +
|
| 55 | +## Language |
| 56 | +
|
| 57 | +TODO (not for agents) |
| 58 | +
|
| 59 | +## Project Type |
| 60 | +
|
| 61 | +TODO (agents only) |
| 62 | +""" |
| 63 | +) |
| 64 | + |
| 65 | + |
| 66 | +def name_to_file_name(name: str) -> str: |
| 67 | + return name.lower().replace(" ", "_") + ".md" |
| 68 | + |
| 69 | + |
| 70 | +def main(parent: str, title: str): |
| 71 | + # First check that that we are able to make all of the file changes |
| 72 | + # we need to |
| 73 | + |
| 74 | + if parent not in SECTIONS: |
| 75 | + print(f"Invalid parent; must be one of {SECTIONS}") |
| 76 | + return |
| 77 | + parent_dir = DOWNLOADS_DIR / parent |
| 78 | + index_file = parent_dir / "index.md" |
| 79 | + if not index_file.exists(): |
| 80 | + raise FileNotFoundError(f"Parent directory {parent_dir} does not exist") |
| 81 | + |
| 82 | + new_file_name = name_to_file_name(title) |
| 83 | + new_file_path = parent_dir / new_file_name |
| 84 | + if new_file_path.exists(): |
| 85 | + print(f"File {new_file_path} already exists") |
| 86 | + return |
| 87 | + new_file_contents = DOWNLOAD_TEMPLATE.substitute(title=title) |
| 88 | + |
| 89 | + with open(MKDOCS_YML, "r") as f: |
| 90 | + lines = f.readlines() |
| 91 | + index_line = None |
| 92 | + num_leading_spaces = -1 |
| 93 | + expected_parent_line = f"- downloads/{parent}/index.md" |
| 94 | + for i, line in enumerate(lines): |
| 95 | + if line.strip() == expected_parent_line: |
| 96 | + index_line = i |
| 97 | + num_leading_spaces = len(line) - len(line.lstrip()) |
| 98 | + break |
| 99 | + line_to_insert = f"{' ' * num_leading_spaces}- {title}: downloads/{parent}/{new_file_name}\n" |
| 100 | + line_inserted = False |
| 101 | + for i in range(index_line + 1, len(lines)): |
| 102 | + if len(lines[i]) - len(lines[i].lstrip()) <= num_leading_spaces: |
| 103 | + lines.insert(i + 1, line_to_insert) |
| 104 | + line_inserted = True |
| 105 | + break |
| 106 | + if not line_inserted: |
| 107 | + lines.append(line_to_insert) |
| 108 | + |
| 109 | + # then do the actual edits |
| 110 | + |
| 111 | + print(f"Adding file {new_file_path}") |
| 112 | + with open(new_file_path, "w") as f: |
| 113 | + f.write(new_file_contents) |
| 114 | + |
| 115 | + print(f"Adding link to {index_file}") |
| 116 | + with open(index_file, "a") as f: |
| 117 | + f.write(f"* [{title}](./{new_file_name})\n") |
| 118 | + |
| 119 | + print(f"Adding to mkdocs.yml") |
| 120 | + with open(MKDOCS_YML, "w") as f: |
| 121 | + f.write("".join(lines)) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + if len(sys.argv) != 3: |
| 126 | + print(f"Usage: {sys.argv[0]} <parent> <title>") |
| 127 | + sys.exit(1) |
| 128 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments