Skip to content

Commit

Permalink
Remove trailing empty Text() objects that get displayed as newlines.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Apr 26, 2024
1 parent ec1130b commit fc4d73c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rich_rst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,16 @@ def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderR
)
document.walkabout(visitor)

if visitor.renderables and isinstance(visitor.renderables[-1], Text):
# Strip all trailing newlines and newline-like rich objects
while visitor.renderables and isinstance(visitor.renderables[-1], Text):
visitor.renderables[-1].rstrip()
visitor.renderables[-1].end = "\n"
if visitor.renderables[-1]:
break
else:
# Remove empty ``Text()`` objects.
visitor.renderables.pop()

for renderable in visitor.renderables:
yield from console.render(renderable, options)
if self.log_errors and visitor.errors:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_vectors/bullet_list.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* foo
* bar
18 changes: 18 additions & 0 deletions tests/test_vectors/bullet_list_expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.r1 {color: #f1fa8c; text-decoration-color: #f1fa8c; font-weight: bold}
body {
color: #f8f8f2;
background-color: #282a36;
}
</style>
</head>
<body>
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code style="font-family:inherit"><span class="r1"></span>foo
<span class="r1"></span>bar
</code></pre>
</body>
</html>

0 comments on commit fc4d73c

Please sign in to comment.