drudru/ansi_up

Combining link and color change doesn't work

Closed this issue · 5 comments

Hi, thanks for this library, I'm considering using it over my homebrew solution for ANSI to HTML.
I've a need for "colored links" but combining links and color results in rendering errors in ansi_up.

Here's a test.py producing ANSI using Python's Rich. (pip3 install rich)

from rich.console import Console

console = Console(color_system="standard", width=160, highlight=False)

console.print("[bold red]This is bold red text[/bold red]")
console.print("[link=https://google.com/]This links to Google[/link]")
console.print("[link=https://google.com/][bold red]This links to Google and is bold red[/bold red][/link]")
console.print("[bold red]bold red here[/bold red] then a [link=https://google.com/]a link[/link]")

If you then run it

python3 test.py > test.ans
cat test.ans # renders correctly in iTerm2 etc

image

But rendering it though ansi_up I get:

image

Any ideas?
Super thanks!

drudru commented

Hi, thanks for the report. Can you attach the test.ans file (or provide a hex dump)?
I will take a look.

Sure, here it is (produced with the script above). It's been .zip'ed since GH won't allow ".ans" files.
test.ans.zip

Here's the hexdump of the same test.ans:

0000000 5b1b 3b31 3133 546d 6968 2073 7369 6220
0000010 6c6f 2064 6572 2064 6574 7478 5b1b 6d30
0000020 1b0a 385d 693b 3d64 3131 3639 3134 683b
0000030 7474 7370 2f3a 672f 6f6f 6c67 2e65 6f63
0000040 2f6d 5c1b 6854 7369 6c20 6e69 736b 7420
0000050 206f 6f47 676f 656c 5d1b 3b38 1b3b 0a5c
0000060 5d1b 3b38 6469 393d 3136 3137 3b37 7468
0000070 7074 3a73 2f2f 6f67 676f 656c 632e 6d6f
0000080 1b2f 1b5c 315b 333b 6d31 6854 7369 6c20
0000090 6e69 736b 7420 206f 6f47 676f 656c 6120
00000a0 646e 6920 2073 6f62 646c 7220 6465 5b1b
00000b0 6d30 5d1b 3b38 1b3b 0a5c 5b1b 3b31 3133
00000c0 626d 6c6f 2064 6572 2064 6568 6572 5b1b
00000d0 6d30 7420 6568 206e 2061 5d1b 3b38 6469
00000e0 353d 3635 3636 3b39 7468 7074 3a73 2f2f
00000f0 6f67 676f 656c 632e 6d6f 1b2f 615c 6c20
0000100 6e69 1b6b 385d 3b3b 5c1b 000a
000010b
drudru commented

Thanks for the hexdump and file.
I added the ascii interpretation to the dump to make it a bit easier to decipher.

00000000  1b 5b 31 3b 33 31 6d 54  68 69 73 20 69 73 20 62  |.[1;31mThis is b|
00000010  6f 6c 64 20 72 65 64 20  74 65 78 74 1b 5b 30 6d  |old red text.[0m|
00000020  0a 1b 5d 38 3b 69 64 3d  31 31 39 36 34 31 3b 68  |..]8;id=119641;h|
00000030  74 74 70 73 3a 2f 2f 67  6f 6f 67 6c 65 2e 63 6f  |ttps://google.co|
00000040  6d 2f 1b 5c 54 68 69 73  20 6c 69 6e 6b 73 20 74  |m/.\This links t|
00000050  6f 20 47 6f 6f 67 6c 65  1b 5d 38 3b 3b 1b 5c 0a  |o Google.]8;;.\.|
00000060  1b 5d 38 3b 69 64 3d 39  36 31 37 31 37 3b 68 74  |.]8;id=961717;ht|
00000070  74 70 73 3a 2f 2f 67 6f  6f 67 6c 65 2e 63 6f 6d  |tps://google.com|
00000080  2f 1b 5c 1b 5b 31 3b 33  31 6d 54 68 69 73 20 6c  |/.\.[1;31mThis l|
00000090  69 6e 6b 73 20 74 6f 20  47 6f 6f 67 6c 65 20 61  |inks to Google a|
000000a0  6e 64 20 69 73 20 62 6f  6c 64 20 72 65 64 1b 5b  |nd is bold red.[|
000000b0  30 6d 1b 5d 38 3b 3b 1b  5c 0a 1b 5b 31 3b 33 31  |0m.]8;;.\..[1;31|
000000c0  6d 62 6f 6c 64 20 72 65  64 20 68 65 72 65 1b 5b  |mbold red here.[|
000000d0  30 6d 20 74 68 65 6e 20  61 20 1b 5d 38 3b 69 64  |0m then a .]8;id|
000000e0  3d 35 35 36 36 36 39 3b  68 74 74 70 73 3a 2f 2f  |=556669;https://|
000000f0  67 6f 6f 67 6c 65 2e 63  6f 6d 2f 1b 5c 61 20 6c  |google.com/.\a l|
00000100  69 6e 6b 1b 5d 38 3b 3b  1b 5c 0a                 |ink.]8;;.\.|
0000010b
drudru commented

Hi @drudru - ok.
Thanks for uploading the file.
Unfortunately, I see the problem.

The 'rich' library is generating codes outside of the spec.
https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#encodings

There shouldn't be any way to style the text via ansi codes inside of an OSC 8 URL.

This limitation is good from multiple perspectives

  • from a security perspective, the code becomes more difficult
  • in general, It also makes my code much more complicated
  • it is rare that people use tags in the content part of an anchor

So, I see this as not a bug

Thanks for reporting it though

Well thanks for taking a look.