Skip to content

Commit

Permalink
Fix RST rendering for HORIZONTALLINE (#370)
Browse files Browse the repository at this point in the history
* Add HORIZONTALLINE test with current output.

* Fix '.. raw::' indent.

* Lint.
  • Loading branch information
felixfontein authored Jan 16, 2025
1 parent 55ba46f commit 84d56f8
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 15 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/370-fixes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- "Fix rendering of ``HORIZONTALLINE`` in reStructuredText output. An earlier fix for leading whitespace mangled the resulting ``raw`` directive
(https://github.com/ansible-community/antsibull-docs/pull/370)."
13 changes: 12 additions & 1 deletion src/antsibull_docs/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ def column_width(string: str) -> int:
return _column_width(string)


def _lstrip(line: str, lines: list[str], no: int) -> str:
if (
no >= 2
and lines[no - 2].startswith(".. raw:: ")
and lines[no - 1] == ""
and line.startswith(" ")
):
return line
return line.lstrip()


def rst_indent(
value: str, width: t.Union[int, str], first: bool = False, blank: bool = False
) -> str:
Expand All @@ -277,7 +288,7 @@ def rst_indent(
lines = (value + "\n").splitlines()

# Remove trailing whitespace
stripped_lines = [line.lstrip() for line in lines]
stripped_lines = [_lstrip(line, lines, no) for no, line in enumerate(lines)]

if blank:
rv = ("\n" + indent).join(stripped_lines)
Expand Down
7 changes: 5 additions & 2 deletions tests/functional/ansible-doc-cache-all-others.json
Original file line number Diff line number Diff line change
Expand Up @@ -23931,7 +23931,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -23940,7 +23942,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
7 changes: 5 additions & 2 deletions tests/functional/ansible-doc-cache-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -23848,7 +23848,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -23857,7 +23859,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23848,7 +23848,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -23857,7 +23859,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23007,7 +23007,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -23016,7 +23018,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -2146,7 +2148,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -1305,7 +1307,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
7 changes: 5 additions & 2 deletions tests/functional/ansible-doc-cache-ns2.col.json
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,9 @@
"description": [
"Foo bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz)."
"Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).",
"HORIZONTALLINE",
"Another line."
],
"filename": "ansible_collections/ns2/col/plugins/modules/foo2.py",
"has_action": false,
Expand All @@ -1305,7 +1307,8 @@
"bar": {
"description": [
"Some bar.",
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value."
"See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.",
"Some text. HORIZONTALLINE More text."
],
"type": "str"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Synopsis
- Foo bar.
- See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.
- Reference using alias - :ansopt:`ns2.col.foo\_redirect#module:bar` and :ansopt:`ns2.col.foo\_redirect#module:baz`.
- .. raw:: html

<hr>
- Another line.


.. Aliases
Expand Down Expand Up @@ -110,6 +114,14 @@ Parameters

See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.

Some text.

.. raw:: html

<hr>

More text.


.. raw:: html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Synopsis
- Foo bar.
- See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.
- Reference using alias - :ansopt:`ns2.col.foo\_redirect#module:bar` and :ansopt:`ns2.col.foo\_redirect#module:baz`.
- .. raw:: html

<hr>
- Another line.


.. Aliases
Expand Down Expand Up @@ -110,6 +114,14 @@ Parameters

See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.

Some text.

.. raw:: html

<hr>

More text.


.. raw:: html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Synopsis
- Foo bar.
- See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.
- Reference using alias - :ansopt:`ns2.col.foo\_redirect#module:bar` and :ansopt:`ns2.col.foo\_redirect#module:baz`.
- .. raw:: html

<hr>
- Another line.


.. Aliases
Expand Down Expand Up @@ -107,6 +111,14 @@ Parameters

See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.

Some text.

.. raw:: html

<hr>

More text.


.. raw:: html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Synopsis
- Foo bar.
- See :literal:`foo\_param\_1` (of role `ns2.col.foo <foo_role.rst>`__, entrypoint main) for a random role parameter reference. And :literal:`foo\_param\_2=42` (of role `ns2.col.foo <foo_role.rst>`__, entrypoint main) for one with a value.
- Reference using alias - :literal:`bar` (of module `ns2.col.foo\_redirect <foo_redirect_module.rst>`__) and :literal:`baz` (of module `ns2.col.foo\_redirect <foo_redirect_module.rst>`__).
- ------------
- Another line.


Aliases: foo_3_redirect, foo_4_redirect, foo_5_redirect
Expand Down Expand Up @@ -58,6 +60,7 @@ Parameters
<td valign="top">
<p>Some bar.</p>
<p>See <code class="ansible-option literal notranslate"><strong><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_1"><span class="std std-ref"><span class="pre">foo_param_1</span></span></a></strong></code> for a random role parameter reference. And <code class="ansible-option-value literal notranslate"><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_2"><span class="std std-ref"><span class="pre">foo_param_2=42</span></span></a></code> for one with a value.</p>
<p>Some text.<hr/>More text.</p>
</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Synopsis
- Foo bar.
- See :literal:`foo\_param\_1` (of role `ns2.col.foo <foo_role.rst>`__, entrypoint main) for a random role parameter reference. And :literal:`foo\_param\_2=42` (of role `ns2.col.foo <foo_role.rst>`__, entrypoint main) for one with a value.
- Reference using alias - :literal:`bar` (of module `ns2.col.foo\_redirect <foo_redirect_module.rst>`__) and :literal:`baz` (of module `ns2.col.foo\_redirect <foo_redirect_module.rst>`__).
- ------------
- Another line.


Aliases: foo_3_redirect, foo_4_redirect, foo_5_redirect
Expand Down Expand Up @@ -58,6 +60,7 @@ Parameters
<td valign="top">
<p>Some bar.</p>
<p>See <code class="ansible-option literal notranslate"><strong><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_1"><span class="std std-ref"><span class="pre">foo_param_1</span></span></a></strong></code> for a random role parameter reference. And <code class="ansible-option-value literal notranslate"><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_2"><span class="std std-ref"><span class="pre">foo_param_2=42</span></span></a></code> for one with a value.</p>
<p>Some text.<hr/>More text.</p>
</td>
</tr>
</tbody>
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/baseline-squash-hierarchy/foo2_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Synopsis
- Foo bar.
- See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.
- Reference using alias - :ansopt:`ns2.col.foo\_redirect#module:bar` and :ansopt:`ns2.col.foo\_redirect#module:baz`.
- .. raw:: html

<hr>
- Another line.


.. Aliases
Expand Down Expand Up @@ -110,6 +114,14 @@ Parameters

See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.

Some text.

.. raw:: html

<hr>

More text.


.. raw:: html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Synopsis
- Foo bar.
- See :ansopt:`ns2.col.foo#role:main:foo\_param\_1` for a random role parameter reference. And :ansopt:`ns2.col.foo#role:main:foo\_param\_2=42` for one with a value.
- Reference using alias - :ansopt:`ns2.col.foo\_redirect#module:bar` and :ansopt:`ns2.col.foo\_redirect#module:baz`.
- .. raw:: html

<hr>
- Another line.


.. Aliases
Expand Down Expand Up @@ -90,6 +94,7 @@ Parameters
<td><div class="ansible-option-cell">
<p>Some bar.</p>
<p>See <code class="ansible-option literal notranslate"><strong><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_1"><span class="std std-ref"><span class="pre">foo_param_1</span></span></a></strong></code> for a random role parameter reference. And <code class="ansible-option-value literal notranslate"><a class="reference internal" href="../../ns2/col/foo_role.html#parameter-main--foo_param_2"><span class="std std-ref"><span class="pre">foo_param_2=42</span></span></a></code> for one with a value.</p>
<p>Some text.<hr/>More text.</p>
</div></td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
- See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And
O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.
- Reference using alias - O(ns2.col.foo_redirect#module:bar) and O(ns2.col.foo_redirect#module:baz).
- HORIZONTALLINE
- Another line.
options:
bar:
description:
- Some bar.
- See O(ns2.col.foo#role:main:foo_param_1) for a random role parameter reference. And
O(ns2.col.foo#role:main:foo_param_2=42) for one with a value.
- Some text. HORIZONTALLINE More text.
type: str

attributes:
Expand Down

0 comments on commit 84d56f8

Please sign in to comment.