Skip to content

Commit 9b8e1b1

Browse files
committed
feat: Support new Griffe expressions (in v0.33)
1 parent 4fe3d20 commit 9b8e1b1

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
]
3131
dependencies = [
3232
"mkdocstrings>=0.20",
33-
"griffe>=0.30,<0.33",
33+
"griffe>=0.33",
3434
]
3535

3636
[project.urls]

src/mkdocstrings_handlers/python/handler.py

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
340340
self.env.trim_blocks = True
341341
self.env.lstrip_blocks = True
342342
self.env.keep_trailing_newline = False
343+
self.env.filters["split_path"] = rendering.do_split_path
343344
self.env.filters["crossref"] = rendering.do_crossref
344345
self.env.filters["multi_crossref"] = rendering.do_multi_crossref
345346
self.env.filters["order_members"] = rendering.do_order_members

src/mkdocstrings_handlers/python/rendering.py

+22
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ def repl(match: Match) -> str:
195195
return Markup(text).format(**variables)
196196

197197

198+
def do_split_path(path: str, full_path: str) -> list[tuple[str, str]]:
199+
"""Split object paths for building cross-references.
200+
201+
Parameters:
202+
path: The path to split.
203+
204+
Returns:
205+
A list of pairs (title, full path).
206+
"""
207+
if "." not in path:
208+
return [(path, full_path)]
209+
pairs = []
210+
full_path = ""
211+
for part in path.split("."):
212+
if full_path:
213+
full_path += f".{part}"
214+
else:
215+
full_path = part
216+
pairs.append((part, full_path))
217+
return pairs
218+
219+
198220
def _keep_object(name: str, filters: Sequence[tuple[Pattern, bool]]) -> bool:
199221
keep = None
200222
rules = set()
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1-
{%- set original_expression = expression -%}
2-
{%- if original_expression is iterable and original_expression is not string -%}
3-
{%- for expression in original_expression -%}
4-
{%- include "expression.html" with context -%}
5-
{%- endfor -%}
6-
{%- elif original_expression is string -%}
7-
{{ original_expression }}
8-
{%- else -%}
9-
{%- with annotation = original_expression|attr(config.annotations_path) -%}
10-
{%- filter stash_crossref(length=annotation|length) -%}
11-
<span data-autorefs-optional{% if annotation != original_expression.full %}-hover{% endif %}="{{ original_expression.full }}">{{ annotation }}</span>
12-
{%- endfilter -%}
1+
{%- macro crossref(name, annotation_path) -%}
2+
{%- with full = name.canonical_path -%}
3+
{%- if annotation_path == "brief" -%}
4+
{%- set annotation = name.canonical_name -%}
5+
{%- elif annotation_path == "source" -%}
6+
{%- set annotation = name.name -%}
7+
{%- elif annotation_path == "full" -%}
8+
{%- set annotation = full -%}
9+
{%- endif -%}
10+
{%- for title, path in annotation|split_path(full) -%}
11+
{%- filter stash_crossref(length=title|length) -%}
12+
<span data-autorefs-optional{% if title != path %}-hover{% endif %}="{{ path }}">{{ title }}</span>
13+
{%- endfilter -%}
14+
{%- if not loop.last -%}.{%- endif -%}
15+
{%- endfor -%}
1316
{%- endwith -%}
14-
{%- endif -%}
17+
{%- endmacro -%}
18+
19+
{%- macro render(expression, annotations_path) -%}
20+
{%- if expression is string -%}
21+
{%- if signature -%}{{ expression|safe }}{%- else -%}{{ expression }}{%- endif -%}
22+
{%- elif expression.classname == "ExprName" -%}
23+
{{ crossref(expression, annotations_path) }}
24+
{%- elif expression.classname == "ExprAttribute" -%}
25+
{%- if annotations_path == "brief" -%}
26+
{{ render(expression.last, "brief") }}
27+
{%- elif annotations_path == "full" -%}
28+
{{ render(expression.first, "full") }}
29+
{%- for element in expression -%}
30+
{%- if not loop.first -%}
31+
{{ render(element, "brief") }}
32+
{%- endif -%}
33+
{%- endfor -%}
34+
{%- else -%}
35+
{%- for element in expression -%}
36+
{{ render(element, annotations_path) }}
37+
{%- endfor -%}
38+
{%- endif -%}
39+
{%- else -%}
40+
{%- for element in expression -%}
41+
{{ render(element, annotations_path) }}
42+
{%- endfor -%}
43+
{%- endif -%}
44+
{%- endmacro -%}
45+
46+
{{ render(expression, config.annotations_path) }}

0 commit comments

Comments
 (0)