| 1 |
# coding=utf-8 |
| 2 |
|
| 3 |
MARKDOWN = """Heading |
| 4 |
======= |
| 5 |
|
| 6 |
Sub-heading |
| 7 |
----------- |
| 8 |
|
| 9 |
### Heading |
| 10 |
|
| 11 |
#### H4 Heading |
| 12 |
|
| 13 |
##### H5 Heading |
| 14 |
|
| 15 |
###### H6 Heading |
| 16 |
|
| 17 |
|
| 18 |
Paragraphs are separated |
| 19 |
by a blank line. |
| 20 |
|
| 21 |
Two spaces at the end of a line |
| 22 |
produces a line break. |
| 23 |
|
| 24 |
Text attributes _italic_, |
| 25 |
**bold**, `monospace`. |
| 26 |
|
| 27 |
Horizontal rule: |
| 28 |
|
| 29 |
--- |
| 30 |
|
| 31 |
Bullet list: |
| 32 |
|
| 33 |
* apples |
| 34 |
* oranges |
| 35 |
* pears |
| 36 |
|
| 37 |
Numbered list: |
| 38 |
|
| 39 |
1. lather |
| 40 |
2. rinse |
| 41 |
3. repeat |
| 42 |
|
| 43 |
An [example](http://example.com). |
| 44 |
|
| 45 |
> Markdown uses email-style > characters for blockquoting. |
| 46 |
> |
| 47 |
> Lorem ipsum |
| 48 |
|
| 49 |
 |
| 50 |
|
| 51 |
|
| 52 |
``` |
| 53 |
a=1 |
| 54 |
``` |
| 55 |
|
| 56 |
```python |
| 57 |
import this |
| 58 |
``` |
| 59 |
|
| 60 |
```somelang |
| 61 |
foobar |
| 62 |
``` |
| 63 |
|
| 64 |
import this |
| 65 |
|
| 66 |
|
| 67 |
1. List item |
| 68 |
|
| 69 |
Code block |
| 70 |
""" |
| 71 |
|
| 72 |
import io |
| 73 |
import re |
| 74 |
|
| 75 |
from rich.console import Console, RenderableType |
| 76 |
from rich.markdown import Markdown |
| 77 |
|
| 78 |
re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b") |
| 79 |
|
| 80 |
|
| 81 |
def replace_link_ids(render: str) -> str: |
| 82 |
"""Link IDs have a random ID and system path which is a problem for |
| 83 |
reproducible tests. |
| 84 |
|
| 85 |
""" |
| 86 |
return re_link_ids.sub("id=0;foo\x1b", render) |
| 87 |
|
| 88 |
|
| 89 |
def render(renderable: RenderableType) -> str: |
| 90 |
console = Console( |
| 91 |
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False |
| 92 |
) |
| 93 |
console.print(renderable) |
| 94 |
output = replace_link_ids(console.file.getvalue()) |
| 95 |
print(repr(output)) |
| 96 |
return output |
| 97 |
|
| 98 |
|
| 99 |
def test_markdown_render(): |
| 100 |
markdown = Markdown(MARKDOWN) |
| 101 |
rendered_markdown = render(markdown) |
| 102 |
expected = " \x1b[1;4mHeading\x1b[0m \n\n\x1b[4;35mSub-heading\x1b[0m \n\n\x1b[1;35mHeading\x1b[0m \n\n\x1b[3;35mH4 Heading\x1b[0m \n\n\x1b[3mH5 Heading\x1b[0m \n\n\x1b[2mH6 Heading\x1b[0m \n\nParagraphs are separated by a blank line. \n\nTwo spaces at the end of a line produces a line break. \n\nText attributes \x1b[3mitalic\x1b[0m, \x1b[1mbold\x1b[0m, \x1b[1;36;40mmonospace\x1b[0m. \n\nHorizontal rule: \n\n\x1b[2m----------------------------------------------------------------------------------------------------\x1b[0m\n\nBullet list: \n\n\x1b[1m • \x1b[0mapples \n\x1b[1m • \x1b[0moranges \n\x1b[1m • \x1b[0mpears \n\nNumbered list: \n\n\x1b[36m 1 \x1b[0mlather \n\x1b[36m 2 \x1b[0mrinse \n\x1b[36m 3 \x1b[0mrepeat \n\nAn \x1b]8;id=0;foo\x1b\\\x1b[4;34mexample\x1b[0m\x1b]8;;\x1b\\. \n\n\x1b[35m▌ \x1b[0m\x1b[35mMarkdown uses email-style > characters for blockquoting.\x1b[0m\x1b[35m \x1b[0m\n\x1b[35m▌ \x1b[0m\x1b[35mLorem ipsum\x1b[0m\x1b[35m \x1b[0m\n\n🌆 \x1b]8;id=0;foo\x1b\\progress\x1b]8;;\x1b\\ \n\n\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34ma=1\x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\n\n\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\x1b[38;2;255;70;137;48;2;39;40;34mimport\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mthis\x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\n\n\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mfoobar\x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\n\n\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mimport this\x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[48;2;39;40;34m \x1b[0m\n\n\x1b[36m 1 \x1b[0mList item \n\x1b[36m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[36m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mCode block\x1b[0m\x1b[48;2;39;40;34m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n\x1b[36m \x1b[0m\x1b[48;2;39;40;34m \x1b[0m\n" |
| 103 |
assert rendered_markdown == expected |
| 104 |
|
| 105 |
|
| 106 |
def test_inline_code(): |
| 107 |
markdown = Markdown( |
| 108 |
"inline `import this` code", |
| 109 |
inline_code_lexer="python", |
| 110 |
inline_code_theme="emacs", |
| 111 |
) |
| 112 |
result = render(markdown) |
| 113 |
expected = "inline \x1b[1;38;2;170;34;255;48;2;248;248;248mimport\x1b[0m\x1b[38;2;187;187;187;48;2;248;248;248m \x1b[0m\x1b[1;38;2;0;0;255;48;2;248;248;248mthis\x1b[0m code \n" |
| 114 |
print(result) |
| 115 |
print(repr(result)) |
| 116 |
assert result == expected |
| 117 |
|
| 118 |
|
| 119 |
def test_markdown_table(): |
| 120 |
markdown = Markdown( |
| 121 |
"""\ |
| 122 |
| Year | Title | Director | Box Office (USD) | |
| 123 |
|------|:------------------------------------------------:|:------------------|------------------:| |
| 124 |
| 1982 | *E.T. the Extra-Terrestrial* | Steven Spielberg | $792.9 million | |
| 125 |
| 1980 | Star Wars: Episode V – The Empire Strikes Back | Irvin Kershner | $538.4 million | |
| 126 |
| 1983 | Star Wars: Episode VI – Return of the Jedi | Richard Marquand | $475.1 million | |
| 127 |
| 1981 | Raiders of the Lost Ark | Steven Spielberg | $389.9 million | |
| 128 |
| 1984 | Indiana Jones and the Temple of Doom | Steven Spielberg | $333.1 million | |
| 129 |
""" |
| 130 |
) |
| 131 |
result = render(markdown) |
| 132 |
expected = "\n\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mYear\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[1m \x1b[0m\x1b[36mTitle\x1b[0m\x1b[1m \x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mDirector\x1b[0m\x1b[1m \x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mBox Office (USD)\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ──────────────────────────────────────────────────────────────────────────────────────── \x1b[0m\n\x1b[36m \x1b[0m1982 \x1b[36m \x1b[0m \x1b[3mE.T. the Extra-Terrestrial\x1b[0m \x1b[36m \x1b[0mSteven Spielberg \x1b[36m \x1b[0m $792.9 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m1980 \x1b[36m \x1b[0mStar Wars: Episode V – The Empire Strikes Back \x1b[36m \x1b[0mIrvin Kershner \x1b[36m \x1b[0m $538.4 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m1983 \x1b[36m \x1b[0m Star Wars: Episode VI – Return of the Jedi \x1b[36m \x1b[0mRichard Marquand \x1b[36m \x1b[0m $475.1 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m1981 \x1b[36m \x1b[0m Raiders of the Lost Ark \x1b[36m \x1b[0mSteven Spielberg \x1b[36m \x1b[0m $389.9 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m1984 \x1b[36m \x1b[0m Indiana Jones and the Temple of Doom \x1b[36m \x1b[0mSteven Spielberg \x1b[36m \x1b[0m $333.1 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\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\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mYear\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mThis \x1b[0m\x1b[36mcolumn\x1b[0m\x1b[36m displays \x1b[0m\x1b[3;36mthe\x1b[0m\x1b[36m movie \x1b[0m\x1b[3;36mtitle\x1b[0m\x1b[36m \x1b[0m\x1b[9;36mdescription\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mDirector\x1b[0m\x1b[1m \x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mBox Office (USD)\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ────────────────────────────────────────────────────────────────────────────────────────── \x1b[0m\n\x1b[36m \x1b[0m1982 \x1b[36m \x1b[0m \x1b[3mE.T. the Extra-Terrestrial\x1b[0m (\x1b]8;id=0;foo\x1b\\\x1b[4;34mWikipedia article\x1b[0m\x1b]8;;\x1b\\) \x1b[36m \x1b[0mSteven Spielberg \x1b[36m \x1b[0m $792.9 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m1980 \x1b[36m \x1b[0m Star Wars: Episode V – The \x1b[3mEmpire\x1b[0m \x1b[1mStrikes\x1b[0m \x1b[9mBack\x1b[0m \x1b[36m \x1b[0mIrvin Kershner \x1b[36m \x1b[0m $538.4 million\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\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\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mleft\x1b[0m\x1b[1m \x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[1m \x1b[0m\x1b[36mcenter\x1b[0m\x1b[1m \x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[1m \x1b[0m\x1b[36mright\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ──────────────────────────────────────────────────────────────── \x1b[0m\n\x1b[36m \x1b[0mThis is a long row \x1b[36m \x1b[0mbecause it contains \x1b[36m \x1b[0ma fairly long sentence.\x1b[36m \x1b[0m\n\x1b[36m \x1b[0ma\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e \x1b[36m \x1b[0m a\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e \x1b[36m \x1b[0m a\x1b[3mb\x1b[0m \x1b[3mc\x1b[0m \x1b[9md\x1b[0m e\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\n" |
| 166 |
assert result == expected |
| 167 |
|
| 168 |
|
| 169 |
def test_partial_table(): |
| 170 |
markdown = Markdown("| Simple | Table |\n| ------ | ----- ") |
| 171 |
result = render(markdown) |
| 172 |
print(repr(result)) |
| 173 |
expected = "\n\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mSimple\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\x1b[36mTable\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ───────────── \x1b[0m\n\x1b[36m \x1b[0m\n" |
| 174 |
assert result == expected |
| 175 |
|
| 176 |
|
| 177 |
def test_table_with_empty_cells() -> None: |
| 178 |
"""Test a table with empty cells is rendered without extra newlines above. |
| 179 |
Regression test for #3027 https://github.com/Textualize/rich/issues/3027 |
| 180 |
""" |
| 181 |
complete_table = Markdown( |
| 182 |
"""\ |
| 183 |
| First Header | Second Header | |
| 184 |
| ------------- | ------------- | |
| 185 |
| Content Cell | Content Cell | |
| 186 |
| Content Cell | Content Cell | |
| 187 |
""" |
| 188 |
) |
| 189 |
table_with_empty_cells = Markdown( |
| 190 |
"""\ |
| 191 |
| First Header | | |
| 192 |
| ------------- | ------------- | |
| 193 |
| Content Cell | Content Cell | |
| 194 |
| | Content Cell | |
| 195 |
""" |
| 196 |
) |
| 197 |
result = len(render(table_with_empty_cells).splitlines()) |
| 198 |
expected = len(render(complete_table).splitlines()) |
| 199 |
assert result == expected |
| 200 |
|
| 201 |
|
| 202 |
def test_inline_code_in_table_cells() -> None: |
| 203 |
"""Test inline code in table cells. |
| 204 |
|
| 205 |
Regression test for https://github.com/Textualize/rich/issues/4038 |
| 206 |
|
| 207 |
""" |
| 208 |
markdown = Markdown( |
| 209 |
"| Col |\n|---|\n| `print('hello');` |\n", |
| 210 |
inline_code_theme="monokai", |
| 211 |
inline_code_lexer="python", |
| 212 |
) |
| 213 |
result = render(markdown) |
| 214 |
expected = "\n\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mCol\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ─────────────── \x1b[0m\n\x1b[36m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mprint\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m(\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34m'\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34mhello\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34m'\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m)\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m;\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\n" |
| 215 |
assert result == expected |
| 216 |
|
| 217 |
|
| 218 |
if __name__ == "__main__": |
| 219 |
markdown = Markdown(MARKDOWN) |
| 220 |
rendered = render(markdown) |
| 221 |
print(rendered) |
| 222 |
print(repr(rendered)) |