Skip to content

Commit 2a603b8

Browse files
jorgepilotoAWhetter
authored andcommitted
Recursive rendering of children with their own page
1 parent 93fb571 commit 2a603b8

File tree

8 files changed

+121
-219
lines changed

8 files changed

+121
-219
lines changed

autoapi/mappers/base.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,50 @@ def create_class(self, data, options=None, **kwargs):
327327
"""
328328
raise NotImplementedError
329329

330+
def output_child_rst(self, obj, obj_parent, detail_dir, source_suffix):
331+
332+
if not obj.display:
333+
return
334+
335+
# Skip nested cases like functions in functions or clases in clases
336+
if obj.type == obj_parent.type:
337+
return
338+
339+
obj_child_page_level = _OWN_PAGE_LEVELS.index(obj.type)
340+
desired_page_level = _OWN_PAGE_LEVELS.index(self.app.config.autoapi_own_page_level)
341+
is_own_page = obj_child_page_level <= desired_page_level
342+
if not is_own_page:
343+
return
344+
345+
obj_child_rst = obj.render(
346+
is_own_page=is_own_page,
347+
)
348+
if not obj_child_rst:
349+
return
350+
351+
function_page_level = _OWN_PAGE_LEVELS.index("function")
352+
is_level_beyond_function = function_page_level < desired_page_level
353+
if obj.type in ["exception", "class"]:
354+
if not is_level_beyond_function:
355+
outfile = f"{obj.short_name}{source_suffix}"
356+
path = os.path.join(detail_dir, outfile)
357+
else:
358+
outdir = os.path.join(detail_dir, obj.short_name)
359+
ensuredir(outdir)
360+
path = os.path.join(outdir, f"index{source_suffix}")
361+
else:
362+
is_parent_in_detail_dir = detail_dir.endswith(obj_parent.short_name)
363+
outdir = detail_dir if is_parent_in_detail_dir else os.path.join(detail_dir, obj_parent.short_name)
364+
ensuredir(outdir)
365+
path = os.path.join(outdir, f"{obj.short_name}{source_suffix}")
366+
367+
with open(path, "wb+") as obj_child_detail_file:
368+
obj_child_detail_file.write(obj_child_rst.encode("utf-8"))
369+
370+
for obj_child in obj.children:
371+
child_detail_dir = os.path.join(detail_dir, obj.name)
372+
self.output_child_rst(obj_child, obj, child_detail_dir, source_suffix)
373+
330374
def output_rst(self, root, source_suffix):
331375
for _, obj in status_iterator(
332376
self.objects_to_render.items(),
@@ -347,6 +391,9 @@ def output_rst(self, root, source_suffix):
347391
path = os.path.join(detail_dir, f"index{source_suffix}")
348392
with open(path, "wb+") as detail_file:
349393
detail_file.write(rst.encode("utf-8"))
394+
395+
for child in obj.children:
396+
self.output_child_rst(child, obj, detail_dir, source_suffix)
350397

351398
if self.app.config.autoapi_add_toctree_entry:
352399
self._output_top_rst(root)

autoapi/templates/python/class.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,69 @@
3737
{% set visible_classes = obj.classes|rejectattr("inherited")|selectattr("display")|list %}
3838
{% endif %}
3939
{% for klass in visible_classes %}
40-
{{ klass.render()|indent(3) }}
40+
{{ klass.render(own_page_types=[])|indent(3) }}
4141
{% endfor %}
4242
{% if "inherited-members" in autoapi_options %}
4343
{% set visible_properties = obj.properties|selectattr("display")|list %}
4444
{% else %}
4545
{% set visible_properties = obj.properties|rejectattr("inherited")|selectattr("display")|list %}
4646
{% endif %}
47+
{% if "property" in own_page_types and visible_properties %}
48+
49+
Properties
50+
----------
51+
52+
.. toctree::
53+
:hidden:
54+
55+
{% for property in visible_properties %}
56+
{{ property.name }}
57+
{% endfor %}
58+
{% else %}
4759
{% for property in visible_properties %}
4860
{{ property.render()|indent(3) }}
4961
{% endfor %}
62+
{% endif %}
5063
{% if "inherited-members" in autoapi_options %}
5164
{% set visible_attributes = obj.attributes|selectattr("display")|list %}
5265
{% else %}
5366
{% set visible_attributes = obj.attributes|rejectattr("inherited")|selectattr("display")|list %}
5467
{% endif %}
68+
{% if "attribute" in own_page_types and visible_attributes %}
69+
70+
Attributes
71+
----------
72+
73+
.. toctree::
74+
:hidden:
75+
76+
{% for attribute in visible_attributes %}
77+
{{ attribute.name }}
78+
{% endfor %}
79+
{% else %}
5580
{% for attribute in visible_attributes %}
5681
{{ attribute.render()|indent(3) }}
5782
{% endfor %}
83+
{% endif %}
5884
{% if "inherited-members" in autoapi_options %}
5985
{% set visible_methods = obj.methods|selectattr("display")|list %}
6086
{% else %}
6187
{% set visible_methods = obj.methods|rejectattr("inherited")|selectattr("display")|list %}
6288
{% endif %}
89+
{% if "method" in own_page_types and visible_methods %}
90+
91+
Methods
92+
-------
93+
94+
.. toctree::
95+
:hidden:
96+
97+
{% for method in visible_methods %}
98+
{{ method.name }}
99+
{% endfor %}
100+
{% else %}
63101
{% for method in visible_methods %}
64102
{{ method.render()|indent(3) }}
65103
{% endfor %}
104+
{% endif %}
66105
{% endif %}

autoapi/templates/python/module.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Attributes
8181
:hidden:
8282

8383
{% for attribute in visible_attributes %}
84-
{{ attribute.short_name }}/index.rst
84+
{{ attribute.short_name }}
8585
{% endfor %}
8686

8787
{% endif%}
@@ -127,7 +127,18 @@ Classes
127127
:hidden:
128128

129129
{% for klass in visible_classes %}
130+
{#
131+
The set own_page_types sometimes is not ordered! This changes the value of
132+
its last element. Thus, the best way to check is to verify if 'function'
133+
lies within the list
134+
Do -> if 'function' not in own_page_types
135+
Instead of -> if "class" == (own_page_types | list | last)
136+
#}
137+
{% if "method" not in own_page_types %}
138+
{{ klass.short_name }}.rst
139+
{% else %}
130140
{{ klass.short_name }}/index.rst
141+
{% endif %}
131142
{% endfor %}
132143

133144
{% endif %}
@@ -145,12 +156,12 @@ Classes
145156
{% if "function" in own_page_types or "show-module-summary" in autoapi_options %}
146157
Functions
147158
---------
148-
{% if "class" in own_page_types %}
159+
{% if "function" in own_page_types %}
149160
.. toctree::
150161
:hidden:
151162

152163
{% for function in visible_functions %}
153-
{{ function.short_name }}/index.rst
164+
{{ function.short_name }}.rst
154165
{% endfor %}
155166

156167
{% endif %}

tests/python/pypackageexample/autoapi/example/_private_module/index.rst

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/python/pypackageexample/autoapi/example/foo/index.rst

Lines changed: 0 additions & 110 deletions
This file was deleted.

tests/python/pypackageexample/autoapi/example/index.rst

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/python/pypackageexample/autoapi/index.rst

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)