Spaces:
Paused
Paused
File size: 4,408 Bytes
6d2d8b6 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """把 docs/*.md 渲染为独立可打开的 HTML 文档(GitHub 风格 + Mermaid 支持)。
用法:
python scripts/render_docs.py
"""
from __future__ import annotations
import re
from pathlib import Path
import markdown
from pygments.formatters import HtmlFormatter
ROOT = Path(__file__).resolve().parent.parent
DOCS_DIR = ROOT / "docs"
OUT_DIR = ROOT / "docs_html"
CSS = """
:root { color-scheme: light; }
* { box-sizing: border-box; }
body {
margin: 0; padding: 32px 16px 80px;
background: #f6f8fa; color: #1f2328;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
"Microsoft YaHei", Helvetica, Arial, sans-serif;
line-height: 1.7;
}
.container { max-width: 900px; margin: 0 auto; }
.markdown-body {
background: #ffffff; border: 1px solid #d0d7de; border-radius: 8px;
padding: 32px 40px;
}
.markdown-body h1, .markdown-body h2 {
border-bottom: 1px solid #d8dee4; padding-bottom: .3em; margin-top: 1.6em;
}
.markdown-body h1:first-child { margin-top: 0; }
.markdown-body h1 { font-size: 1.9em; }
.markdown-body h2 { font-size: 1.45em; }
.markdown-body h3 { font-size: 1.15em; }
.markdown-body code {
background: rgba(175,184,193,.2); border-radius: 6px;
padding: .15em .4em; font-family: "Cascadia Code", Consolas, monospace; font-size: .9em;
}
.markdown-body pre {
background: #f6f8fa; border: 1px solid #d8dee4; border-radius: 8px;
padding: 14px 16px; overflow: auto;
}
.markdown-body pre code { background: none; padding: 0; }
.markdown-body table {
border-collapse: collapse; width: 100%; margin: 1em 0; display: block; overflow-x: auto;
}
.markdown-body th, .markdown-body td {
border: 1px solid #d0d7de; padding: 8px 12px; text-align: left; white-space: nowrap;
}
.markdown-body th { background: #f6f8fa; font-weight: 600; }
.markdown-body blockquote {
border-left: 4px solid #d0d7de; margin: 1em 0; padding: 2px 16px; color: #57606a;
background: #f6f8fa; border-radius: 0 6px 6px 0;
}
.markdown-body a { color: #0969da; text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.toc { background: #f6f8fa; border: 1px solid #d8dee4; border-radius: 8px; padding: 16px 20px; }
.toc ul { list-style: none; padding-left: 1em; margin: .25em 0; }
footer { text-align: center; color: #57606a; margin-top: 24px; font-size: .85em; }
"""
def render(md_path: Path, out_path: Path) -> None:
text = md_path.read_text(encoding="utf-8")
body = markdown.markdown(
text,
extensions=[
"tables",
"fenced_code",
"toc",
"sane_lists",
"attr_list",
"codehilite",
],
extension_configs={
"codehilite": {"guess_lang": False},
"toc": {"permalink": False, "title": "目录"},
},
)
highlight_css = HtmlFormatter().get_style_defs(".codehilite")
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{md_path.stem} - 笔趣阁爬虫项目文档</title>
<style>{CSS}</style>
<style>{highlight_css}</style>
</head>
<body>
<div class="container">
<div class="markdown-body">
{body}
</div>
<footer>笔趣阁爬虫项目文档 · 由 Markdown 自动渲染</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
<script>mermaid.initialize({{ startOnLoad: true, theme: "default" }});</script>
</body>
</html>
"""
out_path.write_text(html, encoding="utf-8")
print(f"渲染完成: {out_path}")
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
for md_path in sorted(DOCS_DIR.glob("*.md")):
out_name = re.sub(r"[^\w\-.]+", "_", md_path.stem) + ".html"
render(md_path, OUT_DIR / out_name)
index = OUT_DIR / "index.html"
index.write_text(
f"""<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="utf-8"><title>笔趣阁爬虫项目文档</title>
<style>{CSS}</style></head><body><div class="container"><div class="markdown-body">
<h1>笔趣阁爬虫项目文档</h1>
<ul>
<li><a href="爬虫技术文档.html">爬虫技术文档(架构与实现思路)</a></li>
<li><a href="API接口文档.html">API 接口文档</a></li>
</ul>
</div></div></body></html>
""",
encoding="utf-8",
)
print(f"渲染完成: {index}")
if __name__ == "__main__":
main()
|