Skip to content

Commit 31877a7

Browse files
committed
Prevent unwanted whitespace around method headings
1 parent de9a1f0 commit 31877a7

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

mkdocstrings/handlers/crystal/renderer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ def update_env(self, md: Markdown, config: dict) -> None:
6666
self.env.filters["convert_markdown"] = self.do_convert_markdown
6767
self.env.filters["reference"] = self.do_reference
6868

69-
def do_code_highlight(self, code, **kwargs) -> str:
69+
def do_code_highlight(self, code, *, title: str = "", **kwargs) -> str:
7070
text = str(code)
7171
stext = text.lstrip()
7272
indent = text[: len(text) - len(stext)]
73-
html = indent + self.env.filters["highlight"](stext, **kwargs)
73+
html = self.env.filters["highlight"](stext, **kwargs)
74+
# HACK: Replace the end of the first tag with injected content.
75+
tag_end = Markup(">")
76+
if indent:
77+
html = html.replace(tag_end, tag_end + indent, 1)
7478
if isinstance(code, crystal_html.TextWithLinks):
7579
html = crystal_html.linkify_highlighted_html(html, code.tokens, self.do_reference)
80+
if title:
81+
prefix = Markup('<span class="doc-title">{}</span>').format(title)
82+
html = html.replace(tag_end, tag_end + prefix, 1)
7683
return html
7784

7885
def do_reference(self, path: Union[str, DocPath], text: Optional[str] = None) -> str:

mkdocstrings/templates/crystal/material/constant.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{{ log.debug() }}
22

33
<div class="doc doc-object doc-{{ obj.kind }}">
4-
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.name) %}
5-
<code>{{ obj.name }} = <span style="font-weight: normal">{{ obj.value |code_highlight(language="crystal", inline=True) }}</span></code>
6-
{% endfilter %}
4+
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.name) -%}
5+
{{ obj.value |code_highlight(title=obj.name + " = ", language="crystal", inline=True) }}
6+
{%- endfilter %}
77

88
<div class="doc doc-contents {% if root %}first{% endif %}">
99
{% if obj.doc %}{{ obj.doc |convert_markdown(obj, heading_level, obj.abs_id) }}{% endif %}

mkdocstrings/templates/crystal/material/method.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{{ log.debug() }}
22

33
<div class="doc doc-object doc-method doc-{{ obj.kind }}">
4-
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.short_name) %}
5-
{% if obj.is_abstract %}<small style="font-weight: normal">abstract</small> {% endif %}
6-
<code>{{ obj.short_name }}<span style="font-weight: normal">{{ obj.args_string |code_highlight(language="crystal", inline=True) }}</span></code>
7-
{% endfilter %}
4+
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.short_name) -%}
5+
{% if obj.is_abstract %}<small>abstract</small> {% endif %}
6+
{{ obj.args_string |code_highlight(title=obj.short_name, language="crystal", inline=True) }}
7+
{%- endfilter %}
88

99
<div class="doc doc-contents {% if root %}first{% endif %}">
1010
{% if obj.doc %}{{ obj.doc |convert_markdown(obj, heading_level, obj.abs_id) }}{% endif %}

mkdocstrings/templates/crystal/material/type.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
<div class="doc doc-object doc-type {{ obj.kind }}">
44
{% if obj.parent %}
5-
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.name) %}
5+
{% filter heading(heading_level, id=obj.abs_id, class="doc doc-heading", toc_label=obj.name) -%}
66
{% if obj.is_abstract %}abstract {% endif %}{{ obj.kind }} <code>{{ obj.full_name }}</code>
77
{% if obj.superclass %}
88
<br/><small>inherits <code>{{ obj.superclass |reference }}</code></small>
99
{% endif %}
10-
{% endfilter %}
10+
{%- endfilter %}
1111
{% endif %}
1212

1313
<div class="doc doc-contents {% if root %}first{% endif %}">

0 commit comments

Comments
 (0)