Skip to content

feat: Support new Griffe expressions (in v0.33) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
]
dependencies = [
"mkdocstrings>=0.20",
"griffe>=0.30,<0.33",
"griffe>=0.33",
]

[project.urls]
Expand Down
1 change: 1 addition & 0 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
self.env.trim_blocks = True
self.env.lstrip_blocks = True
self.env.keep_trailing_newline = False
self.env.filters["split_path"] = rendering.do_split_path
self.env.filters["crossref"] = rendering.do_crossref
self.env.filters["multi_crossref"] = rendering.do_multi_crossref
self.env.filters["order_members"] = rendering.do_order_members
Expand Down
22 changes: 22 additions & 0 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ def repl(match: Match) -> str:
return Markup(text).format(**variables)


def do_split_path(path: str, full_path: str) -> list[tuple[str, str]]:
"""Split object paths for building cross-references.

Parameters:
path: The path to split.

Returns:
A list of pairs (title, full path).
"""
if "." not in path:
return [(path, full_path)]
pairs = []
full_path = ""
for part in path.split("."):
if full_path:
full_path += f".{part}"
else:
full_path = part
pairs.append((part, full_path))
return pairs


def _keep_object(name: str, filters: Sequence[tuple[Pattern, bool]]) -> bool:
keep = None
rules = set()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
{%- set original_expression = expression -%}
{%- if original_expression is iterable and original_expression is not string -%}
{%- for expression in original_expression -%}
{%- include "expression.html" with context -%}
{%- endfor -%}
{%- elif original_expression is string -%}
{{ original_expression }}
{%- else -%}
{%- with annotation = original_expression|attr(config.annotations_path) -%}
{%- filter stash_crossref(length=annotation|length) -%}
<span data-autorefs-optional{% if annotation != original_expression.full %}-hover{% endif %}="{{ original_expression.full }}">{{ annotation }}</span>
{%- endfilter -%}
{%- macro crossref(name, annotation_path) -%}
{%- with full = name.canonical_path -%}
{%- if annotation_path == "brief" -%}
{%- set annotation = name.canonical_name -%}
{%- elif annotation_path == "source" -%}
{%- set annotation = name.name -%}
{%- elif annotation_path == "full" -%}
{%- set annotation = full -%}
{%- endif -%}
{%- for title, path in annotation|split_path(full) -%}
{%- filter stash_crossref(length=title|length) -%}
<span data-autorefs-optional{% if title != path %}-hover{% endif %}="{{ path }}">{{ title }}</span>
{%- endfilter -%}
{%- if not loop.last -%}.{%- endif -%}
{%- endfor -%}
{%- endwith -%}
{%- endif -%}
{%- endmacro -%}

{%- macro render(expression, annotations_path) -%}
{%- if expression is string -%}
{%- if signature -%}{{ expression|safe }}{%- else -%}{{ expression }}{%- endif -%}
{%- elif expression.classname == "ExprName" -%}
{{ crossref(expression, annotations_path) }}
{%- elif expression.classname == "ExprAttribute" -%}
{%- if annotations_path == "brief" -%}
{{ render(expression.last, "brief") }}
{%- elif annotations_path == "full" -%}
{{ render(expression.first, "full") }}
{%- for element in expression -%}
{%- if not loop.first -%}
{{ render(element, "brief") }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{%- for element in expression -%}
{{ render(element, annotations_path) }}
{%- endfor -%}
{%- endif -%}
{%- else -%}
{%- for element in expression -%}
{{ render(element, annotations_path) }}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}

{{ render(expression, config.annotations_path) }}