Files
kancloud-download/mdtohtml.py
iuu f17b2e579b 添加 mdtohtml.py
MD文件转html
2024-12-06 03:39:54 +00:00

129 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import markdown
import os
# 定义源目录和目标目录
source_dir = './output' # 存放 Markdown 文件的源目录
target_dir = './output_html' # 生成 HTML 文件的目标目录
# 确保目标目录存在,如果不存在则创建
os.makedirs(target_dir, exist_ok=True)
# 获取文件夹中的所有 .md 文件
def get_md_files(directory):
md_files = []
for root, dirs, files in os.walk(directory):
# 排除隐藏文件夹(例如 .git 等)
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
if file.endswith('.md'):
md_files.append(os.path.relpath(os.path.join(root, file), directory))
return md_files
# 获取源目录下的所有 Markdown 文件
md_files = get_md_files(source_dir)
# 创建目录 HTML 内容
directory_html_content = '''<ul>'''
# 在目录页面添加每个文件的链接
for md_filename in md_files:
# 取文件名的 "_" 后部分作为 HTML 文件名
html_filename = md_filename.split('_', 1)[1].replace('.md', '.html')
# 使用 Markdown 文件名的 "_" 前部分作为目录标题
title = md_filename.split('_', 1)[0]
directory_html_content += f' <li><a href="{html_filename}">{title}</a></li>\n'
directory_html_content += '''</ul>'''
# 创建 HTML 页面,包含正文内容和目录
for md_filename in md_files:
# 获取 Markdown 文件的完整路径
md_file_path = os.path.join(source_dir, md_filename)
# 读取 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 文件名,取 "_" 后部分
html_filename = md_filename.split('_', 1)[1].replace('.md', '.html')
# 获取标题,取 "_" 前部分
title = md_filename.split('_', 1)[0]
# 生成目标 HTML 文件的路径
html_file_path = os.path.join(target_dir, html_filename)
# 创建 HTML 文件头部,设置字符编码为 UTF-8并将目录嵌入到页面
html_header = f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<style>
body {{
display: flex;
font-family: Arial, sans-serif;
margin: 0;
}}
.sidebar {{
width: 220px;
padding: 20px;
background-color: #f4f4f4;
}}
.sidebar a {{
display: block;
padding: 5px;
text-decoration: none;
color: #007bff;
}}
.sidebar a:hover {{
background-color: #ddd;
}}
.content {{
margin-left: 240px;
padding: 20px;
flex: 1;
max-width: 100%;
box-sizing: border-box;
word-wrap: break-word;
}}
h1 {{
margin-top: 0;
}}
img {{
/* width: 100%; 图片自适应宽度 */
height: auto; /* 保持图片纵横比 */
max-width: 100%; /* 防止图片过大 */
}}
</style>
</head>
<body>
<div class="sidebar">
<h2>文件目录</h2>
{directory_html_content}
</div>
<div class="content">
'''
# 添加正文内容
html_content = f'<h1>{title}</h1>' + html_content
# 添加 HTML 文件尾部
html_footer = ''' </div>
</body>
</html>'''
# 完整的 HTML 内容
full_html_content = html_header + html_content + html_footer
# 将 HTML 内容保存到文件
with open(html_file_path, 'w', encoding='utf-8') as html_file:
html_file.write(full_html_content)
print(f"{md_filename} 已转换为 {html_filename}")