Docs: improve API landing page
This commit is contained in:
		
							parent
							
								
									9d1440752e
								
							
						
					
					
						commit
						5f4a02cc69
					
				@ -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"
 | 
			
		||||
@ -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):
 | 
			
		||||
@ -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.
 | 
			
		||||
 | 
			
		||||
@ -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("_"):
 | 
			
		||||
 | 
			
		||||
@ -236,3 +236,6 @@ modelfree
 | 
			
		||||
bdq
 | 
			
		||||
util
 | 
			
		||||
logp
 | 
			
		||||
autogenerated
 | 
			
		||||
subpackage
 | 
			
		||||
subpackages
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user