Skip to content

Commit

Permalink
Add some documentation in README file.
Browse files Browse the repository at this point in the history
Revise template loader.
  • Loading branch information
changlichun committed Aug 9, 2023
1 parent 228e3c6 commit 6b627f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
32 changes: 32 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Directive MATLAB object
``.. application:: appname`` **application definition**
``.. autoapplication:: appname`` * auto-document
``:app:`appname``` * reference
``.. autosummary`` **Generate autodoc summaries**
==================================== ===========================================

Several options are available for auto-directives.
Expand Down Expand Up @@ -276,6 +277,37 @@ Instead use the ``mat:`` prefix before the desired directives::
.. mat:automodule:: matsrc
.. mat:autofunction:: matsrc.func

Autosummary
===========

Mailly base on `sphinx.ext.autosummary <https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html>`_. The
``.. autosummary::`` directive can be used to generate a summary table of the MATLAB modules or MATLAB functions and classes in a module.

Example::

.. autosummary for modules
.. autosummary::
:toctree: generated/

folder1
folder2

.. autosummary for functions and classes
.. currentmodule:: <module_name>
.. autosummary::
:toctree: generated/

func1
func2
class1
class2

More details can be found in the `sphinx.ext.autosummary <https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html>`_.
And test examples can be found in the ``test_doc_autosummary``.

.. note::

May not be compatible with ``sphinx.ext.autosummary``.

Online Demo
===========
Expand Down
50 changes: 23 additions & 27 deletions sphinxcontrib/mat_autosummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from os import path
from typing import TYPE_CHECKING, Any, Sequence

from jinja2 import TemplateNotFound
from jinja2.sandbox import SandboxedEnvironment

import sphinx.locale
Expand Down Expand Up @@ -99,6 +100,20 @@ def __init__(self, app: Sphinx) -> None:
self.env.add_extension("jinja2.ext.i18n")
self.env.install_gettext_translations(app.translator)

def render(self, template_name: str, context: dict) -> str:
"""Render a template file."""
try:
template = self.env.get_template(template_name)
except TemplateNotFound:
try:
# objtype is given as template_name
template = self.env.get_template("mat%s.rst" % template_name)
except TemplateNotFound:
# fallback to base.rst
template = self.env.get_template("matbase.rst")

return template.render(context)


def generate_autosummary_content(
name: str,
Expand Down Expand Up @@ -670,34 +685,15 @@ def get_items(self, names: list[str]) -> list[tuple[str, str, str, str]]:


def setup(app: Sphinx) -> dict[str, Any]:
# I need autodoc
app.setup_extension("sphinx.ext.autodoc")
app.add_node(
autosummary_toc,
html=(autosummary_toc_visit_html, autosummary_noop),
latex=(autosummary_noop, autosummary_noop),
text=(autosummary_noop, autosummary_noop),
man=(autosummary_noop, autosummary_noop),
texinfo=(autosummary_noop, autosummary_noop),
)
app.add_node(
autosummary_table,
html=(autosummary_table_visit_html, autosummary_noop),
latex=(autosummary_noop, autosummary_noop),
text=(autosummary_noop, autosummary_noop),
man=(autosummary_noop, autosummary_noop),
texinfo=(autosummary_noop, autosummary_noop),
)
# sphinx.ext.autosummary
app.setup_extension("sphinx.ext.autosummary")

app.add_directive_to_domain("mat", "autosummary", MatAutosummary)

# replace autosummary generate with our own
for listener in app.events.listeners.get("builder-inited", []):
if listener.handler.__name__ == "process_generate_options":
app.disconnect(listener.id)
app.connect("builder-inited", process_generate_options)
app.add_config_value("autosummary_context", {}, True)
app.add_config_value("autosummary_filename_map", {}, "html")
app.add_config_value("autosummary_generate", True, True, [bool, list])
app.add_config_value("autosummary_generate_overwrite", True, False)
app.add_config_value(
"autosummary_mock_imports", lambda config: config.autodoc_mock_imports, "env"
)
app.add_config_value("autosummary_imported_members", [], False, [bool])
app.add_config_value("autosummary_ignore_module_all", True, "env", bool)

return {"version": sphinx.__display_version__, "parallel_read_safe": True}
20 changes: 20 additions & 0 deletions tests/test_docs_autosummary/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
autosummary for modules
=======================

.. autosummary::
:toctree: generated/
:template: matmodule.rst
Expand All @@ -6,3 +9,20 @@
test_data.+package
test_data.@ClassFolder
test_data.submodule

.. Specifying template files by :template: directive. matmodule.rst is the default template file for modules(folders).
.. And matclass.rst is the default template file for classes.
.. Could be omitted or user could create their own template files.
.. IF omitted, the template file will be chosen based on the object type.
autosummary for classes and functions
=====================================

.. currentmodule:: test_data
.. autosummary::
:toctree: generated/

test_data.f_example
test_data.f_inputargs_error
test_data.Bool
test_data.ClassAbstract

0 comments on commit 6b627f8

Please sign in to comment.