Advertisement
Guest User

markdown to html converter

a guest
Nov 20th, 2023
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | Source Code | 0 0
  1. import re
  2. import mistune
  3. from bs4 import BeautifulSoup as bs
  4.  
  5.  
  6. def main():
  7.     file = input("Name of Markdown file: ")
  8.     while ('.md' not in file):
  9.         print("that is not a recognized Markdown file.")
  10.         file = input("Name of Markdown file: ")
  11.  
  12.     fileName = file.split('.')[0]
  13.     html_doc = open(fileName+".html", 'w')
  14.  
  15.     generated_html = ("<!DOCTYPE html>" +
  16.                       "<!--Converted via md-to-html-->" +
  17.                       "<html><head></head><body>")
  18.  
  19.     with open(file) as f:
  20.         content = f.readlines()
  21.         for line in content:
  22.             generated_html += mistune.markdown(line)
  23.  
  24.     generated_html += "</body></html>"
  25.  
  26.     # make BeautifulSoup
  27.     soup = bs(generated_html, "html.parser")
  28.     # prettify the html
  29.     prettyHTML = soup.prettify()
  30.     # write to the html doc
  31.     html_doc.write(prettyHTML)
  32.  
  33.  
  34. if __name__ == "__main__":
  35.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement