1- """
2- Generate the API index page for all modules.
3- Inspired by `movement` package.
4- """
5-
61import os
72from pathlib import Path
83
94# Modules to exclude from the API index
10- exclude_modules = [
11- "cli_entrypoint" ,
12- "loader_widgets" ,
13- "meta_widget" ,
14- ]
5+ EXCLUDE_MODULES = {"cli_entrypoint" , "loader_widgets" , "meta_widget" }
156
167# Set the current working directory to the directory of this script
17- script_dir = Path (__file__ ).resolve ().parent
18- os .chdir (script_dir )
8+ SCRIPT_DIR = Path (__file__ ).resolve ().parent
9+ os .chdir (SCRIPT_DIR )
1910
2011
2112def make_api_index ():
22- """Create a doctree of all ``movement`` modules ."""
23- doctree = " \n "
13+ """Generate a properly formatted `api_index.rst` file for Sphinx documentation ."""
14+
2415 api_path = Path ("../derotation" )
16+ module_entries = []
17+
2518 for path in sorted (api_path .rglob ("*.py" )):
2619 # Convert file path to module name
2720 rel_path = path .relative_to (api_path .parent )
2821 module_name = str (rel_path .with_suffix ("" )).replace (os .sep , "." )
29- if rel_path .stem not in exclude_modules :
30- doctree += f" { module_name } \n "
31- # # Get the header
32- api_head_path = Path ("source" ) / "_templates" / "api_index_head.rst"
33- api_head = api_head_path .read_text ()
34- # Write api_index.rst with header + doctree
22+
23+ if rel_path .stem not in EXCLUDE_MODULES :
24+ module_entries .append (module_name )
25+
26+ # Construct the API index content
27+ api_index_content = """\
28+ .. _target-api:
29+
30+ API Reference
31+ =============
32+
33+ This section contains automatically generated documentation for the `derotation` package.
34+
35+ .. toctree::
36+ :maxdepth: 2
37+ :caption: API Documentation
38+
39+ """
40+
41+ # Add module entries for the toctree
42+ api_index_content += "\n " .join (f" { module } " for module in module_entries ) + "\n \n "
43+
44+ # Add the autosummary directive
45+ api_index_content += """\
46+ .. rubric:: Modules
47+
48+ .. autosummary::
49+ :toctree: api
50+ :nosignatures:
51+
52+ """
53+
54+ # Add module entries for autosummary
55+ api_index_content += "\n " .join (f" { module } " for module in module_entries ) + "\n "
56+
57+ # Write the generated content to `api_index.rst`
3558 output_path = Path ("source" ) / "api_index.rst"
36- with output_path .open ( "w" ) as f :
37- f . write ( api_head )
38- f . write ( doctree )
59+ output_path .write_text ( api_index_content , encoding = "utf-8" )
60+
61+ print ( f"Generated { output_path } " )
3962
4063
4164if __name__ == "__main__" :
42- make_api_index ()
65+ make_api_index ()
0 commit comments