Skip to content

Commit

Permalink
refactor: Add backlinks metadata to autorefs elements
Browse files Browse the repository at this point in the history
Issue-153: #153
  • Loading branch information
pawamoy committed Feb 4, 2025
1 parent 40067f7 commit 0fd25e6
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 58 deletions.
6 changes: 6 additions & 0 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from collections.abc import Iterable
import glob
import os
import posixpath
Expand Down Expand Up @@ -273,6 +274,11 @@ def render(self, data: CollectorItem, options: PythonOptions) -> str: # noqa: D
},
)

def render_backlinks(self, backlinks: Mapping[str, Iterable[str]]) -> str: # noqa: D102 (ignore missing docstring)
template = self.env.get_template("backlinks.html.jinja")
verbose_type = {key: key.capitalize().replace("-by", " by") for key in backlinks.keys()}
return template.render(backlinks=backlinks, config=self.get_options({}), verbose_type=verbose_type)

def update_env(self, config: Any) -> None: # noqa: ARG002
"""Update the Jinja environment with custom filters and tests.
Expand Down
4 changes: 2 additions & 2 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ def do_format_attribute(

signature = str(attribute_path).strip()
if annotations and attribute.annotation:
annotation = template.render(context.parent, expression=attribute.annotation, signature=True)
annotation = template.render(context.parent, expression=attribute.annotation, signature=True, backlink_type="returned-by")
signature += f": {annotation}"
if attribute.value:
value = template.render(context.parent, expression=attribute.value, signature=True)
value = template.render(context.parent, expression=attribute.value, signature=True, backlink_type="used-by")
signature += f" = {value}"

signature = do_format_code(signature, line_length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Context:
{% include "docstring"|get_template with context %}
{% endwith %}
{% endblock docstring %}

<backlinks identifier="{{ html_id }}" handler="python" />
{% endblock contents %}
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{#- Template for backlinks.
This template renders backlinks.
Context:
backlinks (Mapping[str, Iterable[str]]): The backlinks to render.
config (dict): The configuration options.
-#}

{% block logs scoped %}
{#- Logging block.
This block can be used to log debug messages, deprecation messages, warnings, etc.
-#}
{{ log.debug("Rendering backlinks") }}
{% endblock logs %}

<div class="doc doc-backlinks">
{% for backlink_type, backlink_list in backlinks | dictsort %}
<b class="doc doc-backlink-type">{{ verbose_type[backlink_type] }}:</b>
<ul class="doc doc-backlink-list">
{% for url, title in backlink_list | sort(attribute=1) %}
<li><a href="{{ url }}" class="doc doc-backlink">{{ (title or url.split("#", 1)[-1]) | safe }}</a></li>
{% endfor %}
</ul>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Context:
heading_level,
role="class",
id=html_id,
title=html_id,
class="doc doc-heading",
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + class.name,
) %}
Expand Down Expand Up @@ -130,7 +131,11 @@ Context:
{% if config.show_bases and class.bases %}
<p class="doc doc-class-bases">
Bases: {% for expression in class.bases -%}
<code>{% include "expression"|get_template with context %}</code>{% if not loop.last %}, {% endif %}
<code>
{%- with backlink_type = "subclassed-by" -%}
{%- include "expression"|get_template with context -%}
{%- endwith -%}
</code>{% if not loop.last %}, {% endif %}
{% endfor -%}
</p>
{% endif %}
Expand Down Expand Up @@ -159,6 +164,8 @@ Context:
{% endif %}
{% endblock docstring %}

<backlinks identifier="{{ html_id }}" handler="python" />

{% block summary scoped %}
{#- Summary block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Context:
<td><code><autoref identifier="{{ obj.path }}.{{ attribute.name }}" optional hover>{{ attribute.name }}</autoref></code></td>
<td>
{% if attribute.annotation %}
{% with expression = attribute.annotation %}
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -60,7 +60,7 @@ Context:
<li class="doc-section-item field-body">
<b><code><autoref identifier="{{ obj.path }}.{{ attribute.name }}" optional hover>{{ attribute.name }}</autoref></code></b>
{% if attribute.annotation %}
{% with expression = attribute.annotation %}
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
(<code>{% include "expression"|get_template with context %}</code>)
{% endwith %}
{% endif %}
Expand Down Expand Up @@ -94,7 +94,7 @@ Context:
{% if attribute.annotation %}
<span class="doc-attribute-annotation">
<b>TYPE:</b>
{% with expression = attribute.annotation %}
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Context:
<td><code>{{ parameter.name }}</code></td>
<td>
{% if parameter.annotation %}
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -60,7 +60,7 @@ Context:
<li class="doc-section-item field-body">
<b><code>{{ parameter.name }}</code></b>
{% if parameter.annotation %}
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
(<code>{% include "expression"|get_template with context %}</code>)
{% endwith %}
{% endif %}
Expand Down Expand Up @@ -94,7 +94,7 @@ Context:
{% if parameter.annotation %}
<span class="doc-param-annotation">
<b>{{ lang.t("TYPE:") }}</b>
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Context:
</td>
<td>
{% if parameter.annotation %}
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -63,7 +63,7 @@ Context:
</td>
<td>
{% if parameter.default %}
{% with expression = parameter.default %}
{% with expression = parameter.default, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% else %}
Expand Down Expand Up @@ -96,10 +96,10 @@ Context:
<b><code>{{ parameter.name }}</code></b>
{% endif %}
{% if parameter.annotation %}
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
(<code>{% include "expression"|get_template with context %}</code>
{%- if parameter.default %}, {{ lang.t("default:") }}
{% with expression = parameter.default %}
{% with expression = parameter.default, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %})
Expand Down Expand Up @@ -149,15 +149,15 @@ Context:
{% if parameter.annotation %}
<span class="doc-param-annotation">
<b>{{ lang.t("TYPE:") }}</b>
{% with expression = parameter.annotation %}
{% with expression = parameter.annotation, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
{% endif %}
{% if parameter.default %}
<span class="doc-param-default">
<b>{{ lang.t("DEFAULT:") }}</b>
{% with expression = parameter.default %}
{% with expression = parameter.default, backlink_type = "used-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Context:
<tr class="doc-section-item">
<td>
{% if raises.annotation %}
{% with expression = raises.annotation %}
{% with expression = raises.annotation, backlink_type = "raised-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -57,7 +57,7 @@ Context:
{% for raises in section.value %}
<li class="doc-section-item field-body">
{% if raises.annotation %}
{% with expression = raises.annotation %}
{% with expression = raises.annotation, backlink_type = "raised-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
Expand All @@ -84,7 +84,7 @@ Context:
<tr class="doc-section-item">
<td>
<span class="doc-raises-annotation">
{% with expression = raises.annotation %}
{% with expression = raises.annotation, backlink_type = "raised-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Context:
{% if name_column %}<td>{% if receives.name %}<code>{{ receives.name }}</code>{% endif %}</td>{% endif %}
<td>
{% if receives.annotation %}
{% with expression = receives.annotation %}
{% with expression = receives.annotation, backlink_type = "received-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -61,7 +61,7 @@ Context:
<li class="doc-section-item field-body">
{% if receives.name %}<b><code>{{ receives.name }}</code></b>{% endif %}
{% if receives.annotation %}
{% with expression = receives.annotation %}
{% with expression = receives.annotation, backlink_type = "received-by" %}
{% if receives.name %} ({% endif %}
<code>{% include "expression"|get_template with context %}</code>
{% if receives.name %}){% endif %}
Expand Down Expand Up @@ -93,7 +93,7 @@ Context:
<code>{{ receives.name }}</code>
{% elif receives.annotation %}
<span class="doc-receives-annotation">
{% with expression = receives.annotation %}
{% with expression = receives.annotation, backlink_type = "received-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand All @@ -107,7 +107,7 @@ Context:
<p>
<span class="doc-receives-annotation">
<b>{{ lang.t("TYPE:") }}</b>
{% with expression = receives.annotation %}
{% with expression = receives.annotation, backlink_type = "received-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Context:
{% if name_column %}<td>{% if returns.name %}<code>{{ returns.name }}</code>{% endif %}</td>{% endif %}
<td>
{% if returns.annotation %}
{% with expression = returns.annotation %}
{% with expression = returns.annotation, backlink_type = "returned-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -61,7 +61,7 @@ Context:
<li class="doc-section-item field-body">
{% if returns.name %}<b><code>{{ returns.name }}</code></b>{% endif %}
{% if returns.annotation %}
{% with expression = returns.annotation %}
{% with expression = returns.annotation, backlink_type = "returned-by" %}
{% if returns.name %} ({% endif %}
<code>{% include "expression"|get_template with context %}</code>
{% if returns.name %}){% endif %}
Expand Down Expand Up @@ -93,7 +93,7 @@ Context:
<code>{{ returns.name }}</code>
{% elif returns.annotation %}
<span class="doc-returns-annotation">
{% with expression = returns.annotation %}
{% with expression = returns.annotation, backlink_type = "returned-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand All @@ -107,7 +107,7 @@ Context:
<p>
<span class="doc-returns-annotation">
<b>{{ lang.t("TYPE:") }}</b>
{% with expression = returns.annotation %}
{% with expression = returns.annotation, backlink_type = "returned-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Context:
<tr class="doc-section-item">
<td>
{% if warns.annotation %}
{% with expression = warns.annotation %}
{% with expression = warns.annotation, backlink_type = "emitted-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -57,7 +57,7 @@ Context:
{% for warns in section.value %}
<li class="doc-section-item field-body">
{% if warns.annotation %}
{% with expression = warns.annotation %}
{% with expression = warns.annotation, backlink_type = "emitted-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
Expand All @@ -84,7 +84,7 @@ Context:
<tr class="doc-section-item">
<td>
<span class="doc-warns-annotation">
{% with expression = warns.annotation %}
{% with expression = warns.annotation, backlink_type = "emitted-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Context:
{% if name_column %}<td>{% if yields.name %}<code>{{ yields.name }}</code>{% endif %}</td>{% endif %}
<td>
{% if yields.annotation %}
{% with expression = yields.annotation %}
{% with expression = yields.annotation, backlink_type = "yielded-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
{% endif %}
Expand All @@ -61,7 +61,7 @@ Context:
<li class="doc-section-item field-body">
{% if yields.name %}<b><code>{{ yields.name }}</code></b>{% endif %}
{% if yields.annotation %}
{% with expression = yields.annotation %}
{% with expression = yields.annotation, backlink_type = "yielded-by" %}
{% if yields.name %} ({% endif %}
<code>{% include "expression"|get_template with context %}</code>
{% if yields.name %}){% endif %}
Expand Down Expand Up @@ -93,7 +93,7 @@ Context:
<code>{{ yields.name }}</code>
{% elif yields.annotation %}
<span class="doc-yields-annotation">
{% with expression = yields.annotation %}
{% with expression = yields.annotation, backlink_type = "yielded-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand All @@ -107,7 +107,7 @@ Context:
<p>
<span class="doc-yields-annotation">
<b>{{ lang.t("TYPE:") }}:</b>
{% with expression = yields.annotation %}
{% with expression = yields.annotation, backlink_type = "yielded-by" %}
<code>{% include "expression"|get_template with context %}</code>
{% endwith %}
</span>
Expand Down
Loading

0 comments on commit 0fd25e6

Please sign in to comment.