Add ability to have sub-chapters

This adds the ability for an RFC to have sub-chapters.
This commit is contained in:
Eric Huss 2023-02-12 10:44:02 -08:00
parent 5c8d632e2a
commit 2f348cfb6a
3 changed files with 61 additions and 23 deletions

View File

@ -18,7 +18,7 @@ jobs:
echo `pwd`/mdbook >> $GITHUB_PATH
- name: Generate Book
run: |
./generate-book.sh
./generate-book.py
- name: Deploy GitHub Pages
run: |
git worktree add gh-pages gh-pages

60
generate-book.py Executable file
View File

@ -0,0 +1,60 @@
#!/usr/bin/env python3
"""
This auto-generates the mdBook SUMMARY.md file based on the layout on the filesystem.
This generates the `src` directory based on the contents of the `text` directory.
Most RFCs should be kept to a single chapter. However, in some rare cases it
may be necessary to spread across multiple pages. In that case, place them in
a subdirectory with the same name as the RFC. For example:
0123-my-awesome-feature.md
0123-my-awesome-feature/extra-material.md
It is recommended that if you have static content like images that you use a similar layout:
0123-my-awesome-feature.md
0123-my-awesome-feature/diagram.svg
The chapters are presented in sorted-order.
"""
import os
import shutil
import subprocess
def main():
if os.path.exists('src'):
# Clear out src to remove stale links in case you switch branches.
shutil.rmtree('src')
os.mkdir('src')
for path in os.listdir('text'):
symlink(f'../text/{path}', f'src/{path}')
symlink('../README.md', 'src/introduction.md')
with open('src/SUMMARY.md', 'w') as summary:
summary.write('[Introduction](introduction.md)\n\n')
collect(summary, 'text', 0)
subprocess.call(['mdbook', 'build'])
def collect(summary, path, depth):
entries = [e for e in os.scandir(path) if e.name.endswith('.md')]
entries.sort(key=lambda e: e.name)
for entry in entries:
indent = ' '*depth
name = entry.name[:-3]
link_path = entry.path[5:]
summary.write(f'{indent}- [{name}]({link_path})\n')
maybe_subdir = os.path.join(path, name)
if os.path.isdir(maybe_subdir):
collect(summary, maybe_subdir, depth+1)
def symlink(src, dst):
if not os.path.exists(dst):
os.symlink(src, dst)
if __name__ == '__main__':
main()

View File

@ -1,22 +0,0 @@
#!/usr/bin/env bash
set -e
if [ ! -d src ]; then
mkdir src
fi
printf '[Introduction](introduction.md)\n\n' > src/SUMMARY.md
find text ! -type d -print0 | xargs -0 -I {} ln -fs ../{} src/
find ./text ! -type d -name '*.md' -print0 \
| sort -z \
| while read -r -d '' file;
do
printf -- '- [%s](%s)\n' "$(basename "$file" ".md")" "$(basename "$file")"
done >> src/SUMMARY.md
ln -fs ../README.md src/introduction.md
mdbook build