Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of empty markup parameters for RST #51

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/51-rst.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "Fix handling of empty markup parameters for RST (https://github.com/ansible-community/antsibull-docs-parser/pull/51)."
115 changes: 91 additions & 24 deletions src/antsibull_docs_parser/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from .html import _url_escape


def rst_escape(value: str, escape_ending_whitespace=False) -> str:
def rst_escape(
value: str,
escape_ending_whitespace: bool = False,
*,
must_not_be_empty: bool = False,
) -> str:
"""Escape RST specific constructs."""
value = value.replace("\\", "\\\\")
value = value.replace("<", "\\<")
Expand All @@ -28,6 +33,8 @@ def rst_escape(value: str, escape_ending_whitespace=False) -> str:
value = value + "\\ "
if escape_ending_whitespace and value.startswith(" "):
value = "\\ " + value
if not value and must_not_be_empty:
value = "\\ "

return value

Expand All @@ -53,33 +60,55 @@ def _format_option_like(
if value is not None:
result.append("=")
result.append(value)
return f'\\ :{role}:`{rst_escape("".join(result), True)}`\\ '
text = rst_escape(
"".join(result), escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :{role}:`{text}`\\ "

def format_error(self, part: dom.ErrorPart) -> str:
return (
f"\\ :strong:`ERROR while parsing`\\ : {rst_escape(part.message, True)}\\ "
text = rst_escape(
part.message, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :strong:`ERROR while parsing`\\ : {text}\\ "

def format_bold(self, part: dom.BoldPart) -> str:
return f"\\ :strong:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :strong:`{text}`\\ "

def format_code(self, part: dom.CodePart) -> str:
return f"\\ :literal:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :literal:`{text}`\\ "

def format_horizontal_line(self, part: dom.HorizontalLinePart) -> str:
return "\n\n.. raw:: html\n\n <hr>\n\n"

def format_italic(self, part: dom.ItalicPart) -> str:
return f"\\ :emphasis:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :emphasis:`{text}`\\ "

def format_link(self, part: dom.LinkPart) -> str:
return f"\\ `{rst_escape(part.text)} <{_url_escape(part.url)}>`__\\ "
if not part.text:
return ""
text = rst_escape(part.text, escape_ending_whitespace=True)
return f"\\ `{text} <{_url_escape(part.url)}>`__\\ "

def format_module(self, part: dom.ModulePart, url: t.Optional[str]) -> str:
return f"\\ :ref:`{rst_escape(part.fqcn)} <ansible_collections.{part.fqcn}_module>`\\ "
text = rst_escape(
part.fqcn, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :ref:`{text} <ansible_collections.{part.fqcn}_module>`\\ "

def format_rst_ref(self, part: dom.RSTRefPart) -> str:
return f"\\ :ref:`{rst_escape(part.text)} <{part.ref}>`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :ref:`{text} <{part.ref}>`\\ "

def format_url(self, part: dom.URLPart) -> str:
return f"\\ {_url_escape(part.url)}\\ "
Expand All @@ -88,13 +117,19 @@ def format_text(self, part: dom.TextPart) -> str:
return rst_escape(part.text)

def format_env_variable(self, part: dom.EnvVariablePart) -> str:
return f"\\ :envvar:`{rst_escape(part.name, True)}`\\ "
text = rst_escape(
part.name, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :envvar:`{text}`\\ "

def format_option_name(self, part: dom.OptionNamePart, url: t.Optional[str]) -> str:
return self._format_option_like(part, "ansopt")

def format_option_value(self, part: dom.OptionValuePart) -> str:
return f"\\ :ansval:`{rst_escape(part.value, True)}`\\ "
text = rst_escape(
part.value, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :ansval:`{text}`\\ "

def format_plugin(self, part: dom.PluginPart, url: t.Optional[str]) -> str:
return (
Expand Down Expand Up @@ -128,39 +163,65 @@ def _format_option_like(
if plugin_result:
plugin_result.append(", ")
plugin_result.append("entrypoint ")
plugin_result.append(rst_escape(entrypoint, True))
plugin_result.append(
rst_escape(
entrypoint, escape_ending_whitespace=True, must_not_be_empty=True
)
)
plugin_text = f" (of {''.join(plugin_result)})" if plugin_result else ""
value_text = part.name
value = part.value
if value is not None:
value_text = f"{value_text}={value}"
return f"\\ :literal:`{rst_escape(value_text, True)}`{plugin_text}\\ "
escaped_text = rst_escape(
value_text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :literal:`{escaped_text}`{plugin_text}\\ "

def format_error(self, part: dom.ErrorPart) -> str:
return (
f"\\ :strong:`ERROR while parsing`\\ : {rst_escape(part.message, True)}\\ "
text = rst_escape(
part.message, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :strong:`ERROR while parsing`\\ : {text}\\ "

def format_bold(self, part: dom.BoldPart) -> str:
return f"\\ :strong:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :strong:`{text}`\\ "

def format_code(self, part: dom.CodePart) -> str:
return f"\\ :literal:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :literal:`{text}`\\ "

def format_horizontal_line(self, part: dom.HorizontalLinePart) -> str:
return "\n\n------------\n\n"

def format_italic(self, part: dom.ItalicPart) -> str:
return f"\\ :emphasis:`{rst_escape(part.text, True)}`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :emphasis:`{text}`\\ "

def format_link(self, part: dom.LinkPart) -> str:
return f"\\ `{rst_escape(part.text)} <{_url_escape(part.url)}>`__\\ "
if not part.text:
return ""
text = rst_escape(part.text, escape_ending_whitespace=True)
return f"\\ `{text} <{_url_escape(part.url)}>`__\\ "

def format_module(self, part: dom.ModulePart, url: t.Optional[str]) -> str:
return f"\\ :ref:`{rst_escape(part.fqcn)} <ansible_collections.{part.fqcn}_module>`\\ "
text = rst_escape(
part.fqcn, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :ref:`{text} <ansible_collections.{part.fqcn}_module>`\\ "

def format_rst_ref(self, part: dom.RSTRefPart) -> str:
return f"\\ :ref:`{rst_escape(part.text)} <{part.ref}>`\\ "
text = rst_escape(
part.text, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :ref:`{text} <{part.ref}>`\\ "

def format_url(self, part: dom.URLPart) -> str:
return f"\\ {_url_escape(part.url)}\\ "
Expand All @@ -169,13 +230,19 @@ def format_text(self, part: dom.TextPart) -> str:
return rst_escape(part.text)

def format_env_variable(self, part: dom.EnvVariablePart) -> str:
return f"\\ :envvar:`{rst_escape(part.name, True)}`\\ "
text = rst_escape(
part.name, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :envvar:`{text}`\\ "

def format_option_name(self, part: dom.OptionNamePart, url: t.Optional[str]) -> str:
return self._format_option_like(part)

def format_option_value(self, part: dom.OptionValuePart) -> str:
return f"\\ :literal:`{rst_escape(part.value, True)}`\\ "
text = rst_escape(
part.value, escape_ending_whitespace=True, must_not_be_empty=True
)
return f"\\ :literal:`{text}`\\ "

def format_plugin(self, part: dom.PluginPart, url: t.Optional[str]) -> str:
return (
Expand Down
21 changes: 21 additions & 0 deletions test-vectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ test_vectors:
ansible_doc_text: This is a `test' `module' *markup*.
rst_plain: This is a \ :literal:`test`\ \ :emphasis:`module`\ \ :strong:`markup`\
.
empty_tags:
source: |-
C() I() B() C() U() L(,) R(,) V() O() RV() E()
html: <p><code class='docutils literal notranslate'></code> <em></em> <b></b>
<code class='docutils literal notranslate'></code> <a href=''></a> <a href=''></a>
<span class='module'></span> <code class="ansible-value literal notranslate"></code>
<code class="ansible-option literal notranslate"><strong></strong></code> <code
class="ansible-return-value literal notranslate"></code> <code class="xref std
std-envvar literal notranslate"></code></p>
html_plain: <p><code></code> <em></em> <b></b> <code></code> <a href=''></a> <a
href=''></a> <span></span> <code></code> <code><strong></strong></code> <code></code>
<code></code></p>
md: <code></code> <em></em> <b></b> <code></code> []() []() <code></code> <code><strong></strong></code>
<code></code> <code></code>
rst: '\ :literal:`\ `\ \ :emphasis:`\ `\ \ :strong:`\ `\ \ :literal:`\ `\ \
\ \ :ref:`\ <>`\ \ :ansval:`\ `\ \ :ansopt:`\ `\ \ :ansretval:`\ `\ \
:envvar:`\ `\ '
rst_plain: '\ :literal:`\ `\ \ :emphasis:`\ `\ \ :strong:`\ `\ \ :literal:`\
`\ \ \ \ :ref:`\ <>`\ \ :literal:`\ `\ \ :literal:`\ `\ \ :literal:`\
`\ \ :envvar:`\ `\ '
ansible_doc_text: "`' `' ** `' <> `' `' `' `'"
module:
source: |-
The M(a.b.c) module.
Expand Down