| 1 |
{#- Template for Python classes. |
| 2 |
|
| 3 |
This template renders a Python class. |
| 4 |
|
| 5 |
Context: |
| 6 |
class (griffe.Class): The class to render. |
| 7 |
root (bool): Whether this is the root object, injected with `:::` in a Markdown page. |
| 8 |
heading_level (int): The HTML heading level to use. |
| 9 |
config (dict): The configuration options. |
| 10 |
-#} |
| 11 |
|
| 12 |
{% block logs scoped %} |
| 13 |
{#- Logging block. |
| 14 |
|
| 15 |
This block can be used to log debug messages, deprecation messages, warnings, etc. |
| 16 |
-#} |
| 17 |
{{ log.debug("Rendering " + class.path) }} |
| 18 |
{% endblock logs %} |
| 19 |
|
| 20 |
{% import "language.html.jinja" as lang with context %} |
| 21 |
{#- Language module providing the `t` translation method. -#} |
| 22 |
|
| 23 |
<div class="doc doc-object doc-class"> |
| 24 |
{% with obj = class, html_id = class.path, all_members = class.all_members %} |
| 25 |
|
| 26 |
{% if root %} |
| 27 |
{% set show_full_path = config.show_root_full_path %} |
| 28 |
{% set root_members = True %} |
| 29 |
{% elif root_members %} |
| 30 |
{% set show_full_path = config.show_root_members_full_path or config.show_object_full_path %} |
| 31 |
{% set root_members = False %} |
| 32 |
{% else %} |
| 33 |
{% set show_full_path = config.show_object_full_path %} |
| 34 |
{% endif %} |
| 35 |
|
| 36 |
{% set class_name = class.path if show_full_path else class.name %} |
| 37 |
|
| 38 |
{% if not root or config.show_root_heading %} |
| 39 |
{% filter heading( |
| 40 |
heading_level, |
| 41 |
role="class", |
| 42 |
id=html_id, |
| 43 |
class="doc doc-heading", |
| 44 |
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code> '|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else class.name), |
| 45 |
skip_inventory=config.skip_local_inventory, |
| 46 |
) %} |
| 47 |
|
| 48 |
{% block heading scoped %} |
| 49 |
{#- Heading block. |
| 50 |
|
| 51 |
This block renders the heading for the class. |
| 52 |
-#} |
| 53 |
{% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-class"></code>{% endif %} |
| 54 |
{% if config.heading and root %} |
| 55 |
{{ config.heading }} |
| 56 |
{% elif config.separate_signature %} |
| 57 |
<span class="doc doc-object-name doc-class-name">{{ class_name }}</span> |
| 58 |
{% elif config.merge_init_into_class and "__init__" in all_members %} |
| 59 |
{% with function = all_members["__init__"] %} |
| 60 |
{%+ filter highlight(language="python", inline=True) %} |
| 61 |
{{ class_name -}} |
| 62 |
{%- with obj = function -%} |
| 63 |
{%- include "type_parameters.html.jinja" with context -%} |
| 64 |
{%- endwith -%} |
| 65 |
{%- include "signature.html.jinja" with context -%} |
| 66 |
{% endfilter %} |
| 67 |
{% endwith %} |
| 68 |
{% else %} |
| 69 |
{# TODO: Maybe render type parameters here. #} |
| 70 |
<code>{{ class_name }}</code> |
| 71 |
{% endif %} |
| 72 |
{% endblock heading %} |
| 73 |
|
| 74 |
{% block labels scoped %} |
| 75 |
{#- Labels block. |
| 76 |
|
| 77 |
This block renders the labels for the class. |
| 78 |
-#} |
| 79 |
{% with labels = class.labels %} |
| 80 |
{% include "labels.html.jinja" with context %} |
| 81 |
{% endwith %} |
| 82 |
{% endblock labels %} |
| 83 |
|
| 84 |
{% endfilter %} |
| 85 |
|
| 86 |
{% block signature scoped %} |
| 87 |
{#- Signature block. |
| 88 |
|
| 89 |
This block renders the signature for the class. |
| 90 |
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled. |
| 91 |
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled. |
| 92 |
|
| 93 |
If the class is generic, but the `__init__` method isn't or `merge_init_into_class` is disabled, |
| 94 |
the class signature is rendered if `separate_signature` and `show_signature_type_parameters` are enabled. |
| 95 |
|
| 96 |
If the `__init__` method or any overloads are generic, they are rendered as methods if |
| 97 |
`merge_init_into_class`, `separate_signature` and `show_signature_type_parameters` are enabled. |
| 98 |
-#} |
| 99 |
{% if config.merge_init_into_class and "__init__" in all_members %} |
| 100 |
{% with function = all_members["__init__"] %} |
| 101 |
{% if function.overloads and config.show_overloads %} |
| 102 |
<div class="doc-overloads"> |
| 103 |
{% for overload in function.overloads %} |
| 104 |
{% filter format_signature(overload, config.line_length, annotations=True, crossrefs=config.signature_crossrefs) %} |
| 105 |
{{ class.name }} |
| 106 |
{% endfilter %} |
| 107 |
{% endfor %} |
| 108 |
</div> |
| 109 |
{% endif %} |
| 110 |
{% if config.separate_signature and not (config.show_overloads and function.overloads and config.overloads_only) %} |
| 111 |
{% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %} |
| 112 |
{{ class.name }} |
| 113 |
{% endfilter %} |
| 114 |
{% endif %} |
| 115 |
{% endwith %} |
| 116 |
{% endif %} |
| 117 |
{% endblock signature %} |
| 118 |
|
| 119 |
{% else %} |
| 120 |
{% if config.show_root_toc_entry %} |
| 121 |
{% filter heading(heading_level, |
| 122 |
role="class", |
| 123 |
id=html_id, |
| 124 |
toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-class"></code> '|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else class.name), |
| 125 |
hidden=True, |
| 126 |
skip_inventory=config.skip_local_inventory, |
| 127 |
) %} |
| 128 |
{% endfilter %} |
| 129 |
{% endif %} |
| 130 |
{% set heading_level = heading_level - 1 %} |
| 131 |
{% endif %} |
| 132 |
|
| 133 |
<div class="doc doc-contents {% if root %}first{% endif %}"> |
| 134 |
{% block contents scoped %} |
| 135 |
{#- Contents block. |
| 136 |
|
| 137 |
This block renders the contents of the class. |
| 138 |
It contains other blocks that users can override. |
| 139 |
Overriding the contents block allows to rearrange the order of the blocks. |
| 140 |
-#} |
| 141 |
{% block bases scoped %} |
| 142 |
{#- Class bases block. |
| 143 |
|
| 144 |
This block renders the bases for the class. |
| 145 |
-#} |
| 146 |
{% if config.show_bases and class.bases %} |
| 147 |
<p class="doc doc-class-bases"> |
| 148 |
Bases: {% for expression in class.bases -%} |
| 149 |
<code> |
| 150 |
{%- with backlink_type = "subclassed-by" -%} |
| 151 |
{%- include "expression.html.jinja" with context -%} |
| 152 |
{%- endwith -%} |
| 153 |
</code>{% if not loop.last %}, {% endif %} |
| 154 |
{% endfor -%} |
| 155 |
</p> |
| 156 |
{% endif %} |
| 157 |
{% endblock bases %} |
| 158 |
|
| 159 |
{% block inheritance_diagram scoped %} |
| 160 |
{#- Inheritance diagram block. |
| 161 |
|
| 162 |
This block renders the inheritance diagram for the class, |
| 163 |
using Mermaid syntax and a bit of JavaScript to make the nodes clickable, |
| 164 |
linking to the corresponding class documentation. |
| 165 |
-#} |
| 166 |
{% if config.show_inheritance_diagram and class.bases %} |
| 167 |
{% macro edges(class) %} |
| 168 |
{% for base in class.resolved_bases %} |
| 169 |
{{ base.path }} --> {{ class.path }} |
| 170 |
{{ edges(base) }} |
| 171 |
{% endfor %} |
| 172 |
{% endmacro %} |
| 173 |
<div style="display: none"> |
| 174 |
<autoref identifier="{{ class.path }}" optional id="mermaid-link-{{ class.path }}"></autoref> |
| 175 |
{% for base in class.mro() %} |
| 176 |
<autoref identifier="{{ base.path }}" optional id="mermaid-link-{{ base.path }}"></autoref> |
| 177 |
{% endfor %} |
| 178 |
</div> |
| 179 |
<pre class="mermaid"><code id="mermaid-diagram-{{ class.path }}"> |
| 180 |
flowchart {{ config.inheritance_diagram_direction }} |
| 181 |
{{ class.path }}[{{ class.name }}] |
| 182 |
{% for base in class.mro() %} |
| 183 |
{{ base.path }}[{{ base.name }}] |
| 184 |
{% endfor %} |
| 185 |
|
| 186 |
{{ edges(class) | safe }} |
| 187 |
|
| 188 |
click {{ class.path }} href "" "{{ class.path }}" |
| 189 |
{% for base in class.mro() %} |
| 190 |
click {{ base.path }} href "" "{{ base.path }}" |
| 191 |
{% endfor %} |
| 192 |
</code></pre> |
| 193 |
<script> |
| 194 |
var diagram = document.getElementById('mermaid-diagram-{{ class.path }}'); |
| 195 |
diagram.innerHTML = diagram.innerHTML.replace(/click ([\w.]+) href "" "\1"/g, function(match, nodeID, offset) { |
| 196 |
try { |
| 197 |
const link = document.getElementById("mermaid-link-" + nodeID).href; |
| 198 |
return `click ${nodeID} href "${link}" "${nodeID}"` |
| 199 |
} catch (e) { |
| 200 |
return ''; |
| 201 |
} |
| 202 |
}); |
| 203 |
</script> |
| 204 |
{% endif %} |
| 205 |
{% endblock inheritance_diagram %} |
| 206 |
|
| 207 |
{% block docstring scoped %} |
| 208 |
{#- Docstring block. |
| 209 |
|
| 210 |
This block renders the docstring for the class. |
| 211 |
-#} |
| 212 |
{% with docstring_sections = class.docstring.parsed %} |
| 213 |
{% include "docstring.html.jinja" with context %} |
| 214 |
{% endwith %} |
| 215 |
{% if config.merge_init_into_class %} |
| 216 |
{# We don't want to merge the inherited `__init__` method docstring into the class docstring #} |
| 217 |
{# if such inherited method was not selected through `inherited_members`. #} |
| 218 |
{% with check_members = all_members if (config.inherited_members is true or (config.inherited_members is iterable and "__init__" in config.inherited_members)) else class.members %} |
| 219 |
{% if "__init__" in check_members and check_members["__init__"].has_docstring %} |
| 220 |
{% with function = check_members["__init__"] %} |
| 221 |
{% with obj = function, docstring_sections = function.docstring.parsed %} |
| 222 |
{% include "docstring.html.jinja" with context %} |
| 223 |
{% endwith %} |
| 224 |
{% endwith %} |
| 225 |
{% endif %} |
| 226 |
{% endwith %} |
| 227 |
{% endif %} |
| 228 |
{% endblock docstring %} |
| 229 |
|
| 230 |
{% if config.backlinks %} |
| 231 |
<backlinks identifier="{{ html_id }}" handler="python" /> |
| 232 |
{% endif %} |
| 233 |
|
| 234 |
{% block summary scoped %} |
| 235 |
{#- Summary block. |
| 236 |
|
| 237 |
This block renders auto-summaries for classes, methods, and attributes. |
| 238 |
-#} |
| 239 |
{% include "summary.html.jinja" with context %} |
| 240 |
{% endblock summary %} |
| 241 |
|
| 242 |
{% block source scoped %} |
| 243 |
{#- Source block. |
| 244 |
|
| 245 |
This block renders the source code for the class. |
| 246 |
-#} |
| 247 |
{% if config.show_source %} |
| 248 |
{% if config.merge_init_into_class %} |
| 249 |
{% if "__init__" in all_members and all_members["__init__"].source %} |
| 250 |
{% with init = all_members["__init__"] %} |
| 251 |
<details class="mkdocstrings-source"> |
| 252 |
<summary>{{ lang.t("Source code in") }} <code>{{ init|source_location }}</code></summary> |
| 253 |
{{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }} |
| 254 |
</details> |
| 255 |
{% endwith %} |
| 256 |
{% endif %} |
| 257 |
{% elif class.source %} |
| 258 |
<details class="mkdocstrings-source"> |
| 259 |
<summary>{{ lang.t("Source code in") }} <code>{{ class|source_location }}</code></summary> |
| 260 |
{{ class.source|highlight(language="python", linestart=class.lineno or 0, linenums=True) }} |
| 261 |
</details> |
| 262 |
{% endif %} |
| 263 |
{% endif %} |
| 264 |
{% endblock source %} |
| 265 |
|
| 266 |
{% block children scoped %} |
| 267 |
{#- Children block. |
| 268 |
|
| 269 |
This block renders the children (members) of the class. |
| 270 |
-#} |
| 271 |
{% set root = False %} |
| 272 |
{% set heading_level = heading_level + 1 %} |
| 273 |
{% include "children.html.jinja" with context %} |
| 274 |
{% endblock children %} |
| 275 |
{% endblock contents %} |
| 276 |
</div> |
| 277 |
|
| 278 |
{% endwith %} |
| 279 |
</div> |