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

Swap open-vs-close tag order when converting Text to markup. #3163

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure font is correctly inherited in exported HTML https://github.com/Textualize/rich/issues/3104
- Fixed typing for `FloatPrompt`.

### Changed

- Swap open-vs-close tag order when converting Text to markup.

## [13.6.0] - 2023-09-30

### Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ The following people have contributed to the development of Rich:
- [Qiming Xu](https://github.com/xqm32)
- [James Addison](https://github.com/jayaddison)
- [Pierro](https://github.com/xpierroz)
- [Christopher Pinard](https://github.com/slaarti)
- [Bernhard Wagner](https://github.com/bwagner)
- [Aaron Beaudoin](https://github.com/AaronBeaudoin)
12 changes: 6 additions & 6 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,20 @@ def markup(self) -> str:

plain = self.plain
markup_spans = [
(0, False, self.style),
*((span.start, False, span.style) for span in self._spans),
*((span.end, True, span.style) for span in self._spans),
(len(plain), True, self.style),
(0, True, self.style),
*((span.start, True, span.style) for span in self._spans),
*((span.end, False, span.style) for span in self._spans),
(len(plain), False, self.style),
]
markup_spans.sort(key=itemgetter(0, 1))
position = 0
append = output.append
for offset, closing, style in markup_spans:
for offset, opening, style in markup_spans:
if offset > position:
append(escape(plain[position:offset]))
position = offset
if style:
append(f"[/{style}]" if closing else f"[{style}]")
append(f"[{style}]" if opening else f"[/{style}]")
markup = "".join(output)
return markup

Expand Down
6 changes: 6 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def test_from_markup():
assert text._spans == [Span(7, 13, "bold")]


def test_markup_round_trip():
src = "[red]foo[/red][blue]bar[/blue][yellow]baz[/yellow]"
text = Text.from_markup(src)
assert text.markup == src


def test_from_ansi():
text = Text.from_ansi("Hello, \033[1mWorld!\033[0m")
assert str(text) == "Hello, World!"
Expand Down