Skip to content

Commit d98813f

Browse files
committed
refactor: Make source admonitions more subtle
Issue-mkdocstrings#525: mkdocstrings/mkdocstrings#525
1 parent 1b064a0 commit d98813f

File tree

4 files changed

+95
-56
lines changed

4 files changed

+95
-56
lines changed

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ plugins:
174174
show_root_heading: true
175175
show_root_full_path: false
176176
show_signature_annotations: true
177-
show_source: false
177+
show_source: true
178178
show_symbol_type_heading: true
179179
show_symbol_type_toc: true
180180
signature_crossrefs: true

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

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,43 @@ Context:
102102
{% endif %}
103103

104104
<div class="doc doc-contents {% if root %}first{% endif %}">
105+
106+
{% block source scoped %}
107+
{#- Source block.
108+
109+
This block renders the source code for the class.
110+
-#}
111+
{% if config.show_source %}
112+
{% if config.merge_init_into_class %}
113+
{% if "__init__" in class.all_members and class.all_members["__init__"].source %}
114+
{% with init = class.all_members["__init__"] %}
115+
<details class="source">
116+
<summary>Source code in <code>
117+
{%- if init.relative_filepath.is_absolute() -%}
118+
{{ init.relative_package_filepath }}
119+
{%- else -%}
120+
{{ init.relative_filepath }}
121+
{%- endif -%}
122+
</code></summary>
123+
{{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }}
124+
</details>
125+
{% endwith %}
126+
{% endif %}
127+
{% elif class.source %}
128+
<details class="source">
129+
<summary>Source code in <code>
130+
{%- if class.relative_filepath.is_absolute() -%}
131+
{{ class.relative_package_filepath }}
132+
{%- else -%}
133+
{{ class.relative_filepath }}
134+
{%- endif -%}
135+
</code></summary>
136+
{{ class.source|highlight(language="python", linestart=class.lineno or 0, linenums=True) }}
137+
</details>
138+
{% endif %}
139+
{% endif %}
140+
{% endblock source %}
141+
105142
{% block contents scoped %}
106143
{#- Contents block.
107144
@@ -150,42 +187,6 @@ Context:
150187
{% include "summary"|get_template with context %}
151188
{% endblock summary %}
152189

153-
{% block source scoped %}
154-
{#- Source block.
155-
156-
This block renders the source code for the class.
157-
-#}
158-
{% if config.show_source %}
159-
{% if config.merge_init_into_class %}
160-
{% if "__init__" in class.all_members and class.all_members["__init__"].source %}
161-
{% with init = class.all_members["__init__"] %}
162-
<details class="quote">
163-
<summary>Source code in <code>
164-
{%- if init.relative_filepath.is_absolute() -%}
165-
{{ init.relative_package_filepath }}
166-
{%- else -%}
167-
{{ init.relative_filepath }}
168-
{%- endif -%}
169-
</code></summary>
170-
{{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }}
171-
</details>
172-
{% endwith %}
173-
{% endif %}
174-
{% elif class.source %}
175-
<details class="quote">
176-
<summary>Source code in <code>
177-
{%- if class.relative_filepath.is_absolute() -%}
178-
{{ class.relative_package_filepath }}
179-
{%- else -%}
180-
{{ class.relative_filepath }}
181-
{%- endif -%}
182-
</code></summary>
183-
{{ class.source|highlight(language="python", linestart=class.lineno or 0, linenums=True) }}
184-
</details>
185-
{% endif %}
186-
{% endif %}
187-
{% endblock source %}
188-
189190
{% block children scoped %}
190191
{#- Children block.
191192

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Context:
8080
This block renders the signature for the function,
8181
as well as its overloaded signatures if any.
8282
-#}
83+
{% with source = function.lines[function.docstring.endlineno-function.lineno+1:] %}
84+
{% if source %}
85+
<details class="source">
86+
<summary>
87+
{% endif %}
8388
{% if function.overloads %}
8489
<div class="doc-overloads">
8590
{% for overload in function.overloads %}
@@ -94,6 +99,12 @@ Context:
9499
{{ function.name }}
95100
{% endfilter %}
96101
{% endif %}
102+
{% if source %}
103+
</summary>
104+
{{ source|join("\n")|highlight(language="python", linestart=function.docstring.endlineno+1 or 0, linenums=True) }}
105+
</details>
106+
{% endif %}
107+
{% endwith %}
97108
{% endblock signature %}
98109

99110
{% else %}
@@ -119,6 +130,7 @@ Context:
119130
It contains other blocks that users can override.
120131
Overriding the contents block allows to rearrange the order of the blocks.
121132
-#}
133+
<a href="/sources/{{ function.relative_package_filepath.with_suffix('') }}/" target="_blank">View source</a>
122134
{% block docstring scoped %}
123135
{#- Docstring block.
124136
@@ -128,25 +140,6 @@ Context:
128140
{% include "docstring"|get_template with context %}
129141
{% endwith %}
130142
{% endblock docstring %}
131-
132-
{% block source scoped %}
133-
{#- Source block.
134-
135-
This block renders the source code for the function.
136-
-#}
137-
{% if config.show_source and function.source %}
138-
<details class="quote">
139-
<summary>{{ lang.t("Source code in") }} <code>
140-
{%- if function.relative_filepath.is_absolute() -%}
141-
{{ function.relative_package_filepath }}
142-
{%- else -%}
143-
{{ function.relative_filepath }}
144-
{%- endif -%}
145-
</code></summary>
146-
{{ function.source|highlight(language="python", linestart=function.lineno or 0, linenums=True) }}
147-
</details>
148-
{% endif %}
149-
{% endblock source %}
150143
{% endblock contents %}
151144
</div>
152145

src/mkdocstrings_handlers/python/templates/material/style.css

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,48 @@ code.doc-symbol-module::after {
140140
color: inherit;
141141
border-bottom: 1px dotted currentcolor;
142142
}
143+
144+
:root {
145+
--md-admonition-icon--source: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m14.6 16.6 4.6-4.6-4.6-4.6L16 6l6 6-6 6zm-5.2 0L4.8 12l4.6-4.6L8 6l-6 6 6 6z"/></svg>')
146+
/* --md-admonition-icon--source: "" */
147+
}
148+
.md-typeset .admonition.source,
149+
.md-typeset details.source {
150+
border-color: var(--md-code-bg-color);
151+
}
152+
.md-typeset .source > .admonition-title,
153+
.md-typeset .source > summary {
154+
background-color: var(--md-code-bg-color);
155+
}
156+
.md-typeset .source > .admonition-title::before,
157+
.md-typeset .source > summary::before {
158+
/* background-color: rgb(128, 128, 128); */
159+
background-color: var(--md-code-bg-color);
160+
/* -webkit-mask-image: var(--md-admonition-icon--source);
161+
mask-image: var(--md-admonition-icon--source); */
162+
-webkit-mask-image: unset;
163+
mask-image: unset;
164+
}
165+
166+
.doc details.source {
167+
background-color: var(--md-code-bg-color);
168+
border: 0;
169+
border-radius: 0;
170+
margin: 0;
171+
padding: 0;
172+
transition: none;
173+
}
174+
175+
.doc details.source summary {
176+
background-color: inherit;
177+
border: 0;
178+
border-radius: 0;
179+
padding-top: 0;
180+
padding-bottom: 0;
181+
}
182+
183+
184+
.doc details.source summary code {
185+
box-shadow: none;
186+
padding: 0;
187+
}

0 commit comments

Comments
 (0)