[BUG] Markup escaping not working as expected
See original GitHub issueDescribe the bug
It’s a bit of an edge case, but right now it’s not possible to output a backslash immediately before a square bracket starting a markup token. While it does properly output a single backslash if you feed in r'\\'
followed by a markup token, it still disables interpreting the token as markup.
To Reproduce Working cases:
rich.print(r'[bold]text[/]')
prints “text”
rich.print(r'\[not a tag]')
prints “[not a tag]”
Not working case:
rich.print(r'\\[bold]some text[/]')
gives an error about an unexpected close tag, because it doesn’t treat “[bold]” as markup. The expected result here was “\text”, a single backslash followed by the text in bold.
Adding more backslashes also produces a surprising result. It seems like the rightmost backslash is always consumed to disable markup on the square bracket, but all the previous backslashes are passed through as-is, rather than having every other backslash escape the following one. So, with three backslashes, I was expecting one to be output (consuming the first two of the input) and one to be left over to escape the square bracket and disable markup. WIth four backslashes, I was expecting two backslashes to be output (consuming all four of the input) and the square bracket after that to be treated as markup.
Platform Rich 9.5.1 on macOS Mojave (10.14.6) with Apple’s Terminal.app.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10
Top GitHub Comments
Looks good - thanks!
Preserving backslashes as-is elsewhere in the string makes sense to me. I do something similar in some code I wrote which allows users to input raw unicode characters by entering something like
\u203c
or\U0001f348
inside their input string, converting those sequences to a single Unicode character when the entered code point is a valid printable character. Entering\\u203c
is turned into\u203c
(removing one backslash), but entering\\
not followed by a “u” or “U” and the right number of hex digits is left alone.Upgraded here - looks great. Thanks so much!