Open Erotemic opened 1 year ago
A funny collary to this issue:
The text:
[blue][link=/data/store/Media/Music/Deep Purple]Deep Purple[/link][/blue]
In the block:
├─╼ [blue][link=/data/store/Media/Music/Jethro Tull]Jethro Tull[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/Bread]Bread[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/Deep Purple]Deep Purple[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/Tenacious D]Tenacious D[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/The Shins]The Shins[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/Dragonforce]Dragonforce[/link][/blue]
├─╼ [blue][link=/data/store/Media/Music/Red Hot Chili Peppers]Red Hot Chili Peppers[/link][/blue]
Renders like this:
However:
rich.print('[blue][link=file:///data/store/Media/Music/Deep%20Purple]Deep Purple[/link][/blue]')
does work.
I was able to solve this by replaceing the brakets with "%5B" and "%5D":
import rich
import pathlib
import os
path = pathlib.Path('/data/store/Media/Music/Bon Iver/For Emma, Forever Ago [2007]')
try:
encoded_path = 'file://' + os.fspath(path).replace(' ', '%20').replace('[', '%5B').replace(']', '%5D')
escaped_path = rich.markup.escape(os.fspath(path))
rich.print(rf'[blue][link={encoded_path}]{escaped_path}[/link][/blue]')
except Exception as ex:
print(f'ex={ex!r}')
It might be nice to mention URL encoding in the links docs and refer to urllib.parse
(e.g. 'file://' + urllib.parse.quote(os.fspath(path))
)
I found that when the brackets are surrounded by blanks, the MarkupError will not occur.
Describe the bug
It seems like rich.print cannot handle paths that contain square brakets using the
[link]
directive.MWE:
This causes:
Furthermore, the standard things that I could think of to escape the square brakets don't seem to work.
Platform
Click to expand
OS: Ubuntu 22.04.3 LTS (x86_64) Python version: 3.11.2 (main, Apr 1 2023, 18:27:37) [GCC 11.3.0] (64-bit runtime) Python platform: Linux-6.2.0-36-generic-x86_64-with-glibc2.35 ``` ╭─────────────────────────Is there an escape character specific for link? Or is there a way I can "encode-away" the braket in the path? Or is this just a bug that markup links don't currently support?