Introduction
Markdown is a easy to use markup language used for formatting plain text. In this python program , we convert markdown to html . The program allows users to store html output text in a html file if needed.
Program
Make sure that markdown library is installed or install it with the below code in the command prompt:
pip install markdown
Full program to create markdown to html converter in python:
import markdown def conversion(text): html_text=markdown.markdown(text) return html_text print("enter markdown text to html (click enter two times to end):") md=[] while True: text=input() if text: md.append(text) else: break markdown_text = "\n".join(md) converted=conversion(markdown_text) print("markdown text converted to html") print(converted) html_file=input("do you want to save this html text in html file?(yes/no)") if html_file=="yes": try: file_name=input("enter a file name(without .html):") with open(file_name + ".html","w") as file1: file1.write(converted) print("the html text has been saved in the",file_name,".html file") except Exception as e: print("there was a error saving the file",e) else: print("Your html text is not saved ")
Program Explanation
we shall breakdown the program into smaller parts and understand how it works:
Importing markdown library
import markdown
Here, The markdown library is imported to enable the conversion of Markdown text into HTML format.
Defining the conversion function
def conversion(text): html_text=markdown.markdown(text) return html_text
Here, you define a function named conversion that takes a string text as input. The function uses markdown.markdown() to convert the Markdown text into HTML format and it returns the resulting html text.
User Input for markdown text
print("enter markdown text to html (click enter two times to end):") md=[] while True: text=input() if text: md.append(text) else: break
This section prompts the user to enter Markdown text. It initializes an empty list md to store each line of input. The program continues to accept input until the user submits an empty line .
Join lines and Convert to html
markdown_text = "\n".join(md) converted=conversion(markdown_text) print("markdown text converted to html") print(converted)
Here, you join all lines of Markdown text into a single string using newline characters. Then, you call the conversion function to convert this string into HTML. Finally, we print the converted HTML text.
Saving html output in a file
html_file=input("do you want to save this html text in html file?(yes/no)") if html_file=="yes": try: file_name=input("enter a file name(without .html):") with open(file_name + ".html","w") as file1: file1.write(converted) print("the html text has been saved in the",file_name,".html file") except Exception as e: print("there was a error saving the file",e) else: print("Your html text is not saved ")
Here, the program asks the user if they want to save the HTML output. If the user answers “yes,” it prompts for a file name (without the .html extension). The program opens a new file with the specified name, writes the converted HTML content to it, and confirms that the file has been saved.
try except is used so if any error occurs while saving, it prints an error message.
If the user chooses not to save, the program informs them that the HTML text will not be saved.
Output
if the user wants to convert markdown to html and to store the resulting html text in a html file:
if the user wants to convert markdown to html and do not want to store the resulting html text in a html file: