Skip to content

Commit

Permalink
escape last slash
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 29, 2023
1 parent 368e8ad commit 2dc8398
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rich/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def escape_backslashes(match: Match[str]) -> str:
return f"{backslashes}{backslashes}\\{text}"

markup = _escape(escape_backslashes, markup)
if markup.endswith("\\"):
return markup + "\\"
return markup


Expand Down Expand Up @@ -226,7 +228,6 @@ def pop_style(style_name: str) -> Tuple[int, Tag]:


if __name__ == "__main__": # pragma: no cover

MARKUP = [
"[red]Hello World[/red]",
"[magenta]Hello [b]World[/b]",
Expand Down
14 changes: 13 additions & 1 deletion tests/test_markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rich.console import Console
from rich.errors import MarkupError
from rich.markup import RE_TAGS, Tag, _parse, escape, render
from rich.text import Span
from rich.text import Span, Text


def test_re_no_match():
Expand Down Expand Up @@ -44,6 +44,18 @@ def test_escape():
assert escape("[nil, [nil]]") == r"[nil, \[nil]]"


def test_escape_backslash_end():
# https://github.com/Textualize/rich/issues/2987
value = "C:\\"
assert escape(value) == "C:\\\\"

escaped_tags = f"[red]{escape(value)}[/red]"
assert escaped_tags == "[red]C:\\\\[/red]"
escaped_text = Text.from_markup(escaped_tags)
assert escaped_text.plain == "C:\\"
assert escaped_text.spans == [Span(0, 3, "red")]


def test_render_escape():
console = Console(width=80, color_system=None)
console.begin_capture()
Expand Down

0 comments on commit 2dc8398

Please sign in to comment.