|
| 1 | +""" |
| 2 | +Auto-generate the index and nav pages |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +HERE = Path(__file__).parent |
| 10 | + |
| 11 | + |
| 12 | +def write_index(pages_sorted: list[Path], out_file: Path) -> None: |
| 13 | + content_l = [ |
| 14 | + "# Forcings email archive", |
| 15 | + "", |
| 16 | + "Here we provide an archive of emails with updates about the forcings sent by the CMIP International Project Office (IPO).", |
| 17 | + "", |
| 18 | + ] |
| 19 | + |
| 20 | + for page in pages_sorted: |
| 21 | + content_l.append(f"- [{page.stem}]({page.name})") |
| 22 | + |
| 23 | + content = "\n".join(content_l) |
| 24 | + with open(out_file, "w") as fh: |
| 25 | + fh.write(content) |
| 26 | + |
| 27 | + return |
| 28 | + |
| 29 | + |
| 30 | +def write_nav(pages_sorted: list[Path], out_file: Path) -> None: |
| 31 | + content_l = [ |
| 32 | + "- [Overview](index.md)", |
| 33 | + ] |
| 34 | + |
| 35 | + for page in pages_sorted: |
| 36 | + content_l.append(f"- [{page.stem}]({page.name})") |
| 37 | + |
| 38 | + content = "\n".join(content_l) |
| 39 | + with open(out_file, "w") as fh: |
| 40 | + fh.write(content) |
| 41 | + |
| 42 | + return |
| 43 | + |
| 44 | + |
| 45 | +def main() -> None: |
| 46 | + pages = [f for f in HERE.glob("*.md") if f.name not in ["index.md", "SUMMARY.md"]] |
| 47 | + pages_sorted = sorted(pages, key=lambda x: x.name, reverse=True) |
| 48 | + |
| 49 | + write_index(pages_sorted=pages_sorted, out_file=HERE / "index.md") |
| 50 | + write_nav(pages_sorted=pages_sorted, out_file=HERE / "SUMMARY.md") |
| 51 | + |
| 52 | + |
| 53 | +# # Can't use this here as not called as main, hence the below |
| 54 | +# if __name__ == "__main__": |
| 55 | +# main() |
| 56 | +# |
| 57 | +main() |
0 commit comments