Skip to content

Commit

Permalink
fix: Fix categories rendering
Browse files Browse the repository at this point in the history
Issue #14: #14
  • Loading branch information
pawamoy committed Apr 18, 2022
1 parent d5ea1c5 commit 6407cf4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
self.env.filters["order_members"] = rendering.do_order_members
self.env.filters["format_code"] = rendering.do_format_code
self.env.filters["format_signature"] = rendering.do_format_signature
self.env.filters["filter_docstrings"] = rendering.do_filter_docstrings

def get_anchors(self, data: CollectorItem) -> list[str]: # noqa: D102 (ignore missing docstring)
try:
Expand Down
18 changes: 18 additions & 0 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ def repl(match): # noqa: WPS430
return Markup(text).format(**variables)


def do_filter_docstrings(
objects_dictionary: dict[str, Object | Alias],
keep_empty: bool = True,
) -> list[Object | Alias]:
"""Filter a dictionary of objects based on their docstrings.
Parameters:
objects_dictionary: The dictionary of objects.
keep_empty: Whether to keep objects with no/empty docstrings (recursive check).
Returns:
A list of objects.
"""
if keep_empty:
return list(objects_dictionary.values())
return [obj for obj in objects_dictionary.values() if obj.has_docstrings]


@lru_cache(maxsize=1)
def _get_black_formatter():
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% set extra_level = 0 %}
{% endif %}

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

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

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

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

0 comments on commit 6407cf4

Please sign in to comment.