脚本工具 批量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()
相关推荐
-凌凌漆-18 分钟前
【vue】选项式api与组合式api
前端·javascript·vue.js
派葛穆19 分钟前
Python-PyQt5 安装与配置教程
开发语言·python·qt
自可乐29 分钟前
Milvus向量数据库/RAG基础设施学习教程
数据库·人工智能·python·milvus
可触的未来,发芽的智生43 分钟前
发现:认知的普适节律 发现思维的8次迭代量子
javascript·python·神经网络·程序人生·自然语言处理
真智AI1 小时前
用 LLM 辅助生成可跑的 Python 单元测试:pytest + coverage 覆盖率报告(含运行指令与排坑)
python·单元测试·pytest
0思必得02 小时前
[Web自动化] Selenium处理文件上传和下载
前端·爬虫·python·selenium·自动化·web自动化
Hui Baby2 小时前
Java SPI 与 Spring SPI
java·python·spring
小猪咪piggy2 小时前
【Python】(3) 函数
开发语言·python
夜鸣笙笙2 小时前
交换最小值和最大值
python
2301_822363602 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python