Skip to content

Commit 61d8abe

Browse files
jorgepilotoAWhetter
authored andcommitted
Recursive rendering of children with their own page
1 parent 15c6d24 commit 61d8abe

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
@@ -329,6 +329,50 @@ def create_class(self, data, options=None, **kwargs):
329329
"""
330330
raise NotImplementedError
331331

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

353400
if self.app.config.autoapi_add_toctree_entry:
354401
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)