Skip to content

Commit 7813e7c

Browse files
authored
Refactor auto-generate API docs (#331)
* Add `clean` action to Windows `make` file * Use `fail-on-warning` mode in Windows `make` file * Drop unused `--keep-going` flag in Makefile * Refactor auto-generate api docs * Update contributing docs * Remove Sphinx version constraint * Document make-mode only * Allow target chaining in Windows make
1 parent 1b6b44d commit 7813e7c

File tree

6 files changed

+50
-65
lines changed

6 files changed

+50
-65
lines changed

CONTRIBUTING.md

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -291,55 +291,24 @@ All subsequent commands should be run from this directory.
291291

292292
To build the documentation, run:
293293

294-
::::{tab-set}
295-
:::{tab-item} Unix platforms with `make`
296294
```sh
297295
make html
298296
```
299297
The local build can be viewed by opening `docs/build/html/index.html` in a browser.
300-
:::
301-
302-
:::{tab-item} All platforms
303-
```sh
304-
python make_api_index.py && sphinx-build source build -W --keep-going
305-
```
306-
The local build can be viewed by opening `docs/build/index.html` in a browser.
307-
:::
308-
::::
309298

310-
To re-build the documentation after making changes, run the command below. It will remove all generated files in `docs/`,
311-
including the auto-generated API index `source/api_index.rst`, and those in `build/`, `source/api/`, and `source/examples/`, and then re-build the documentation.
299+
To re-build the documentation after making changes, we recommend removing existing build files first.
300+
The following command will remove all generated files in `docs/`,
301+
including the auto-generated API index `source/api_index.rst`, and those in `build/`, `source/api/`, and `source/examples/`. It will then re-build the documentation:
312302

313-
::::{tab-set}
314-
:::{tab-item} Unix platforms with `make`
315303
```sh
316304
make clean html
317305
```
318-
:::
319-
320-
:::{tab-item} All platforms
321-
```sh
322-
rm -f source/api_index.rst && rm -rf build && rm -rf source/api && rm -rf source/examples
323-
python make_api_index.py && sphinx-build source build -W --keep-going
324-
```
325-
:::
326-
::::
327306

328307
To check that external links are correctly resolved, run:
329308

330-
::::{tab-set}
331-
:::{tab-item} Unix platforms with `make`
332309
```sh
333310
make linkcheck
334311
```
335-
:::
336-
337-
:::{tab-item} All platforms
338-
```sh
339-
sphinx-build source build -b linkcheck -W --keep-going
340-
```
341-
:::
342-
::::
343312

344313
If the linkcheck step incorrectly marks links with valid anchors as broken, you can skip checking the anchors in specific links by adding the URLs to `linkcheck_anchors_ignore_for_url` in `docs/source/conf.py`, e.g.:
345314

@@ -352,6 +321,14 @@ linkcheck_anchors_ignore_for_url = [
352321
]
353322
```
354323

324+
:::{tip}
325+
The `make` commands can be combined to run multiple tasks sequentially.
326+
For example, to re-build the documentation and check the links, run:
327+
```sh
328+
make clean html linkcheck
329+
```
330+
:::
331+
355332
## Sample data
356333

357334
We maintain some sample datasets to be used for testing, examples and tutorials on an
@@ -399,9 +376,9 @@ To add a new file, you will need to:
399376
6. Determine the sha256 checksum hash of each new file. You can do this in a terminal by running:
400377
::::{tab-set}
401378
:::{tab-item} Ubuntu
402-
```bash
403-
sha256sum <filename>
404-
```
379+
```bash
380+
sha256sum <filename>
381+
```
405382
:::
406383

407384
:::{tab-item} MacOS

docs/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
# You can set these variables from the command line, and also
55
# from the environment for the first two.
66
# -W: if there are warnings, treat them as errors and exit with status 1.
7-
# --keep-going: run sphinx-build to completion and exit with status 1 if errors.
8-
SPHINXOPTS ?= -W --keep-going
7+
SPHINXOPTS ?= -W
98
SPHINXBUILD ?= sphinx-build
109
SOURCEDIR = source
1110
BUILDDIR = build

docs/make.bat

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if "%SPHINXBUILD%" == "" (
99
)
1010
set SOURCEDIR=source
1111
set BUILDDIR=build
12+
set SPHINXOPTS=-W
1213

1314
%SPHINXBUILD% >NUL 2>NUL
1415
if errorlevel 9009 (
@@ -25,10 +26,22 @@ if errorlevel 9009 (
2526

2627
if "%1" == "" goto help
2728

28-
echo "Generating API index..."
29-
python make_api_index.py
29+
:process_targets
30+
if "%1" == "clean" (
31+
@echo Removing auto-generated files under 'docs' and 'src'...
32+
rmdir /S /Q %BUILDDIR%
33+
del /Q %SOURCEDIR%\api_index.rst
34+
rmdir /S /Q %SOURCEDIR%\api\
35+
rmdir /S /Q %SOURCEDIR%\examples\
36+
) else (
37+
@echo Generating API index...
38+
python make_api_index.py
39+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
40+
)
41+
42+
shift
43+
if not "%1" == "" goto process_targets
3044

31-
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
3245
goto end
3346

3447
:help

docs/make_api_index.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
"""Generate the API index page for all ``movement`` modules."""
22

33
import os
4+
from pathlib import Path
45

56
# Modules to exclude from the API index
67
exclude_modules = ["cli_entrypoint"]
78

89
# Set the current working directory to the directory of this script
9-
script_dir = os.path.dirname(os.path.abspath(__file__))
10+
script_dir = Path(__file__).resolve().parent
1011
os.chdir(script_dir)
1112

1213

1314
def make_api_index():
1415
"""Create a doctree of all ``movement`` modules."""
1516
doctree = "\n"
16-
17-
for root, _, files in os.walk("../movement"):
18-
# Remove leading "../"
19-
root = root[3:]
20-
for file in sorted(files):
21-
if file.endswith(".py") and not file.startswith("_"):
22-
# Convert file path to module name
23-
module_name = os.path.join(root, file)
24-
module_name = module_name[:-3].replace(os.sep, ".")
25-
# Check if the module should be excluded
26-
if not any(
27-
file.startswith(exclude_module)
28-
for exclude_module in exclude_modules
29-
):
30-
doctree += f" {module_name}\n"
31-
17+
api_path = Path("../movement")
18+
for path in sorted(api_path.rglob("*.py")):
19+
if path.name.startswith("_"):
20+
continue
21+
# Convert file path to module name
22+
rel_path = path.relative_to(api_path.parent)
23+
module_name = str(rel_path.with_suffix("")).replace(os.sep, ".")
24+
if rel_path.stem not in exclude_modules:
25+
doctree += f" {module_name}\n"
3226
# Get the header
33-
with open("./source/_templates/api_index_head.rst") as f:
34-
api_head = f.read()
27+
api_head_path = Path("source") / "_templates" / "api_index_head.rst"
28+
api_head = api_head_path.read_text()
3529
# Write api_index.rst with header + doctree
36-
with open("./source/api_index.rst", "w") as f:
37-
f.write("..\n This file is auto-generated.\n\n")
30+
output_path = Path("source") / "api_index.rst"
31+
with output_path.open("w") as f:
3832
f.write(api_head)
3933
f.write(doctree)
40-
print(os.path.abspath("./source/api_index.rst"))
4134

4235

4336
if __name__ == "__main__":

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ myst-parser
44
nbsphinx
55
pydata-sphinx-theme
66
setuptools-scm
7-
sphinx>=7.0
7+
sphinx
88
sphinx-autodoc-typehints
99
sphinx-design
1010
sphinx-gallery

docs/source/_templates/api_index_head.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
..
2+
This file is auto-generated.
3+
14
.. _target-api:
25

36
API Reference

0 commit comments

Comments
 (0)