Skip to content

Commit

Permalink
Improve CLI rendering of plugin docs (#3301)
Browse files Browse the repository at this point in the history
* some paragraphs were glued together, that has been fixed by enforcing
  new line between top-level paragraphs,
* plugin's one-line "summary" and "description" were separated by one
  extra empty line, which is now gone.
  • Loading branch information
happz authored Oct 17, 2024
1 parent 66a832a commit b87eff8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tmt/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ def __init__(
lines = self.doc.splitlines()

self.summary: str = lines[0].strip()
self.description: str = '\n'.join(lines[1:])
self.description: str = '\n'.join(lines[1:]).strip()

def __repr__(self) -> str:
return f'<{self.name} from {self.class_.__module__}>'
Expand Down
22 changes: 17 additions & 5 deletions tmt/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
from tmt.log import Logger
from tmt.utils import GeneralError

#: Special string representing a new-line in the stack of rendered
#: paragraphs.
NL = ''


#
# ReST rendering
#
class RestVisitor(docutils.nodes.NodeVisitor):
"""
Custom renderer of docutils nodes.
Expand Down Expand Up @@ -63,6 +64,10 @@ def __init__(self, document: docutils.nodes.document, logger: Logger) -> None:
def rendered(self) -> str:
""" Return the rendered document as a single string """

# Drop any trailing empty lines
while self._rendered_paragraphs and self._rendered_paragraphs[-1] == NL:
self._rendered_paragraphs.pop(-1)

return '\n'.join(self._rendered_paragraphs)

def _emit(self, s: str) -> None:
Expand Down Expand Up @@ -91,8 +96,8 @@ def nl(self) -> None:
# To simplify the implementation, this is merging of multiple
# empty lines into one. Rendering of nodes than does not have
# to worry about an empty line already being on the stack.
if self._rendered_paragraphs and self._rendered_paragraphs[-1] != '':
self._emit_paragraphs([''])
if self._rendered_paragraphs and self._rendered_paragraphs[-1] != NL:
self._emit_paragraphs([NL])

# Simple logging for nodes that have no effect
def _noop_visit(self, node: docutils.nodes.Node) -> None:
Expand Down Expand Up @@ -131,6 +136,13 @@ def visit_paragraph(self, node: docutils.nodes.paragraph) -> None:
def depart_paragraph(self, node: docutils.nodes.paragraph) -> None:
self.log_departure(str(node))

# Top-level paragraphs should be followed by an empty line to
# prevent paragraphs sticking together. Only the top-level ones
# though, we do not want empty lines after every paragraph-like
# string, because a lot of nodes are also paragraphs.
if isinstance(node.parent, docutils.nodes.document):
self.nl()

self.flush()

def visit_Text(self, node: docutils.nodes.Text) -> None: # noqa: N802
Expand Down

0 comments on commit b87eff8

Please sign in to comment.