diff --git a/docs/source/markup.rst b/docs/source/markup.rst index ca4401e1..997e1a7e 100644 --- a/docs/source/markup.rst +++ b/docs/source/markup.rst @@ -26,7 +26,7 @@ There is a shorthand for closing a style. If you omit the style name from the cl These markup tags may be use in combination with each other and don't need to be strictly nested. The following examples demonstrates overlapping of markup tags:: - print("[bold]Bold[italic] bold and italic [/bold] italic[/italic]") + print("[bold]Bold[italic] bold and italic [/bold]italic[/italic]") Errors ~~~~~~ diff --git a/rich/markup.py b/rich/markup.py index fef744b3..99d56631 100644 --- a/rich/markup.py +++ b/rich/markup.py @@ -1,5 +1,5 @@ import re -from typing import Iterable, List, NamedTuple, Optional, Tuple, Union +from typing import Iterable, List, Match, NamedTuple, Optional, Tuple, Union from .errors import MarkupError from .style import Style @@ -46,7 +46,7 @@ def escape(markup: str, _escape=re.compile(r"(\\*)(\[[a-z#\/].*?\])").sub) -> st str: Markup with square brackets escaped. """ - def escape_backslashes(match: re.Match[str]) -> str: + def escape_backslashes(match: Match[str]) -> str: """Called by re.sub replace matches.""" backslashes, text = match.groups() return f"{backslashes}{backslashes}\\{text}" diff --git a/tests/test_markup.py b/tests/test_markup.py index 0e58e297..07c16425 100644 --- a/tests/test_markup.py +++ b/tests/test_markup.py @@ -24,9 +24,14 @@ def test_re_match(): def test_escape(): + # Potential tags assert escape("foo[bar]") == r"foo\[bar]" assert escape(r"foo\[bar]") == r"foo\\\[bar]" + # Not tags (escape not required) + assert escape("[5]") == "[5]" + assert escape("\\[5]") == "\\[5]" + def test_render_escape(): console = Console(width=80, color_system=None)