Debian Codegraph @

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

raw
1 {#- Template for expressions.
2
3 This template renders a Griffe expression,
4 which is a tree-like structure representing a Python expression.
5
6 Context:
7 expression (griffe.Expr): The expression to render.
8 config (dict): The configuration options.
9 -#}
10
11 {% block logs scoped %}
12 {#- Logging block.
13
14 This block can be used to log debug messages, deprecation messages, warnings, etc.
15 -#}
16 {% endblock logs %}
17
18 {%- macro crossref(name, annotation_path, backlink_type="") -%}
19 {#- Output a cross-reference.
20
21 This macro outputs a cross-reference to the given name.
22
23 Parameters:
24 name (griffe.ExprName): The name to cross-reference.
25 annotation_path (str): Either "brief", "source", or "full".
26
27 Returns:
28 Either a cross-reference (using an autoref element) or the name itself.
29 -#}
30 {%- if name.classname == "ExprName" and name.is_type_parameter -%}
31 {{ type_param_crossref(name) }}
32 {%- else -%}
33 {{ object_crossref(name, annotation_path) }}
34 {%- endif -%}
35 {%- endmacro -%}
36
37 {%- macro object_crossref(name, annotation_path) -%}
38 {#- Output a cross-reference to a Griffe object.
39
40 This macro outputs a cross-reference to the given name.
41
42 Parameters:
43 name (griffe.ExprName): The name to cross-reference.
44 annotation_path (str): Either "brief", "source", or "full".
45
46 Returns:
47 Either a cross-reference (using an autoref element) or the name itself.
48 -#}
49 {%- with full = name.canonical_path -%}
50 {%- if annotation_path == "brief" -%}
51 {%- set annotation = name.canonical_name -%}
52 {%- elif annotation_path == "source" -%}
53 {%- set annotation = name.name -%}
54 {%- elif annotation_path == "full" -%}
55 {%- set annotation = full -%}
56 {%- endif -%}
57 {%- for prefix, title, path, suffix in annotation|split_path(full) -%}
58 {{ prefix }}
59 {%- if not signature -%}
60 {#- Always render cross-references outside of signatures. We don't need to stash them. -#}
61 <autoref identifier="{{ path }}"{% if backlink_type %} backlink-type="{{ backlink_type }}" backlink-anchor="{{ html_id }}"{% endif %} optional{% if title != path %} hover{% endif %}>{{ title }}</autoref>
62 {%- elif config.signature_crossrefs -%}
63 {#- We're in a signature and cross-references are enabled, we must render one and stash it. -#}
64 {%- filter stash_crossref(length=title|length) -%}
65 <autoref identifier="{{ path }}"{% if backlink_type %} backlink-type="{{ backlink_type }}" backlink-anchor="{{ html_id }}"{% endif %} optional{% if title != path %} hover{% endif %}>{{ title }}</autoref>
66 {%- endfilter -%}
67 {%- else -%}
68 {#- We're in a signature but cross-references are disabled, we just render the title. -#}
69 {{ title }}
70 {%- endif -%}
71 {{ suffix }}
72 {%- endfor -%}
73 {%- endwith -%}
74 {%- endmacro -%}
75
76 {%- macro type_param_crossref(name) -%}
77 {#- Render a cross-reference to a type parameter heading.
78
79 Parameters:
80 name (griffe.ExprName): The name to cross-reference.
81
82 Returns:
83 The autorefs cross-reference, or the type parameter name.
84 -#}
85 {%- if not signature -%}
86 <autoref identifier="{{ name.canonical_path }}" optional{% if name.name not in obj.type_parameters %} hover{% endif %}>{{ name.name }}</autoref>
87 {%- elif config.signature_crossrefs -%}
88 {%- filter stash_crossref(length=name.name|length) -%}
89 <autoref identifier="{{ name.canonical_path }}" optional{% if name.name not in obj.type_parameters %} hover{% endif %}>{{ name.name }}</autoref>
90 {%- endfilter -%}
91 {%- else -%}
92 {{ name.name }}
93 {%- endif -%}
94 {%- endmacro -%}
95
96 {%- macro param_crossref(expression) -%}
97 {#- Render a cross-reference to a parameter heading.
98
99 Parameters:
100 expression (griffe.expressions.Expr): The expression to render.
101
102 Returns:
103 The autorefs cross-reference, or the parameter name.
104 -#}
105 {%- if config.signature_crossrefs -%}
106 {%- if signature -%}
107 {%- filter stash_crossref(length=expression.name|length) -%}
108 <autoref identifier="{{ expression.canonical_path }}" optional hover>{{ expression.name }}</autoref>
109 {%- endfilter -%}
110 {%- else -%}
111 <autoref identifier="{{ expression.canonical_path }}" optional hover>{{ expression.name }}</autoref>
112 {%- endif -%}
113 {%- else -%}
114 {{ expression.name }}
115 {%- endif -%}
116 {%- endmacro -%}
117
118 {%- macro render(expression, annotations_path, backlink_type="") -%}
119 {#- Render an expression.
120
121 Parameters:
122 expression (griffe.Expr): The expression to render.
123 annotations_path (str): Either "brief", "source", or "full".
124
125 Returns:
126 The rendered expression.
127 -#}
128 {%- if expression is string -%}
129 {%- if signature -%}{{ expression|safe }}{%- else -%}{{ expression }}{%- endif -%}
130 {%- elif expression.classname == "ExprName" -%}
131 {{ crossref(expression, annotations_path, backlink_type) }}
132 {%- elif config.unwrap_annotated and expression.classname == "ExprSubscript" and expression.canonical_path in ("typing.Annotated", "typing_extensions.Annotated") -%}
133 {{ render(expression.slice.elements[0], annotations_path) }}
134 {%- elif expression.classname == "ExprAttribute" -%}
135 {%- if expression.first.is_type_parameter -%}
136 {{ type_param_crossref(expression.first) }}
137 {%- for element in expression -%}
138 {%- if not loop.first -%}
139 {{ render(element, "brief") }}
140 {%- endif -%}
141 {%- endfor -%}
142 {%- elif annotations_path == "brief" -%}
143 {%- if expression.last.is_enum_value -%}
144 {{ crossref(expression.last.parent, "brief", backlink_type) }}.value
145 {%- else -%}
146 {{ render(expression.last, "brief") }}
147 {%- endif -%}
148 {%- elif annotations_path == "full" -%}
149 {{ render(expression.first, "full") }}
150 {%- for element in expression -%}
151 {%- if not loop.first -%}
152 {{ render(element, "brief") }}
153 {%- endif -%}
154 {%- endfor -%}
155 {%- else -%}
156 {%- for element in expression -%}
157 {{ render(element, annotations_path) }}
158 {%- endfor -%}
159 {%- endif -%}
160 {%- elif expression.classname == "ExprKeyword" -%}
161 {{ param_crossref(expression) }}={{ render(expression.value, annotations_path) }}
162 {%- else -%}
163 {%- for element in expression -%}
164 {{ render(element, annotations_path) }}
165 {%- endfor -%}
166 {%- endif -%}
167 {%- endmacro -%}
168
169 {%- if config.modernize_annotations and expression is not string -%}
170 {%- set expression = expression.modernize() -%}
171 {%- endif -%}
172 {{ render(expression, annotations_path|default(config.annotations_path), backlink_type|default("")) }}