#!/usr/bin/env python# -*- coding: utf-8 -*-"""Jinja2 Template rendering module for Sphinx documentation generation."""importtypingasTimportdataclassesfrompathlibimportPathfromjinja2importTemplatefrompicage.apiimportPackage,ModuleifT.TYPE_CHECKING:# pragma: no coverfrom..autotoctreeimportPageFolderdir_here=Path(__file__).absolute().parent
[docs]defget_template(name:str)->Template:""" Load a Jinja2 template by name from the template directory. """path=dir_here.joinpath(f"{name}.tpl")returnTemplate(path.read_text(encoding="utf-8"))
[docs]classTemplateEnum:""" Enum container for loaded Jinja2 templates. """toc=get_template("toc")module=get_template("module")package=get_template("package")
ModuleTemplateParams=Module"""Parameters for module template rendering."""
[docs]defrender_module(params:ModuleTemplateParams)->str:""" Render `module template <https://github.com/MacHu-GWU/docfly-project/blob/main/docfly/template/module.tpl>`_ for Sphinx documentation. Example module `docfly.auto_api_doc <https://github.com/MacHu-GWU/docfly-project/blob/main/docfly/auto_api_doc.py>`_: Example rendered document source: .. code-block:: auto_api_doc ============ .. automodule:: docfly.auto_api_doc :members: """returnTemplateEnum.module.render(params=params)
[docs]@dataclasses.dataclassclassPackageTemplateParams:""" Parameters for package template rendering. """package:Package=dataclasses.field()sub_packages:list[Package]=dataclasses.field()sub_modules:list[Module]=dataclasses.field()
[docs]defrender_package(params:PackageTemplateParams)->str:""" Render `package template <https://github.com/MacHu-GWU/docfly-project/blob/main/docfly/template/package.tpl>`_ for Sphinx documentation. Example package `docfly <https://github.com/MacHu-GWU/docfly-project/tree/main/docfly>`_: Example rendered document source: .. code-block:: docfly ====== .. automodule:: docfly :members: sub packages and modules ------------------------ .. toctree:: :maxdepth: 1 directives <directives/__init__> template <template/__init__> auto_api_doc <auto_api_doc> autotoctree <autotoctree> See: """returnTemplateEnum.package.render(params=params)
[docs]@dataclasses.dataclassclassTocTemplateParams:""" Parameters for TOC template rendering. """page_folders:list["PageFolder"]=dataclasses.field()maxdepth:T.Optional[int]=dataclasses.field(default=None)
[docs]defrender_toc(params:TocTemplateParams)->str:""" Render `toctree template <https://github.com/MacHu-GWU/docfly-project/blob/main/docfly/template/toc.tpl>`_ for Sphinx documentation. Example table of content tree `docfly project docs/sources <https://github.com/MacHu-GWU/docfly-project/tree/main/docs/source>`_: Example rendered document source: .. code-block:: .. toctree:: :maxdepth: 1 Quick Start <01-Quick-Start/index> Sphinx Style Guide <02-Sphinx-Style-Guide/index> """returnTemplateEnum.toc.render(params=params)