Skip to content

Commit

Permalink
Docs: improve API landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Panchenko committed Dec 5, 2023
1 parent 9d14407 commit 5f4a02c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
33 changes: 30 additions & 3 deletions docs/autogen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def module_template(module_qualname: str):
"""


def index_template(package_name: str, doc_references: list[str] | None = None):
def index_template(package_name: str, doc_references: list[str] | None = None, text_prefix=""):
doc_references = doc_references or ""
if doc_references:
doc_references = "\n" + "\n".join(f"* :doc:`{ref}`" for ref in doc_references) + "\n"
Expand All @@ -27,7 +27,7 @@ def index_template(package_name: str, doc_references: list[str] | None = None):
title = dirname.replace("_", r"\_")
if title == "tianshou":
title = "Tianshou API Reference"
return f"{title}\n{'=' * len(title)}" + doc_references
return f"{title}\n{'=' * len(title)}" + text_prefix + doc_references


def write_to_file(content: str, path: str):
Expand All @@ -37,6 +37,14 @@ def write_to_file(content: str, path: str):
os.chmod(path, 0o666)


_SUBTITLE = (
"\n Here is the autogenerated documentation of the Tianshou API. \n \n "
"The Table of Contents to the left has the same structure as the "
"repository's package code. The links at each page point to the submodules and subpackages. \n\n "
"Enjoy scrolling through! \n"
)


def make_rst(src_root, rst_root, clean=False, overwrite=False, package_prefix=""):
"""Creates/updates documentation in form of rst files for modules and packages.
Expand All @@ -59,7 +67,26 @@ def make_rst(src_root, rst_root, clean=False, overwrite=False, package_prefix=""
shutil.rmtree(rst_root)

base_package_name = package_prefix + os.path.basename(src_root)
write_to_file(index_template(base_package_name), os.path.join(rst_root, "index.rst"))

# TODO: reduce duplication with same logic for subpackages below
files_in_dir = os.listdir(src_root)
module_names = [f[:-3] for f in files_in_dir if f.endswith(".py") and not f.startswith("_")]
subdir_refs = [
os.path.join(f, "index")
for f in files_in_dir
if os.path.isdir(os.path.join(src_root, f)) and not f.startswith("_")
]
package_index_rst_path = os.path.join(
rst_root,
"index.rst",
)
log.info(f"Writing {package_index_rst_path}")
write_to_file(
index_template(
base_package_name, doc_references=module_names + subdir_refs, text_prefix=_SUBTITLE,
),
package_index_rst_path,
)

for root, dirnames, filenames in os.walk(src_root):
if os.path.basename(root).startswith("_"):
Expand Down
3 changes: 3 additions & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,6 @@ modelfree
bdq
util
logp
autogenerated
subpackage
subpackages

0 comments on commit 5f4a02c

Please sign in to comment.