8000 Merge pull request #3130 from Textualize/fix-table-inline-styles · Textualize/rich@bef0e50 · GitHub
[go: up one dir, main page]

Skip to content

Commit bef0e50

Browse files
authored
Merge pull request #3130 from Textualize/fix-table-inline-styles
Fix markdown table rendering issue with inline styles/links
2 parents 720800e + e30b822 commit bef0e50

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Markdown table rendering issue with inline styles and links https://github.com/Textualize/rich/issues/3115
13+
814
## [13.5.2] - 2023-08-01
915

1016
### Fixed

rich/markdown.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class TableDataElement(MarkdownElement):
314314

315315
@classmethod
316316
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
317-
style = str(token.attrs.get("style" "")) or ""
317+
style = str(token.attrs.get("style")) or ""
318318

319319
justify: JustifyMethod
320320
if "text-align:right" in style:
@@ -330,15 +330,13 @@ def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
330330
return cls(justify=justify)
331331

332332
def __init__(self, justify: JustifyMethod) -> None:
333-
self.content: TextType = ""
333+
self.content: Text = Text("", justify=justify)
334334
self.justify = justify
335335

336336
def on_text(self, context: "MarkdownContext", text: TextType) -> None:
337-
plain = text.plain if isinstance(text, Text) else text
338-
style = text.style if isinstance(text, Text) else ""
339-
self.content = Text(
340-
plain, justify=self.justify, style=context.style_stack.current
341-
)
337+
text = Text(text) if isinstance(text, str) else text
338+
text.stylize(context.current_style)
339+
self.content.append_text(text)
342340

343341

344342
class ListElement(MarkdownElement):

tests/test_markdown.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,48 @@ def test_markdown_table():
121121
"""\
122122
| Year | Title | Director | Box Office (USD) |
123123
|------|:------------------------------------------------:|:------------------|------------------:|
124-
| 1982 | *E.T. the Extra-Terrestrial* | Steven Spielberg | $792.9 million |
124+
| 1982 | *E.T. the Extra-Terrestrial* | Steven Spielberg | $792.9 million |
125125
| 1980 | Star Wars: Episode V – The Empire Strikes Back | Irvin Kershner | $538.4 million |
126126
| 1983 | Star Wars: Episode VI – Return of the Jedi | Richard Marquand | $475.1 million |
127127
| 1981 | Raiders of the Lost Ark | Steven Spielberg | $389.9 million |
128128
| 1984 | Indiana Jones and the Temple of Doom | Steven Spielberg | $333.1 million |
129129
"""
130130
)
131131
result = render(markdown)
132-
expected = "\n \n \x1b[1m \x1b[0m\x1b[1mYear\x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1m Title \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mDirector \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mBox Office (USD)\x1b[0m\x1b[1m \x1b[0m \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n 1982 \x1b[3m E.T. the Extra-Terrestrial \x1b[0m Steven Spielberg $792.9 million \n 1980 Star Wars: Episode V – The Empire Strikes Back Irvin Kershner $538.4 million \n 1983 Star Wars: Episode VI – Return of the Jedi Richard Marquand $475.1 million \n 1981 Raiders of the Lost Ark Steven Spielberg $389.9 million \n 1984 Indiana Jones and the Temple of Doom Steven Spielberg $333.1 million \n \n"
132+
expected = "\n \n \x1b[1m \x1b[0m\x1b[1mYear\x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1m Title \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mDirector \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mBox Office (USD)\x1b[0m\x1b[1m \x1b[0m \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n 1982 \x1b[3mE.T. the Extra-Terrestrial\x1b[0m Steven Spielberg $792.9 million \n 1980 Star Wars: Episode V – The Empire Strikes Back Irvin Kershner $538.4 million \n 1983 Star Wars: Episode VI – Return of the Jedi Richard Marquand $475.1 million \n 1981 Raiders of the Lost Ark Steven Spielberg $389.9 million \n 1984 Indiana Jones and the Temple of Doom Steven Spielberg $333.1 million \n \n"
133+
assert result == expected
134+
135+
136+
def test_inline_styles_in_table():
137+
"""Regression test for https://github.com/Textualize/rich/issues/3115"""
138+
markdown = Markdown(
139+
"""\
140+
| Year | This **column** displays _the_ movie _title_ ~~description~~ | Director | Box Office (USD) |
141+
|------|:----------------------------------------------------------:|:------------------|------------------:|
142+
| 1982 | *E.T. the Extra-Terrestrial* ([Wikipedia article](https://en.wikipedia.org/wiki/E.T._the_Extra-Terrestrial)) | Steven Spielberg | $792.9 million |
143+
| 1980 | Star Wars: Episode V – The *Empire* **Strikes** ~~Back~~ | Irvin Kershner | $538.4 million |
144+
"""
145+
)
146+
result = render(markdown)
147+
expected = "\n \n \x1b[1m \x1b[0m\x1b[1mYear\x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mThis \x1b[0m\x1b[1mcolumn\x1b[0m\x1b[1m displays \x1b[0m\x1b[1;3mthe\x1b[0m\x1b[1m movie \x1b[0m\x1b[1;3mtitle\x1b[0m\x1b[1m \x1b[0m\x1b[1;9mdescription\x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mDirector \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1mBox Office (USD)\x1b[0m\x1b[1m \x1b[0m \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n 1982 \x1b[3mE.T. the Extra-Terrestrial\x1b[0m (\x1b]8;id=0;foo\x1b\\\x1b[4;34mWikipedia article\x1b[0m\x1b]8;;\x1b\\) Steven Spielberg $792.9 million \n 1980 Star Wars: Episode V – The \x1b[3mEmpire\x1b[0m \x1b[1mStrikes\x1b[0m \x1b[9mBack\x1b[0m Irvin Kershner $538.4 million \n \n"
148+
assert result == expected
149+
150+
151+
def test_inline_styles_with_justification():
152+
"""Regression test for https://github.com/Textualize/rich/issues/3115
153+
154+
In particular, this tests the interaction between the change that was made to fix
155+
#3115 and column text justification.
156+
"""
157+
markdown = Markdown(
158+
"""\
159+
| left | center | right |
160+
| :- | :-: | -: |
161+
| This is a long row | because it contains | a fairly long sentence. |
162+
| a*b* _c_ ~~d~~ e | a*b* _c_ ~~d~~ e | a*b* _c_ ~~d~~ e |"""
163+
)
164+
result = render(markdown)
165+
expected = "\n \n \x1b[1m \x1b[0m\x1b[1mleft \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1m center \x1b[0m\x1b[1m \x1b[0m \x1b[1m \x1b[0m\x1b[1m right\x1b[0m\x1b[1m \x1b[0m \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n This is a long row because it contains a fairly long sentence. \n a\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e a\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e a\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e \n \n"
133166
assert result == expected
134167

135168

0 commit comments

Comments
 (0)
0