|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This auto-generates the mdBook SUMMARY.md file based on the layout on the filesystem. |
| 5 | +
|
| 6 | +This generates the `src` directory based on the contents of the `text` directory. |
| 7 | +
|
| 8 | +Most RFCs should be kept to a single chapter. However, in some rare cases it |
| 9 | +may be necessary to spread across multiple pages. In that case, place them in |
| 10 | +a subdirectory with the same name as the RFC. For example: |
| 11 | +
|
| 12 | + 0123-my-awesome-feature.md |
| 13 | + 0123-my-awesome-feature/extra-material.md |
| 14 | +
|
| 15 | +It is recommended that if you have static content like images that you use a similar layout: |
| 16 | +
|
| 17 | + 0123-my-awesome-feature.md |
| 18 | + 0123-my-awesome-feature/diagram.svg |
| 19 | +
|
| 20 | +The chapters are presented in sorted-order. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import shutil |
| 25 | +import subprocess |
| 26 | + |
| 27 | +def main(): |
| 28 | + if os.path.exists('src'): |
| 29 | + # Clear out src to remove stale links in case you switch branches. |
| 30 | + shutil.rmtree('src') |
| 31 | + os.mkdir('src') |
| 32 | + |
| 33 | + for path in os.listdir('text'): |
| 34 | + symlink(f'../text/{path}', f'src/{path}') |
| 35 | + symlink('../README.md', 'src/introduction.md') |
| 36 | + |
| 37 | + with open('src/SUMMARY.md', 'w') as summary: |
| 38 | + summary.write('[Introduction](introduction.md)\n\n') |
| 39 | + collect(summary, 'text', 0) |
| 40 | + |
| 41 | + subprocess.call(['mdbook', 'build']) |
| 42 | + |
| 43 | +def collect(summary, path, depth): |
| 44 | + entries = [e for e in os.scandir(path) if e.name.endswith('.md')] |
| 45 | + entries.sort(key=lambda e: e.name) |
| 46 | + for entry in entries: |
| 47 | + indent = ' '*depth |
| 48 | + name = entry.name[:-3] |
| 49 | + link_path = entry.path[5:] |
| 50 | + summary.write(f'{indent}- [{name}]({link_path})\n') |
| 51 | + maybe_subdir = os.path.join(path, name) |
| 52 | + if os.path.isdir(maybe_subdir): |
| 53 | + collect(summary, maybe_subdir, depth+1) |
| 54 | + |
| 55 | +def symlink(src, dst): |
| 56 | + if not os.path.exists(dst): |
| 57 | + os.symlink(src, dst) |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments