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] Rendering Inline Color to render Markdown is not working #3587

Closed
1 task done
aiventures opened this issue Dec 14, 2024 · 4 comments
Closed
1 task done

[BUG] Rendering Inline Color to render Markdown is not working #3587

aiventures opened this issue Dec 14, 2024 · 4 comments

Comments

@aiventures
Copy link

aiventures commented Dec 14, 2024

- [X] I've checked docs and closed issues for possible solutions.

  • I can't find my issue in the FAQ.

Hi, rich is great! Maybe you can help me on this here.
I checked yout issue collection and the internet, but found nothing, so I decided to post an issue.

My use case is rendering different colors, icons and markdown using inline Markdown code

The Options I tried is summarized in following ready to run sample code:

# Issue with Markdown Class
# VERSIONS:
# WINDOWS OS in NEW COMMAND LINE EDITO + BASH
# Python 3.11.1
# rich                      12.6.0
[Diagnosis_Data.txt](https://github.com/user-attachments/files/18135496/Diagnosis_Data.txt)

# rich-cli                  1.8.0
# rich-rst                  1.3.1

from rich.console import Console
from rich.markdown import Markdown
from rich.text import Text
from rich.table import Table
from rich.emoji import Emoji
import re

print("------------")
# I also tested Options using color system
console = Console()
# this is my use case: markdown with ,multiple colors and icons in one line
s = f"An [red]APPLE[/red] :apple: keeps the [blue]DOCTOR[/blue] away "
# you might also uncomment emoji conversion / has no effect on issue when deactivated
s = Emoji.replace(s)
s_out = [f"* CASE 1 (print)                   {s}",
         f"* CASE 2 (MARKDOWN inline -error)  {s}",
         f"* CASE 3 (Using Text with style)   {s}",
         f"* CASE 4 (Using Text using markup) {s}",
         f"* CASE 5 (Using markup fix)        {s}",
         f"* CASE 6 (MARKDOWN workaround)     {s}"]

# CASE 1 Test plain rendering
console.print(s_out[0])

# CASE 2 Using MARKDOWN, ERROR JUST SHOWS BRACKETS
# inline_code_lexer has no effect for my use case 
# code_theme has no effect for my use case
console.print(Markdown(s_out[1],inline_code_lexer="python"))

# CASE 3 Try Using Text as intermediate workaround doesn't work 
t = Text(s_out[2],style="red")
# this demonstrates the working of markup for print but not in MarkDown format 
# console.print(Markdown(t.markup)) # will also lead to non coloring ... 
console.print(t.markup)

# CASE 4 Try Using Text with markup
markup = Text(s_out[3]).markup
console.print(markup)

# CASE 5 Try Using rendering with an ugly workaround 
# Note markup value in debugger shows up as follows (escaping the brackets "[" "):
# '* CASE 4 (Using Text using markup) An \\[red]APPLE\\[/red] 🍎 keeps the \\[blue]DOCTOR\\[/blue] away '
markup = Text(s_out[4]).markup
markup_fix = re.sub("\\\\\[","[",markup)
console.print(markup_fix)

# CASE 6 Using the fix to get a working Markdown
markup = Text(s_out[5]).markup
markup_fix = re.sub("\\\\\[","[",markup)
console.print(Markdown(markup_fix))
print("------------")

What I get as output
20241214_112439

Issue description

  • Software / OS Versions in sample code
  • Color rendering is ok for using text, but not markup (Case 1,Case 5)
  • Code keeps running against the wall ignoring [color]..[/color] inline code when trying to use Markdown(...)
  • Converting it to Text and getting back str using Markdown(Text(...).markup) / try to fiddle with escaping (= dropping the extra \ escape char in front of [ ) won't also help (Case 6)
  • Also attached Diagnosis data. Color Rendering works, but not for Markdown.

This is only a pet project, so It's not a pressing issue ...

Thanks !

Platform / Installation information

Click to expand
  • WINDOWS OS using WINDOWS CONSOLE BASH
  • Python 3.11.1
  • rich 12.6.0
  • rich-cli 1.8.0
  • rich-rst 1.3.1
  • Rich Package Dependencies on my machine:
[
    {
        "package": {
            "key": "commonmark",
            "package_name": "commonmark",
            "installed_version": "0.9.1"
        },
        "dependencies": []
    },
    {
        "package": {
            "key": "pygments",
            "package_name": "Pygments",
            "installed_version": "2.18.0"
        },
        "dependencies": []
    },
    {
        "package": {
            "key": "rich",
            "package_name": "rich",
            "installed_version": "12.6.0"
        },
        "dependencies": [
            {
                "key": "commonmark",
                "package_name": "commonmark",
                "installed_version": "0.9.1",
                "required_version": ">=0.9.0,<0.10.0"
            },
            {
                "key": "pygments",
                "package_name": "Pygments",
                "installed_version": "2.18.0",
                "required_version": ">=2.6.0,<3.0.0"
            }
        ]
    }
]
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

Rich's console markup and Markdown are two entirely unrelated markups. I wouldn't expect them to work together.

That said, if you want to create this hybrid markup system, I would make a copy of the Markdown class and adjust it to allow console markup.

@aiventures
Copy link
Author

Thanks @willmcgugan that was a quicl response. Will consider adapting.
However it "looks" for me like it would be natural to use Markdown("[color]...") to link both system together. Ok, will close the issue. Thanks and have good week end.

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

No branches or pull requests

2 participants