Create Markdown to html Converter

Introduction

In this python program, we create a markdown to html converter using the markdown module.
This program allows users to input multiple lines of markdown text and then converts the entire input into html format. The program is interactive, prompting the user to input Markdown text and displaying the HTML output once the input process is complete.

Program

Here is the full program to create markdown to html converter:
import markdown

markdown_text=[]
print("Enter the markdown text(press enter two times to end):")
while True:
    text=input()
    if text:
        markdown_text.append(text)
    else:
        break

md="\n".join(markdown_text)
html=markdown.markdown(md)
print("html text:")
print(html)

 

 Program Explanation

Import the markdown module

import markdown

markdown module is imported which is responsible for converting markdown to html.

Input markdown text

print("Enter the markdown text(press enter two times to end):")
while True:
    text=input()
    if text:
        markdown_text.append(text)
    else:
        break

An empty list markdown_text, is initialized to store the lines of Markdown entered by the user. The program uses a while loop to repeatedly prompt the user to enter text. The loop continues until the user presses enter twice, signaling the end of input.

Combining the input text

md = "\n".join(markdown_text)

After the input ends, the lines stored in markdown_text are joined together into a single string using the join method, with each line separated by a newline .

Convert markdown to html

html = markdown.markdown(md)

The program then converts the combined Markdown string into HTML using the markdown.markdown() function.

Display the html output

print("html text:")
print(html)

Finally, the converted HTML is printed to the console for the user to view.

Output

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top