Skip to content

Commit 93e6b46

Browse files
committed
fixup! refactor: Add backlinks metadata to autorefs elements
1 parent a4c7bec commit 93e6b46

27 files changed

+156
-107
lines changed

src/mkdocstrings_handlers/python/handler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Iterable
56
import glob
67
import os
78
import posixpath
@@ -268,6 +269,11 @@ def render(self, data: CollectorItem, options: PythonOptions) -> str: # noqa: D
268269
},
269270
)
270271

272+
def render_backlinks(self, backlinks: Mapping[str, Iterable[str]]) -> str: # noqa: D102 (ignore missing docstring)
273+
template = self.env.get_template("backlinks.html.jinja")
274+
verbose_type = {key: key.capitalize().replace("-by", " by") for key in backlinks.keys()}
275+
return template.render(backlinks=backlinks, config=self.get_options({}), verbose_type=verbose_type)
276+
271277
def update_env(self, config: Any) -> None: # noqa: ARG002
272278
"""Update the Jinja environment with custom filters and tests.
273279

src/mkdocstrings_handlers/python/rendering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def do_format_attribute(
210210

211211
signature = str(attribute_path).strip()
212212
if annotations and attribute.annotation:
213-
annotation = template.render(context.parent, expression=attribute.annotation, signature=True, reftype="returned-by")
213+
annotation = template.render(context.parent, expression=attribute.annotation, signature=True, backlink_type="returned-by")
214214
signature += f": {annotation}"
215215
if attribute.value:
216-
value = template.render(context.parent, expression=attribute.value, signature=True, reftype="used-by")
216+
value = template.render(context.parent, expression=attribute.value, signature=True, backlink_type="used-by")
217217
signature += f" = {value}"
218218

219219
signature = do_format_code(signature, line_length)

src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Context:
1212

1313
{% block logs scoped %}
1414
{#- Logging block.
15-
15+
1616
This block can be used to log debug messages, deprecation messages, warnings, etc.
1717
-#}
1818
{{ log.debug("Rendering " + attribute.path) }}
@@ -44,7 +44,7 @@ Context:
4444

4545
{% block heading scoped %}
4646
{#- Heading block.
47-
47+
4848
This block renders the heading for the attribute.
4949
-#}
5050
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-attribute"></code>{% endif %}
@@ -60,7 +60,7 @@ Context:
6060

6161
{% block labels scoped %}
6262
{#- Labels block.
63-
63+
6464
This block renders the labels for the attribute.
6565
-#}
6666
{% with labels = attribute.labels %}
@@ -72,7 +72,7 @@ Context:
7272

7373
{% block signature scoped %}
7474
{#- Signature block.
75-
75+
7676
This block renders the signature for the attribute.
7777
-#}
7878
{% if config.separate_signature %}
@@ -99,20 +99,22 @@ Context:
9999
<div class="doc doc-contents {% if root %}first{% endif %}">
100100
{% block contents scoped %}
101101
{#- Contents block.
102-
102+
103103
This block renders the contents of the attribute.
104104
It contains other blocks that users can override.
105105
Overriding the contents block allows to rearrange the order of the blocks.
106106
-#}
107107
{% block docstring scoped %}
108108
{#- Docstring block.
109-
109+
110110
This block renders the docstring for the attribute.
111111
-#}
112112
{% with docstring_sections = attribute.docstring.parsed %}
113113
{% include "docstring"|get_template with context %}
114114
{% endwith %}
115115
{% endblock docstring %}
116+
117+
<backlinks identifier="{{ html_id }}" handler="python" />
116118
{% endblock contents %}
117119
</div>
118120

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{#- Template for backlinks.
2+
3+
This template renders backlinks.
4+
5+
Context:
6+
backlinks (Mapping[str, Iterable[str]]): The backlinks to render.
7+
config (dict): The configuration options.
8+
-#}
9+
10+
{% block logs scoped %}
11+
{#- Logging block.
12+
13+
This block can be used to log debug messages, deprecation messages, warnings, etc.
14+
-#}
15+
{{ log.debug("Rendering backlinks") }}
16+
{% endblock logs %}
17+
18+
<div class="doc doc-backlinks">
19+
{% for backlink_type, backlink_list in backlinks | dictsort %}
20+
<b class="doc doc-backlink-type">{{ verbose_type[backlink_type] }}:</b>
21+
<ul class="doc doc-backlink-list">
22+
{% for url, title in backlink_list | sort(attribute=1) %}
23+
<li><a href="{{ url }}" class="doc doc-backlink">{{ (title or url.split("#", 1)[-1]) | safe }}</a></li>
24+
{% endfor %}
25+
</ul>
26+
{% endfor %}
27+
</div>

src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Context:
1111

1212
{% block logs scoped %}
1313
{#- Logging block.
14-
14+
1515
This block can be used to log debug messages, deprecation messages, warnings, etc.
1616
-#}
1717
{{ log.debug("Rendering " + class.path) }}
@@ -37,13 +37,14 @@ Context:
3737
heading_level,
3838
role="class",
3939
id=html_id,
40+
title=html_id,
4041
class="doc doc-heading",
4142
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + class.name,
4243
) %}
4344

4445
{% block heading scoped %}
4546
{#- Heading block.
46-
47+
4748
This block renders the heading for the class.
4849
-#}
4950
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-class"></code>{% endif %}
@@ -62,7 +63,7 @@ Context:
6263

6364
{% block labels scoped %}
6465
{#- Labels block.
65-
66+
6667
This block renders the labels for the class.
6768
-#}
6869
{% with labels = class.labels %}
@@ -74,7 +75,7 @@ Context:
7475

7576
{% block signature scoped %}
7677
{#- Signature block.
77-
78+
7879
This block renders the signature for the class.
7980
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled.
8081
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled.
@@ -117,21 +118,21 @@ Context:
117118
<div class="doc doc-contents {% if root %}first{% endif %}">
118119
{% block contents scoped %}
119120
{#- Contents block.
120-
121+
121122
This block renders the contents of the class.
122123
It contains other blocks that users can override.
123124
Overriding the contents block allows to rearrange the order of the blocks.
124125
-#}
125126
{% block bases scoped %}
126127
{#- Class bases block.
127-
128+
128129
This block renders the bases for the class.
129130
-#}
130131
{% if config.show_bases and class.bases %}
131132
<p class="doc doc-class-bases">
132133
Bases: {% for expression in class.bases -%}
133134
<code>
134-
{%- with reftype = "subclassed-by" -%}
135+
{%- with backlink_type = "subclassed-by" -%}
135136
{%- include "expression"|get_template with context -%}
136137
{%- endwith -%}
137138
</code>{% if not loop.last %}, {% endif %}
@@ -142,7 +143,7 @@ Context:
142143

143144
{% block docstring scoped %}
144145
{#- Docstring block.
145-
146+
146147
This block renders the docstring for the class.
147148
-#}
148149
{% with docstring_sections = class.docstring.parsed %}
@@ -163,17 +164,19 @@ Context:
163164
{% endif %}
164165
{% endblock docstring %}
165166

167+
<backlinks identifier="{{ html_id }}" handler="python" />
168+
166169
{% block summary scoped %}
167170
{#- Summary block.
168-
171+
169172
This block renders auto-summaries for classes, methods, and attributes.
170173
-#}
171174
{% include "summary"|get_template with context %}
172175
{% endblock summary %}
173176

174177
{% block source scoped %}
175178
{#- Source block.
176-
179+
177180
This block renders the source code for the class.
178181
-#}
179182
{% if config.show_source %}
@@ -209,7 +212,7 @@ Context:
209212

210213
{% block children scoped %}
211214
{#- Children block.
212-
215+
213216
This block renders the children (members) of the class.
214217
-#}
215218
{% set root = False %}

src/mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html.jinja

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Context:
99

1010
{% block logs scoped %}
1111
{#- Logging block.
12-
12+
1313
This block can be used to log debug messages, deprecation messages, warnings, etc.
1414
-#}
1515
{{ log.debug("Rendering attributes section") }}
@@ -36,7 +36,7 @@ Context:
3636
<td><code><autoref identifier="{{ obj.path }}.{{ attribute.name }}" optional hover>{{ attribute.name }}</autoref></code></td>
3737
<td>
3838
{% if attribute.annotation %}
39-
{% with expression = attribute.annotation, reftype = "returned-by" %}
39+
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
4040
<code>{% include "expression"|get_template with context %}</code>
4141
{% endwith %}
4242
{% endif %}
@@ -60,7 +60,7 @@ Context:
6060
<li class="doc-section-item field-body">
6161
<b><code><autoref identifier="{{ obj.path }}.{{ attribute.name }}" optional hover>{{ attribute.name }}</autoref></code></b>
6262
{% if attribute.annotation %}
63-
{% with expression = attribute.annotation, reftype = "returned-by" %}
63+
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
6464
(<code>{% include "expression"|get_template with context %}</code>)
6565
{% endwith %}
6666
{% endif %}
@@ -94,7 +94,7 @@ Context:
9494
{% if attribute.annotation %}
9595
<span class="doc-attribute-annotation">
9696
<b>TYPE:</b>
97-
{% with expression = attribute.annotation, reftype = "returned-by" %}
97+
{% with expression = attribute.annotation, backlink_type = "returned-by" %}
9898
<code>{% include "expression"|get_template with context %}</code>
9999
{% endwith %}
100100
</span>

src/mkdocstrings_handlers/python/templates/material/_base/docstring/other_parameters.html.jinja

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Context:
99

1010
{% block logs scoped %}
1111
{#- Logging block.
12-
12+
1313
This block can be used to log debug messages, deprecation messages, warnings, etc.
1414
-#}
1515
{{ log.debug("Rendering other parameters section") }}
@@ -36,7 +36,7 @@ Context:
3636
<td><code>{{ parameter.name }}</code></td>
3737
<td>
3838
{% if parameter.annotation %}
39-
{% with expression = parameter.annotation, reftype = "used-by" %}
39+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
4040
<code>{% include "expression"|get_template with context %}</code>
4141
{% endwith %}
4242
{% endif %}
@@ -60,7 +60,7 @@ Context:
6060
<li class="doc-section-item field-body">
6161
<b><code>{{ parameter.name }}</code></b>
6262
{% if parameter.annotation %}
63-
{% with expression = parameter.annotation, reftype = "used-by" %}
63+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
6464
(<code>{% include "expression"|get_template with context %}</code>)
6565
{% endwith %}
6666
{% endif %}
@@ -94,7 +94,7 @@ Context:
9494
{% if parameter.annotation %}
9595
<span class="doc-param-annotation">
9696
<b>{{ lang.t("TYPE:") }}</b>
97-
{% with expression = parameter.annotation, reftype = "used-by" %}
97+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
9898
<code>{% include "expression"|get_template with context %}</code>
9999
{% endwith %}
100100
</span>

src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Context:
99

1010
{% block logs scoped %}
1111
{#- Logging block.
12-
12+
1313
This block can be used to log debug messages, deprecation messages, warnings, etc.
1414
-#}
1515
{{ log.debug("Rendering parameters section") }}
@@ -51,7 +51,7 @@ Context:
5151
</td>
5252
<td>
5353
{% if parameter.annotation %}
54-
{% with expression = parameter.annotation, reftype = "used-by" %}
54+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
5555
<code>{% include "expression"|get_template with context %}</code>
5656
{% endwith %}
5757
{% endif %}
@@ -63,7 +63,7 @@ Context:
6363
</td>
6464
<td>
6565
{% if parameter.default %}
66-
{% with expression = parameter.default, reftype = "used-by" %}
66+
{% with expression = parameter.default, backlink_type = "used-by" %}
6767
<code>{% include "expression"|get_template with context %}</code>
6868
{% endwith %}
6969
{% else %}
@@ -96,10 +96,10 @@ Context:
9696
<b><code>{{ parameter.name }}</code></b>
9797
{% endif %}
9898
{% if parameter.annotation %}
99-
{% with expression = parameter.annotation, reftype = "used-by" %}
99+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
100100
(<code>{% include "expression"|get_template with context %}</code>
101101
{%- if parameter.default %}, {{ lang.t("default:") }}
102-
{% with expression = parameter.default, reftype = "used-by" %}
102+
{% with expression = parameter.default, backlink_type = "used-by" %}
103103
<code>{% include "expression"|get_template with context %}</code>
104104
{% endwith %}
105105
{% endif %})
@@ -149,15 +149,15 @@ Context:
149149
{% if parameter.annotation %}
150150
<span class="doc-param-annotation">
151151
<b>{{ lang.t("TYPE:") }}</b>
152-
{% with expression = parameter.annotation, reftype = "used-by" %}
152+
{% with expression = parameter.annotation, backlink_type = "used-by" %}
153153
<code>{% include "expression"|get_template with context %}</code>
154154
{% endwith %}
155155
</span>
156156
{% endif %}
157157
{% if parameter.default %}
158158
<span class="doc-param-default">
159159
<b>{{ lang.t("DEFAULT:") }}</b>
160-
{% with expression = parameter.default, reftype = "used-by" %}
160+
{% with expression = parameter.default, backlink_type = "used-by" %}
161161
<code>{% include "expression"|get_template with context %}</code>
162162
{% endwith %}
163163
</span>

src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html.jinja

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Context:
99

1010
{% block logs scoped %}
1111
{#- Logging block.
12-
12+
1313
This block can be used to log debug messages, deprecation messages, warnings, etc.
1414
-#}
1515
{{ log.debug("Rendering raises section") }}
@@ -34,7 +34,7 @@ Context:
3434
<tr class="doc-section-item">
3535
<td>
3636
{% if raises.annotation %}
37-
{% with expression = raises.annotation, reftype = "raised-by" %}
37+
{% with expression = raises.annotation, backlink_type = "raised-by" %}
3838
<code>{% include "expression"|get_template with context %}</code>
3939
{% endwith %}
4040
{% endif %}
@@ -57,7 +57,7 @@ Context:
5757
{% for raises in section.value %}
5858
<li class="doc-section-item field-body">
5959
{% if raises.annotation %}
60-
{% with expression = raises.annotation, reftype = "raised-by" %}
60+
{% with expression = raises.annotation, backlink_type = "raised-by" %}
6161
<code>{% include "expression"|get_template with context %}</code>
6262
{% endwith %}
6363
@@ -84,7 +84,7 @@ Context:
8484
<tr class="doc-section-item">
8585
<td>
8686
<span class="doc-raises-annotation">
87-
{% with expression = raises.annotation, reftype = "raised-by" %}
87+
{% with expression = raises.annotation, backlink_type = "raised-by" %}
8888
<code>{% include "expression"|get_template with context %}</code>
8989
{% endwith %}
9090
</span>

0 commit comments

Comments
 (0)