Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: color is missing if I print a diff with rich.syntax #43

Closed
15r10nk opened this issue Apr 15, 2024 · 8 comments
Closed

bug: color is missing if I print a diff with rich.syntax #43

15r10nk opened this issue Apr 15, 2024 · 8 comments
Assignees

Comments

@15r10nk
Copy link

15r10nk commented Apr 15, 2024

Description of the bug

I use markdown-exec to include the output of pytest in my documentation.
I use rich to generate some diffs during this run and have the problem that the red and green colors are missing in the diff.

image

I was able to reproduce this bug without pytest, but I have no idea if this is a markdown-exec or rich problem.

Can you please look into?

To Reproduce

see the repository

pip install -r requirements.txt
mkdocs serve

Expected behavior

I would expect that markdown-exec is able to produce the same colors which I also see in the terminal.

Environment information

python -m markdown_exec.debug  # | xclip -selection clipboard
  • System: Linux-6.1.0-18-amd64-x86_64-with-glibc2.36
  • Python: cpython 3.12.1
  • Environment variables:
  • Installed packages:
    • markdown-exec v1.8.0

Additional context

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Hey @15r10nk, thanks for the report!

Interesting. My first intuition is that Rich uses ANSI codes that are not supported by pygments-ansi. I'll replicate the issue locally and report back 🙂

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Unfortunately none of the available color systems improve the situation 😕 I really thought the standard one would help.

I don't know Rich enough to understand what it does with ANSI codes. I think its Syntax objects are a bit more clever and use more than just the 16 color variations. Rich also sets a background for the diff block, that might be what is causing issues here, as background+foreground can be combined in a single ANSI sequence that maybe pygments-ansi or its style sheet have trouble with.

Workaround:

# codi.py

import os
from rich.console import Console
from rich.syntax import Syntax

console=Console(color_system="256")

code = """
  code
- a
+ b
"""

with open(os.devnull, "w") as devnull:
    console = Console(record=True, width=65, file=devnull, markup=False)
    renderable = Syntax(code, "diff", theme="material")
    console.print(renderable, markup=True, highlight=False)
print(console.export_html(inline_styles=True, code_format="<pre><code>{code}</code></pre>"))
<!-- index.md -->
```python exec="1" html="1"
--8<-- "codi.py"
```

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Here's the ANSI output of Rich with 256 color mode:

\e[31m red\e[0m
\e[32m green\e[0m
\e[48;5;235m                                                                   \e[0m
\e[38;5;231;48;5;235m \e[0m\e[38;5;231;48;5;235m code\e[0m\e[48;5;235m                                                             \e[0m
\e[38;5;204;48;5;235m- a\e[0m\e[48;5;235m                                                                \e[0m
\e[38;5;148;48;5;235m+ b\e[0m\e[48;5;235m                                                                \e[0m
\e[48;5;235m                                                                   \e[0m

I've manually escaped the escape sequence (\e) and removed some whitespace.

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Here it is with the standard color mode:

\e[31m red\e[0m
\e[32m green\e[0m
\e[40m                                      \e[0m
\e[97;40m \e[0m\e[97;40m code\e[0m\e[40m                                \e[0m
\e[91;40m- a\e[0m\e[40m                                   \e[0m
\e[92;40m+ b\e[0m\e[40m                                   \e[0m
\e[40m                                      \e[0m

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

It's a bit better with this (works for b but not for a for some reason):

console = Console(color_system="standard")
code = """
  code
- a
+ b
"""
console.print(Syntax(code, "diff", theme="ansi_light"))

@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Aha! This translates as a -Color-BrightRed CSS class but there's no such class in the style sheet, only -Color-Bold-Red. Let me try to update the style sheet 😄

pawamoy added a commit that referenced this issue Apr 15, 2024
For some reason, pygments-ansi-color sometimes output `-Color-BrightRed` CSS classes instead of `-Color-Bold-Red`. These class weren't declared so spans with these classes were unstyled. We fix this by adding such classes for each color.

Issue-43: #43
@pawamoy
Copy link
Owner

pawamoy commented Apr 15, 2024

Should be fixed in v1.8.1. Try with the standard color system and the ansi_light/ansi_dark theme and let me know if there are any other issues 🙂

@pawamoy pawamoy closed this as completed Apr 15, 2024
@15r10nk
Copy link
Author

15r10nk commented Apr 15, 2024

Thank you, it works for me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants