Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markup escaping fixes #1950

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- In Jupyter mode make the link target be set to "_blank"
- Fix some issues with markup handling around "[" characters https://github.com/Textualize/rich/pull/1950

## [11.2.0] - 2022-02-08

Expand Down
15 changes: 8 additions & 7 deletions rich/markup.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -146,6 +145,8 @@ def pop_style(style_name: str) -> Tuple[int, Tag]:

for position, plain_text, tag in _parse(markup):
if plain_text is not None:
# Handle 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
Expand Down Expand Up @@ -233,8 +234,8 @@ def pop_style(style_name: str) -> Tuple[int, Tag]:
":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))

Expand Down
8 changes: 6 additions & 2 deletions tests/test_markup.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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")
Expand All @@ -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"

Expand Down