How to make markdown module python not convert internal content to html - Stack Overflow

admin2025-04-17  3

md converter, somewhat modular

import os

markdown_enabled = False
markdown_install_fail = False

def configure(markdown_status):
    global markdown_enabled
    if markdown_status.lower() == "yes":
        markdown_enabled = True
    elif markdown_status.lower() == "no":
        markdown_enabled = False
    else:
        print("Unknown Setting")
        markdown_enabled = False

if markdown_enabled:
    try:
        import markdown
    except ImportError:
        print("Markdown failed to import.\nAttempting to install it.")
        try:
            os.system("pip install markdown")
            import markdown
        except:
            print("Error: Could Not install Markdown\nUsing basic built-in interpreting")
            markdown_install_fail = True

def basic_markdown_to_html(text):
    text = text.replace("**", "<b>").replace("**", "</b>")
    text = text.replace("```html", "<pre><code>").replace("```", "</code></pre>")
    return text

def convert(text):
    if markdown_enabled and not markdown_install_fail:
        print("Converting to Markdown...")
        print("Using normal markdown processor")
        return markdown.markdown(text)
        print("Markdown Processed")
    else:
        print("Using basic Markdown to HTML conversion...")
        return basic_markdown_to_html(text)

The md processor is converting internal content of the markdown tag to html, and the page is using it, or at least that's what I think.

The ai processor seems like it outputted html, but im seeing processed content like

My first website

content

check the image for more details.

I am trying to make it convert the ais content to html, like chat gpt. It is ouput is html rendered on the page

md converter, somewhat modular

import os

markdown_enabled = False
markdown_install_fail = False

def configure(markdown_status):
    global markdown_enabled
    if markdown_status.lower() == "yes":
        markdown_enabled = True
    elif markdown_status.lower() == "no":
        markdown_enabled = False
    else:
        print("Unknown Setting")
        markdown_enabled = False

if markdown_enabled:
    try:
        import markdown
    except ImportError:
        print("Markdown failed to import.\nAttempting to install it.")
        try:
            os.system("pip install markdown")
            import markdown
        except:
            print("Error: Could Not install Markdown\nUsing basic built-in interpreting")
            markdown_install_fail = True

def basic_markdown_to_html(text):
    text = text.replace("**", "<b>").replace("**", "</b>")
    text = text.replace("```html", "<pre><code>").replace("```", "</code></pre>")
    return text

def convert(text):
    if markdown_enabled and not markdown_install_fail:
        print("Converting to Markdown...")
        print("Using normal markdown processor")
        return markdown.markdown(text)
        print("Markdown Processed")
    else:
        print("Using basic Markdown to HTML conversion...")
        return basic_markdown_to_html(text)

The md processor is converting internal content of the markdown tag to html, and the page is using it, or at least that's what I think.

The ai processor seems like it outputted html, but im seeing processed content like

My first website

content

check the image for more details.

I am trying to make it convert the ais content to html, like chat gpt. It is ouput is html rendered on the page

Share Improve this question asked Jan 31 at 7:25 QuickMash StudiosQuickMash Studios 112 bronze badges 1
  • Show us some sample input and the raw expected and observed output. – Klaus D. Commented Jan 31 at 7:58
Add a comment  | 

1 Answer 1

Reset to default -1

Would you consider using regular expressions to find Markdown syntax in your text?

I think it will help you to identify pieces of text easier.

For example: bold text is a pattern of: **\w+**

And this will help you avoid replacing irrelevant strings. You need to identify all markdown syntaxes and write similar conversions. I can help you with that too. Let me know if you are happy to utilize regex.

转载请注明原文地址:http://anycun.com/QandA/1744876323a88874.html