Skip to content

Commit 58e7126

Browse files
committed
feat: Improve spacing in templates directly
Issue-19: #19
1 parent 3594d42 commit 58e7126

28 files changed

Lines changed: 525 additions & 189 deletions

config/ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ignore = [
5353
"ARG005", # Unused lambda argument
5454
"FBT001", # Boolean positional arg in function definition
5555
"PLR2004", # Magic value used in comparison
56+
"RUF001", # String contains ambiguous `–` (EN DASH)
5657
"S101", # Use of assert detected
5758
]
5859

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ maintain = [
8585
ci = [
8686
"duty>=1.6",
8787
"griffe>=2.0",
88+
"inline-snapshot>=0.6",
8889
"pytest>=8.2",
8990
"pytest-cov>=5.0",
9091
"pytest-randomly>=3.15",

src/griffe2md/_internal/rendering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def do_order_members(
261261

262262
def do_heading(content: str, heading_level: int) -> str:
263263
"""Render a Markdown heading."""
264-
return f"\n{'#' * heading_level} {content}\n\n"
264+
return f"{'#' * heading_level} {content}"
265265

266266

267267
def do_split_path(path: str, full_path: str) -> list[tuple[str, str]]:
@@ -497,4 +497,4 @@ def do_as_modules_section(modules: Sequence[Module], *, check_public: bool = Tru
497497

498498

499499
def _newline_to_br(text: str) -> str:
500-
return text.replace("\n\n", "<br>").replace("\n", " ")
500+
return text.replace("\n\n", "<br>").strip().replace("\n", " ")

src/griffe2md/templates/attribute.md.jinja

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
{%- endif -%}
1010

1111
{%- with obj = attribute, attribute_name = attribute.path if show_full_path else attribute.name -%}
12-
{%- if not root or config.show_root_heading %}
12+
{%- if not root or config.show_root_heading -%}
1313

14-
{% filter heading(heading_level) -%}
14+
{%- filter heading(heading_level) -%}
1515
{%- if config.separate_signature -%}
1616
`{{ attribute_name }}`
17-
{%- else -%}
17+
18+
{% else -%}
1819
`{{- attribute_name }}{% if attribute.annotation -%}: {{ attribute.annotation }}{%- endif -%}`
19-
{%- endif -%}
20+
21+
{% endif -%}
2022
{%- endfilter -%}
2123

2224
{%- if config.separate_signature -%}
@@ -27,11 +29,12 @@
2729
{%- if attribute.value %} = {{ attribute.value|safe }}{%- endif -%}
2830
{%- endfilter %}
2931
```
30-
{%- endif %}
32+
33+
{% endif -%}
3134

3235
{%- else -%}
33-
{%- if config.show_root_toc_entry %}
34-
{% filter heading(heading_level) -%}
36+
{%- if config.show_root_toc_entry -%}
37+
{%- filter heading(heading_level) -%}
3538
{%- endfilter -%}
3639
{%- endif -%}
3740
{%- set heading_level = heading_level - 1 -%}
@@ -40,4 +43,4 @@
4043
{%- with docstring_sections = attribute.docstring.parsed -%}
4144
{%- include "docstring.md.jinja" with context -%}
4245
{%- endwith -%}
43-
{%- endwith -%}
46+
{%- endwith %}

src/griffe2md/templates/children.md.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,5 @@ order_members(config.members_order, members_list) -%}
148148
{%- endif -%}
149149

150150
{%- endfor -%}
151-
152151
{%- endif -%}
153-
154152
{%- endif -%}

src/griffe2md/templates/class.md.jinja

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
{%- endif -%}
1010

1111
{%- with obj = class, class_name = class.path if show_full_path else class.name -%}
12-
{%- if not root or config.show_root_heading %}
12+
{%- if not root or config.show_root_heading -%}
1313

14-
{% filter heading(heading_level) -%}
14+
{%- filter heading(heading_level) -%}
1515
{%- if config.separate_signature -%}
1616
`{{ class_name }}`
17-
{%- elif config.merge_init_into_class and "__init__" in class.members -%}
17+
18+
{% elif config.merge_init_into_class and "__init__" in class.members -%}
1819
{%- with function = class.members["__init__"] -%}
1920
`{{ class_name }}{% include "signature.md.jinja" with context %}`
20-
{%- endwith -%}
21+
22+
{% endwith -%}
2123
{%- else -%}
2224
`{{ class_name }}`
23-
{%- endif -%}
25+
26+
{% endif -%}
2427
{%- endfilter -%}
2528

2629
{%- if config.separate_signature and config.merge_init_into_class -%}
@@ -64,4 +67,4 @@ Bases: {% for expression in class.bases -%}
6467
{%- set root = False -%}
6568
{%- set heading_level = heading_level + 1 -%}
6669
{%- include "children.md.jinja" with context -%}
67-
{%- endwith -%}
70+
{%- endwith %}
Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
{%- if docstring_sections -%}
2-
{% for section in docstring_sections %}
3-
{% if config.show_docstring_description and section.kind.value == "text" -%}
4-
{{ section.value }}
2+
{%- for section in docstring_sections -%}
3+
{%- if config.show_docstring_description and section.kind.value == "text" -%}
4+
{{- section.value }}
5+
56
{% elif config.show_docstring_attributes and section.kind.value == "attributes" -%}
6-
{% include "docstring/attributes.md.jinja" with context -%}
7+
{%- include "docstring/attributes.md.jinja" with context %}
8+
79
{% elif config.show_docstring_parameters and section.kind.value == "parameters" -%}
8-
{% include "docstring/parameters.md.jinja" with context -%}
10+
{%- include "docstring/parameters.md.jinja" with context %}
11+
912
{% elif config.show_docstring_other_parameters and section.kind.value == "other parameters" -%}
10-
{% include "docstring/other_parameters.md.jinja" with context -%}
13+
{%- include "docstring/other_parameters.md.jinja" with context %}
14+
1115
{% elif config.show_docstring_raises and section.kind.value == "raises" -%}
12-
{% include "docstring/raises.md.jinja" with context -%}
16+
{%- include "docstring/raises.md.jinja" with context %}
17+
1318
{% elif config.show_docstring_warns and section.kind.value == "warns" -%}
14-
{% include "docstring/warns.md.jinja" with context -%}
19+
{%- include "docstring/warns.md.jinja" with context %}
20+
1521
{% elif config.show_docstring_yields and section.kind.value == "yields" -%}
16-
{% include "docstring/yields.md.jinja" with context -%}
22+
{%- include "docstring/yields.md.jinja" with context %}
23+
1724
{% elif config.show_docstring_receives and section.kind.value == "receives" -%}
18-
{% include "docstring/receives.md.jinja" with context -%}
25+
{%- include "docstring/receives.md.jinja" with context %}
26+
1927
{% elif config.show_docstring_returns and section.kind.value == "returns" -%}
20-
{% include "docstring/returns.md.jinja" with context -%}
28+
{%- include "docstring/returns.md.jinja" with context %}
29+
2130
{% elif config.show_docstring_examples and section.kind.value == "examples" -%}
22-
{% include "docstring/examples.md.jinja" with context -%}
23-
{% elif config.show_docstring_description and section.kind.value == "admonition" -%}
24-
{% include "docstring/admonition.md.jinja" with context -%}
25-
{% endif %}
26-
{% endfor %}
31+
{%- include "docstring/examples.md.jinja" with context -%}
32+
33+
{%- elif config.show_docstring_description and section.kind.value == "admonition" -%}
34+
{%- include "docstring/admonition.md.jinja" with context %}
35+
36+
{% endif -%}
37+
{%- endfor -%}
2738
{%- endif -%}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{%- if config.docstring_section_style == "table" %}
2-
1+
{%- if config.docstring_section_style == "table" -%}
32
**{{ section.title or "Attributes:" }}**
43

54
Name | Type | Description
@@ -8,10 +7,9 @@ Name | Type | Description
87
[`{{ attribute.name }}`](#{{ obj.path }}.{{ attribute.name }}) | {% if attribute.annotation -%}{%- with expression = attribute.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ attribute.description|newline_to_br }}
98
{%- endfor -%}
109

11-
{%- elif config.docstring_section_style == "list" %}
12-
10+
{%- elif config.docstring_section_style == "list" -%}
1311
**{{ section.title or "Attributes:" }}**
1412
{% for attribute in section.value %}
1513
- [**{{ attribute.name }}**](#{{ obj.path }}.{{ attribute.name }}){%- if attribute.annotation -%}{%- with expression = attribute.annotation %} (<code>{%- include "expression.md.jinja" with context -%}</code>){%- endwith -%}{%- endif %} – {{ attribute.description }}
1614
{%- endfor -%}
17-
{%- endif -%}
15+
{%- endif %}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{%- if config.docstring_section_style == "table" %}
2-
1+
{%- if config.docstring_section_style == "table" -%}
32
**{{ section.title or "Classes:" }}**
43

54
Name | Description
@@ -8,10 +7,9 @@ Name | Description
87
[`{{ class.name }}`](#{{ obj.path }}.{{ class.name }}) | {{ class.description|newline_to_br }}
98
{%- endfor -%}
109

11-
{%- elif config.docstring_section_style == "list" %}
12-
10+
{%- elif config.docstring_section_style == "list" -%}
1311
**{{ section.title or "Classes:" }}**
1412
{% for class in section.value %}
1513
- [**{{ class.name }}**](#{{ obj.path }}.{{ class.name }}) – {{ class.description }}
1614
{%- endfor -%}
17-
{%- endif -%}
15+
{%- endif -%}

src/griffe2md/templates/docstring/examples.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{% for section_type, sub_section in section.value -%}
44
{%- if section_type.value == "text" -%}
55
{{ sub_section }}
6-
{%- elif section_type.value == "examples" %}
76

7+
{% elif section_type.value == "examples" -%}
88
```pycon
99
{{ sub_section }}
1010
```

0 commit comments

Comments
 (0)