把Markdown文件转换为HTML文件

Python 有一个名为 markdown 的库就可以处理这个问题,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import markdown
import sys

def convert_md_to_html(md_file_path, html_file_path):
# 读取Markdown文件
with open(md_file_path, 'r', encoding='utf-8') as md_file:
md_content = md_file.read()

# 使用markdown库将Markdown转换为HTML
html_content = markdown.markdown(md_content)

# 写入HTML文件
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__":
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)

然后用下面代码执行就可以:

1
python md_to_html.py example.md example.html

其实绝大多数的 Markdown 文件,都可以用正则表达式手写完成:

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结构中
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)