脚本工具 批量md转html

md转html,便于打包进APP

就算以后安卓不兼容,APP不能用,自己做个离线网页也能用一辈子

python 复制代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Termux 批量 md → 手机竖屏 html
"""

import os
import pathlib
import markdown
from datetime import datetime

# 1. 待遍历的 md 目录
SRC_DIR = "/storage/emulated/0/Android/LLS_2025/Xmind/测试/Linux内核文章"
# 2. 输出 html 的根目录
DST_DIR = "/storage/emulated/0/Android/LLS_2025/Xmind/测试/linux内核文章转换"

# 3. 极简手机竖屏 CSS
CSS = """<style>
body{margin:0;padding:1em;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif;line-height:1.6;font-size:15px;color:#222;background:#fff}
h1,h2,h3,h4,h5,h6{margin-top:1.2em;margin-bottom:.6em;font-weight:600}
pre{background:#f6f8fa;padding:.8em;border-radius:4px;overflow-x:auto}
code{background:#f6f8fa;padding:.2em .4em;border-radius:3px;font-size:90%}
blockquote{margin:0;padding-left:1em;border-left:4px solid #dfe2e5;color:#6a737d}
img{max-width:100%;height:auto}
table{border-collapse:collapse;width:100%}
th,td{border:1px solid #ddd;padding:.4em}
th{background:#f6f8fa}
</style>"""

def ensure_dir(p: pathlib.Path):
    p.mkdir(parents=True, exist_ok=True)

def md_to_html(md_text: str) -> str:
    """md -> 完整 html 字符串"""
    md = markdown.Markdown(extensions=["extra", "codehilite", "toc"])
    content = md.convert(md_text)
    title = md.Meta.get("title", [""])[0] if hasattr(md, "Meta") else ""
    if not title:
        # 取第一行#标题或文件名
        title = content.splitlines()[0].replace("<h1>", "").replace("</h1>", "").strip()
    html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{title}</title>
{CSS}
</head>
<body>
{content}
<hr>
<small>Generated by 凉安 @ {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</small>
</body>
</html>"""
    return html

def main():
    src_path = pathlib.Path(SRC_DIR).expanduser()
    dst_path = pathlib.Path(DST_DIR).expanduser()
    ensure_dir(dst_path)

    if not src_path.is_dir():
        print("❌ 源目录不存在:", src_path)
        return

    md_files = list(src_path.rglob("*.md"))
    if not md_files:
        print("⚠️  未找到任何 .md 文件")
        return

    for md_file in md_files:
        rel = md_file.relative_to(src_path).with_suffix(".html")
        out_file = dst_path / rel
        ensure_dir(out_file.parent)

        html = md_to_html(md_file.read_text(encoding="utf-8"))
        out_file.write_text(html, encoding="utf-8")
        print("✅", out_file)

    print(f"🎉 全部完成,共转换 {len(md_files)} 个文件 → {dst_path}")

if __name__ == "__main__":
    main()
相关推荐
清水白石0085 小时前
从“点一下导出”到生产级任务队列:Python 异步导出系统设计全景解析
java·数据库·python
小飞侠是个胖子5 小时前
在 WebGL 中构建高性能 3D 沉浸式系统的三套高阶方案
前端·3d
香蕉鼠片5 小时前
CUDA、PyTorch、Transformers、PEFT 全栈详解
人工智能·pytorch·python
MediaTea5 小时前
PyTorch:张量与基础计算模块
人工智能·pytorch·python·深度学习·机器学习
浪子sunny5 小时前
2026股票实时行情数据Skills技能分享
大数据·人工智能·python
ゆづき5 小时前
假如编程语言们有外号
java·c语言·c++·python·学习·c#·生活
wh_xia_jun5 小时前
Vue3 + Vitest 浏览器测试 从零开发指南
前端·javascript·vue.js
深度学习lover5 小时前
<数据集>yolo 电线杆识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·电线杆识别
FlyWIHTSKY5 小时前
区块链前端技术栈介绍
前端·区块链
唐青枫5 小时前
别再让 key 写成字符串:TypeScript keyof 从入门到实战
前端·javascript·typescript