mirror of https://github.com/Textualize/rich.git
Fix issue with markup escaping
This commit is contained in:
parent
ae5865eb79
commit
71325bb5bb
|
@ -1,21 +1,20 @@
|
||||||
|
import re
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
import re
|
|
||||||
from typing import Callable, Iterable, List, Match, NamedTuple, Optional, Tuple, Union
|
from typing import Callable, Iterable, List, Match, NamedTuple, Optional, Tuple, Union
|
||||||
|
|
||||||
|
from ._emoji_replace import _emoji_replace
|
||||||
|
from .emoji import EmojiVariant
|
||||||
from .errors import MarkupError
|
from .errors import MarkupError
|
||||||
from .style import Style
|
from .style import Style
|
||||||
from .text import Span, Text
|
from .text import Span, Text
|
||||||
from .emoji import EmojiVariant
|
|
||||||
from ._emoji_replace import _emoji_replace
|
|
||||||
|
|
||||||
|
|
||||||
RE_TAGS = re.compile(
|
RE_TAGS = re.compile(
|
||||||
r"""((\\*)\[([a-z#\/@].*?)\])""",
|
r"""((\\*)\[([a-z#/@][^[]*?)])""",
|
||||||
re.VERBOSE,
|
re.VERBOSE,
|
||||||
)
|
)
|
||||||
|
|
||||||
RE_HANDLER = re.compile(r"^([\w\.]*?)(\(.*?\))?$")
|
RE_HANDLER = re.compile(r"^([\w.]*?)(\(.*?\))?$")
|
||||||
|
|
||||||
|
|
||||||
class Tag(NamedTuple):
|
class Tag(NamedTuple):
|
||||||
|
@ -146,6 +145,8 @@ def render(
|
||||||
|
|
||||||
for position, plain_text, tag in _parse(markup):
|
for position, plain_text, tag in _parse(markup):
|
||||||
if plain_text is not None:
|
if plain_text is not None:
|
||||||
|
# Capture open brace escapes, where the brace is not part of a tag.
|
||||||
|
plain_text = plain_text.replace("\\[", "[")
|
||||||
append(emoji_replace(plain_text) if emoji else plain_text)
|
append(emoji_replace(plain_text) if emoji else plain_text)
|
||||||
elif tag is not None:
|
elif tag is not None:
|
||||||
if tag.name.startswith("/"): # Closing tag
|
if tag.name.startswith("/"): # Closing tag
|
||||||
|
@ -233,8 +234,8 @@ if __name__ == "__main__": # pragma: no cover
|
||||||
":warning-emoji: [bold red blink] DANGER![/]",
|
":warning-emoji: [bold red blink] DANGER![/]",
|
||||||
]
|
]
|
||||||
|
|
||||||
from rich.table import Table
|
|
||||||
from rich import print
|
from rich import print
|
||||||
|
from rich.table import Table
|
||||||
|
|
||||||
grid = Table("Markup", "Result", padding=(0, 1))
|
grid = Table("Markup", "Result", padding=(0, 1))
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.markup import escape, MarkupError, _parse, render, Tag, RE_TAGS
|
from rich.markup import RE_TAGS, MarkupError, Tag, _parse, escape, render
|
||||||
from rich.text import Span
|
from rich.text import Span
|
||||||
|
|
||||||
|
|
||||||
|
@ -139,6 +139,11 @@ def test_markup_error():
|
||||||
assert render("[foo]hello[/bar]")
|
assert render("[foo]hello[/bar]")
|
||||||
|
|
||||||
|
|
||||||
|
def test_markup_escape():
|
||||||
|
result = str(render("[dim white]\[url=[/]"))
|
||||||
|
assert result == "[url="
|
||||||
|
|
||||||
|
|
||||||
def test_escape_escape():
|
def test_escape_escape():
|
||||||
# Escaped escapes (i.e. double backslash)should be treated as literal
|
# Escaped escapes (i.e. double backslash)should be treated as literal
|
||||||
result = render(r"\\[bold]FOO")
|
result = render(r"\\[bold]FOO")
|
||||||
|
@ -165,7 +170,6 @@ def test_escape_escape():
|
||||||
|
|
||||||
|
|
||||||
def test_events():
|
def test_events():
|
||||||
|
|
||||||
result = render("[@click]Hello[/@click] [@click='view.toggle', 'left']World[/]")
|
result = render("[@click]Hello[/@click] [@click='view.toggle', 'left']World[/]")
|
||||||
assert str(result) == "Hello World"
|
assert str(result) == "Hello World"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue