1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import re
def md_to_html(md_content): """将Markdown文本转换为HTML。"""
md_content = re.sub(r'### (.*)', r'<h3>\1</h3>', md_content) md_content = re.sub(r'## (.*)', r'<h2>\1</h2>', md_content) md_content = re.sub(r'# (.*)', r'<h1>\1</h1>', md_content)
md_content = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', md_content) md_content = re.sub(r'\*(.*?)\*', r'<em>\1</em>', md_content) md_content = re.sub(r'_(.*?)_', r'<em>\1</em>', md_content)
md_content = re.sub(r'\n\* (.*)', r'\n<li>\1</li>', md_content) md_content = re.sub(r'(<li>.*?</li>)', r'<ul>\1</ul>', md_content, flags=re.DOTALL)
html_content = f"""<!DOCTYPE html> <html> <head> <title>Markdown Converted Document</title> <meta charset="utf-8"> </head> <body> {md_content} </body> </html>"""
return html_content
def convert_md_to_html(md_file_path, html_file_path): with open(md_file_path, 'r', encoding='utf-8') as md_file: md_content = md_file.read()
html_content = md_to_html(md_content)
with open(html_file_path, 'w', encoding='utf-8') as html_file: html_file.write(html_content)
print(f"Markdown file {md_file_path} has been converted to HTML file {html_file_path}")
if __name__ == "__main__": import sys if len(sys.argv) != 3: print("Usage: python md_to_html.py [markdown_file_path] [html_file_path]") else: md_file_path = sys.argv[1] html_file_path = sys.argv[2] convert_md_to_html(md_file_path, html_file_path)
|