Spaces:
Paused
Paused
Download biquge-api/scripts/render_docs.py from tanlang002/tupload07: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/spaces/tanlang002/tupload07/resolve/main/biquge-api/scripts/render_docs.py
- Command line
-
hf download hf://spaces/tanlang002/tupload07/biquge-api/scripts/render_docs.py
-
curl -L -o render_docs.py https://huggingface.co/spaces/tanlang002/tupload07/resolve/main/biquge-api/scripts/render_docs.py
4.41 kB
| """把 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() | |