|
| 1 | +""" |
| 2 | +Drive Ford to create Fortran API docs |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import mkdocs_gen_files |
| 12 | + |
| 13 | +REPO_ROOT = Path(__file__).parents[1] |
| 14 | + |
| 15 | +FORD_OUTPUT_DIR = Path("docs") / "fortran-api" |
| 16 | + |
| 17 | +ford = shutil.which("ford") |
| 18 | +if ford is None: |
| 19 | + msg = "Could not find FORD executable" |
| 20 | + raise AssertionError(msg) |
| 21 | + |
| 22 | +subprocess.run( # noqa: S603 |
| 23 | + [ford, "ford_config.md", "--output_dir", str(FORD_OUTPUT_DIR)], |
| 24 | + check=True, |
| 25 | +) |
| 26 | + |
| 27 | +# Copy files across using mkdocs_gen_files |
| 28 | +# so it knows to include the files in the final docs. |
| 29 | +for entry in (REPO_ROOT / FORD_OUTPUT_DIR).rglob("*"): |
| 30 | + if not entry.is_file(): |
| 31 | + continue |
| 32 | + |
| 33 | + with open(entry, "rb") as fh: |
| 34 | + contents = fh.read() |
| 35 | + |
| 36 | + target_file = entry.relative_to(REPO_ROOT / "docs") |
| 37 | + with mkdocs_gen_files.open(target_file, "wb") as fh: |
| 38 | + fh.write(contents) |
| 39 | + |
| 40 | + if target_file.name == "index.html": |
| 41 | + # Also create a home.html file which we can point to from `NAVIGATION.md`. |
| 42 | + # I don't know why just pointing to `index.html` doesn't work, |
| 43 | + # but it doesn't (maybe cause `index.html` is a special name?). |
| 44 | + target_file = target_file.parent / "home.html" |
| 45 | + |
| 46 | + with mkdocs_gen_files.open(target_file, "wb") as fh: |
| 47 | + fh.write(contents) |
| 48 | + |
| 49 | +# # If we want to move back to using literate-nav for maanging our navigation, |
| 50 | +# # also for the Fortran bits, we'll need something like the below |
| 51 | +# # and adjustments to the existing `NAVIGATION.md`. |
| 52 | +# with mkdocs_gen_files.open( |
| 53 | +# (FORD_OUTPUT_DIR).relative_to("docs") / "NAVIGATION.md", "w" |
| 54 | +# ) as fh: |
| 55 | +# fh.writelines("* [example_fgen_basic](home.html)") |
| 56 | + |
| 57 | +# Remove the ford files (which were just copied) |
| 58 | +shutil.rmtree(REPO_ROOT / FORD_OUTPUT_DIR) |
| 59 | + |
| 60 | +# Put back the gitkeep file |
| 61 | +gitkeep = REPO_ROOT / FORD_OUTPUT_DIR / ".gitkeep" |
| 62 | +gitkeep.parent.mkdir() |
| 63 | +gitkeep.touch() |
0 commit comments