Skip to content

Commit c668dfe

Browse files
fix: Fix categories rendering
Issue #14: mkdocstrings/python#14
1 parent bbb1d50 commit c668dfe

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/mkdocstrings_handlers/python/handler.py

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
219219
self.env.filters["order_members"] = rendering.do_order_members
220220
self.env.filters["format_code"] = rendering.do_format_code
221221
self.env.filters["format_signature"] = rendering.do_format_signature
222+
self.env.filters["filter_docstrings"] = rendering.do_filter_docstrings
222223

223224
def get_anchors(self, data: CollectorItem) -> list[str]: # noqa: D102 (ignore missing docstring)
224225
try:

src/mkdocstrings_handlers/python/rendering.py

+18
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ def repl(match): # noqa: WPS430
133133
return Markup(text).format(**variables)
134134

135135

136+
def do_filter_docstrings(
137+
objects_dictionary: dict[str, Object | Alias],
138+
keep_empty: bool = True,
139+
) -> list[Object | Alias]:
140+
"""Filter a dictionary of objects based on their docstrings.
141+
142+
Parameters:
143+
objects_dictionary: The dictionary of objects.
144+
keep_empty: Whether to keep objects with no/empty docstrings (recursive check).
145+
146+
Returns:
147+
A list of objects.
148+
"""
149+
if keep_empty:
150+
return list(objects_dictionary.values())
151+
return [obj for obj in objects_dictionary.values() if obj.has_docstrings]
152+
153+
136154
@lru_cache(maxsize=1)
137155
def _get_black_formatter():
138156
try:

src/mkdocstrings_handlers/python/templates/material/_base/children.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{% set extra_level = 0 %}
1414
{% endif %}
1515

16-
{% if config.show_category_heading and obj.attributes.values()|any %}
16+
{% if config.show_category_heading and obj.attributes|filter_docstrings(config.show_if_no_docstring) %}
1717
{% filter heading(heading_level, id=html_id ~ "-attributes") %}Attributes{% endfilter %}
1818
{% endif %}
1919
{% with heading_level = heading_level + extra_level %}
@@ -24,7 +24,7 @@
2424
{% endfor %}
2525
{% endwith %}
2626

27-
{% if config.show_category_heading and obj.classes.values()|any %}
27+
{% if config.show_category_heading and obj.classes|filter_docstrings(config.show_if_no_docstring) %}
2828
{% filter heading(heading_level, id=html_id ~ "-classes") %}Classes{% endfilter %}
2929
{% endif %}
3030
{% with heading_level = heading_level + extra_level %}
@@ -35,7 +35,7 @@
3535
{% endfor %}
3636
{% endwith %}
3737

38-
{% if config.show_category_heading and obj.functions.values()|any %}
38+
{% if config.show_category_heading and obj.functions|filter_docstrings(config.show_if_no_docstring) %}
3939
{% filter heading(heading_level, id=html_id ~ "-functions") %}Functions{% endfilter %}
4040
{% endif %}
4141
{% with heading_level = heading_level + extra_level %}
@@ -49,7 +49,7 @@
4949
{% endwith %}
5050

5151
{% if config.show_submodules %}
52-
{% if config.show_category_heading and obj.modules.values()|any %}
52+
{% if config.show_category_heading and obj.modules|filter_docstrings(config.show_if_no_docstring) %}
5353
{% filter heading(heading_level, id=html_id ~ "-modules") %}Modules{% endfilter %}
5454
{% endif %}
5555
{% with heading_level = heading_level + extra_level %}

0 commit comments

Comments
 (0)