#!/usr/bin/env python3
"""
Markdown 转微信公众号 HTML 转换器(doocs/md 风格)
参考 doocs/md 项目的优秀设计,提供多种专业主题。
使用方法:
python3 markdown_to_wechat_doocs.py --input article.md --output article.html --theme default
支持主题:
- default: 默认主题(简洁优雅)
- green: 绿意主题(清新自然)
- purple: 紫色主题(优雅高贵)
- orange: 橙心主题(温暖活力)
- cyan: 青色主题(清爽专业)
"""
import os
import sys
import argparse
import re
# doocs/md 风格的主题配置
THEMES = {
"default": {
"name": "默认主题",
"primary": "#3f51b5",
"text": "#2b2b2b",
"text_light": "#595959",
"bg_light": "#f8f9fa",
"border": "#e0e0e0",
"code_bg": "#f6f8fa",
"quote_bg": "#f8f9fa",
"quote_border": "#3f51b5",
},
"green": {
"name": "绿意主题",
"primary": "#3eaf7c",
"text": "#2c3e50",
"text_light": "#6c757d",
"bg_light": "#f3f5f7",
"border": "#eaecef",
"code_bg": "#f3f5f7",
"quote_bg": "#f3f5f7",
"quote_border": "#3eaf7c",
},
"purple": {
"name": "紫色主题",
"primary": "#8e44ad",
"text": "#2c3e50",
"text_light": "#6c757d",
"bg_light": "#f8f9fa",
"border": "#e0e0e0",
"code_bg": "#f8f9fa",
"quote_bg": "#f8f9fa",
"quote_border": "#8e44ad",
},
"orange": {
"name": "橙心主题",
"primary": "#ff6800",
"text": "#2c3e50",
"text_light": "#6c757d",
"bg_light": "#fff8f0",
"border": "#ffe4cc",
"code_bg": "#fff8f0",
"quote_bg": "#fff8f0",
"quote_border": "#ff6800",
},
"cyan": {
"name": "青色主题",
"primary": "#00bcd4",
"text": "#2c3e50",
"text_light": "#6c757d",
"bg_light": "#f0f9ff",
"border": "#d1ecf1",
"code_bg": "#f0f9ff",
"quote_bg": "#f0f9ff",
"quote_border": "#00bcd4",
}
}
def get_theme(theme_name):
"""获取主题配置"""
return THEMES.get(theme_name, THEMES["default"])
def markdown_to_html_doocs(markdown_text, theme_name="default"):
"""
将 Markdown 转换为 doocs/md 风格的 HTML
"""
theme = get_theme(theme_name)
html_lines = []
in_list = False
in_ordered_list = False
in_blockquote = False
in_code_block = False
code_block_lines = []
lines = markdown_text.split('\n')
i = 0
while i < len(lines):
line = lines[i]
# 代码块
if line.strip().startswith('```'):
if not in_code_block:
in_code_block = True
code_block_lines = []
else:
in_code_block = False
code_content = '\n'.join(code_block_lines)
code_content = code_content.replace('<', '<').replace('>', '>')
# 代码块内换行用
标签,确保微信公众号正确显示
code_content = code_content.replace('\n', '
')
html_lines.append(f'''{code_content}
''') in_blockquote = True content = process_inline(line[2:], theme) html_lines.append(f'''{content}
''') # 图片 elif re.match(r'!\[.*?\]\(.*?\)', line): match = re.match(r'!\[(.*?)\]\((.*?)\)', line) if match: alt = match.group(1) src = match.group(2) html_lines.append(f'''') # 普通段落 else: content = process_inline(line, theme) html_lines.append(f''' ''') if alt: html_lines.append(f'''
▲ {alt}
''') html_lines.append('{content}
''') i += 1 # 关闭未关闭的标签 if in_list: # 移除ul/li之间的换行符,避免微信公众号显示多余空行 html_lines.append('
\1', text)
# 链接
text = re.sub(r'\[(.*?)\]\((.*?)\)', rf'\1', text)
return text
def main():
"""命令行入口"""
parser = argparse.ArgumentParser(
description="Markdown 转微信公众号 HTML 转换器(doocs/md 风格)",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
支持主题:
default - 默认主题(简洁优雅)
green - 绿意主题(清新自然)
purple - 紫色主题(优雅高贵)
orange - 橙心主题(温暖活力)
cyan - 青色主题(清爽专业)
"""
)
parser.add_argument(
"--input", "-i",
required=True,
help="输入 Markdown 文件路径"
)
parser.add_argument(
"--output", "-o",
required=True,
help="输出 HTML 文件路径"
)
parser.add_argument(
"--theme", "-t",
choices=["default", "green", "purple", "orange", "cyan"],
default="default",
help="主题选择"
)
args = parser.parse_args()
try:
# 读取 Markdown 文件
with open(args.input, 'r', encoding='utf-8') as f:
markdown_text = f.read()
# 自动读取 footer.md 并拼接到文章末尾
footer_path = os.path.join(os.path.dirname(__file__), '..', 'footer.md')
if os.path.exists(footer_path):
with open(footer_path, 'r', encoding='utf-8') as f:
footer_content = f.read()
if footer_content:
markdown_text = markdown_text.rstrip() + '\n\n' + footer_content
# 转换为 HTML
html_content = markdown_to_html_doocs(markdown_text, args.theme)
# 获取主题信息
theme = get_theme(args.theme)
# 包装在 section 中
final_html = f'''\n+ {i}. {item} • {item}\n+
\n+
]*>.*?
', replace_ol, html_content, flags=re.DOTALL)
# 将ul(无序列表)转换为段落格式
def replace_ul(match):
items = re.findall(r']*>.*?
', replace_ul, html_content, flags=re.DOTALL)
return html_content
if __name__ == "__main__":
main()