| 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 |
""" |
| 65 |
|
| 66 |
import io |
| 67 |
import re |
| 68 |
|
| 69 |
from rich.console import Console, RenderableType |
| 70 |
from rich.markdown import Markdown |
| 71 |
|
| 72 |
re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b") |
| 73 |
|
| 74 |
|
| 75 |
def replace_link_ids(render: str) -> str: |
| 76 |
"""Link IDs have a random ID and system path which is a problem for |
| 77 |
reproducible tests. |
| 78 |
|
| 79 |
""" |
| 80 |
return re_link_ids.sub("id=0;foo\x1b", render) |
| 81 |
|
| 82 |
|
| 83 |
def render(renderable: RenderableType) -> str: |
| 84 |
console = Console( |
| 85 |
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False |
| 86 |
) |
| 87 |
console.print(renderable) |
| 88 |
output = replace_link_ids(console.file.getvalue()) |
| 89 |
return output |
| 90 |
|
| 91 |
|
| 92 |
def test_markdown_render(): |
| 93 |
markdown = Markdown(MARKDOWN, hyperlinks=False) |
| 94 |
rendered_markdown = render(markdown) |
| 95 |
print(repr(rendered_markdown)) |
| 96 |
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[94mexample\x1b[0m (\x1b[4;34mhttp://example.com\x1b[0m). \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🌆 progress \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" |
| 97 |
assert rendered_markdown == expected |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == "__main__": |
| 101 |
markdown = Markdown(MARKDOWN, hyperlinks=False) |
| 102 |
rendered = render(markdown) |
| 103 |
print(rendered) |
| 104 |
print(repr(rendered)) |