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 operator import attrgetter
|
||||
import re
|
||||
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 .style import Style
|
||||
from .text import Span, Text
|
||||
from .emoji import EmojiVariant
|
||||
from ._emoji_replace import _emoji_replace
|
||||
|
||||
|
||||
RE_TAGS = re.compile(
|
||||
r"""((\\*)\[([a-z#\/@].*?)\])""",
|
||||
r"""((\\*)\[([a-z#/@][^[]*?)])""",
|
||||
re.VERBOSE,
|
||||
)
|
||||
|
||||
RE_HANDLER = re.compile(r"^([\w\.]*?)(\(.*?\))?$")
|
||||
RE_HANDLER = re.compile(r"^([\w.]*?)(\(.*?\))?$")
|
||||
|
||||
|
||||
class Tag(NamedTuple):
|
||||
|
@ -146,6 +145,8 @@ def render(
|
|||
|
||||
for position, plain_text, tag in _parse(markup):
|
||||
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)
|
||||
elif tag is not None:
|
||||
if tag.name.startswith("/"): # Closing tag
|
||||
|
@ -233,8 +234,8 @@ if __name__ == "__main__": # pragma: no cover
|
|||
":warning-emoji: [bold red blink] DANGER![/]",
|
||||
]
|
||||
|
||||
from rich.table import Table
|
||||
from rich import print
|
||||
from rich.table import Table
|
||||
|
||||
grid = Table("Markup", "Result", padding=(0, 1))
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import pytest
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
@ -139,6 +139,11 @@ def test_markup_error():
|
|||
assert render("[foo]hello[/bar]")
|
||||
|
||||
|
||||
def test_markup_escape():
|
||||
result = str(render("[dim white]\[url=[/]"))
|
||||
assert result == "[url="
|
||||
|
||||
|
||||
def test_escape_escape():
|
||||
# Escaped escapes (i.e. double backslash)should be treated as literal
|
||||
result = render(r"\\[bold]FOO")
|
||||
|
@ -165,7 +170,6 @@ def test_escape_escape():
|
|||
|
||||
|
||||
def test_events():
|
||||
|
||||
result = render("[@click]Hello[/@click] [@click='view.toggle', 'left']World[/]")
|
||||
assert str(result) == "Hello World"
|
||||
|
||||
|
|
Loading…
Reference in New Issue