|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Generate the mdBook source tree from the RFC repository layout. |
| 5 | +
|
| 6 | +This mirrors the rust-lang/rfcs approach: RFCs live under text/, while src/ |
| 7 | +is generated just before building the book. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + if os.path.exists("src"): |
| 17 | + shutil.rmtree("src") |
| 18 | + os.mkdir("src") |
| 19 | + |
| 20 | + for path in os.listdir("text"): |
| 21 | + symlink(f"../text/{path}", f"src/{path}") |
| 22 | + symlink("../README.md", "src/introduction.md") |
| 23 | + symlink("../SUBCOMMITTEES.md", "src/subcommittees.md") |
| 24 | + |
| 25 | + with open("src/SUMMARY.md", "w") as summary: |
| 26 | + summary.write("[Introduction](introduction.md)\n\n") |
| 27 | + summary.write("- [Subcommittees](subcommittees.md)\n") |
| 28 | + collect(summary, "text", 0) |
| 29 | + |
| 30 | + subprocess.call(["mdbook", "build"]) |
| 31 | + |
| 32 | + |
| 33 | +def collect(summary, path, depth): |
| 34 | + entries = [entry for entry in os.scandir(path) if entry.name.endswith(".md")] |
| 35 | + entries.sort(key=lambda entry: entry.name) |
| 36 | + for entry in entries: |
| 37 | + indent = " " * depth |
| 38 | + name = entry.name[:-3] |
| 39 | + link_path = entry.path[5:] |
| 40 | + summary.write(f"{indent}- [{name}]({link_path})\n") |
| 41 | + maybe_subdir = os.path.join(path, name) |
| 42 | + if os.path.isdir(maybe_subdir): |
| 43 | + collect(summary, maybe_subdir, depth + 1) |
| 44 | + |
| 45 | + |
| 46 | +def symlink(src, dst): |
| 47 | + if not os.path.exists(dst): |
| 48 | + os.symlink(src, dst) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments