Spaces:
Runtime error
Runtime error
Download tools/build_guide_pdf.py from BNAmericas/ComparerTool: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/BNAmericas/ComparerTool/resolve/main/tools/build_guide_pdf.py
- Command line
-
hf download hf://spaces/BNAmericas/ComparerTool/tools/build_guide_pdf.py
-
curl -L -o build_guide_pdf.py https://huggingface.co/spaces/BNAmericas/ComparerTool/resolve/main/tools/build_guide_pdf.py
4.09 kB
| """Build GUIDE_FOR_RESEARCHERS.pdf from GUIDE_FOR_RESEARCHERS.md. | |
| Single-source workflow: | |
| edit GUIDE_FOR_RESEARCHERS.md → run this script → PDF refreshed. | |
| Dependencies (auto-installed if missing): | |
| pip install markdown weasyprint | |
| Usage: | |
| python tools/build_guide_pdf.py | |
| """ | |
| from __future__ import annotations | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| HERE = Path(__file__).resolve().parent | |
| ROOT = HERE.parent | |
| MD_PATH = ROOT / "GUIDE_FOR_RESEARCHERS.md" | |
| PDF_PATH = ROOT / "GUIDE_FOR_RESEARCHERS.pdf" | |
| def _ensure(pkg: str, import_name: str | None = None) -> None: | |
| """Pip-install a missing dependency on first run.""" | |
| try: | |
| __import__(import_name or pkg) | |
| except ImportError: | |
| print(f"Installing {pkg}...") | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", pkg]) | |
| _ensure("markdown") | |
| _ensure("weasyprint") | |
| import markdown # noqa: E402 | |
| from weasyprint import HTML, CSS # noqa: E402 | |
| STYLE = """ | |
| @page { size: A4; margin: 1.4cm; } | |
| * { box-sizing: border-box; } | |
| body { | |
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; | |
| color: #111827; | |
| line-height: 1.6; | |
| margin: 0; | |
| font-size: 11pt; | |
| } | |
| .cover { | |
| background: #2541B2; | |
| color: #FFFFFF; | |
| padding: 28px 32px; | |
| border-radius: 10px; | |
| margin: 0 0 22px 0; | |
| } | |
| .cover h1 { margin: 0; font-size: 22pt; font-weight: 600; color: #FFFFFF; } | |
| .cover p { margin: 6px 0 0 0; font-size: 11pt; color: #DBE3F8; } | |
| h2 { | |
| color: #1E3A8A; | |
| font-size: 15pt; | |
| font-weight: 600; | |
| margin: 22px 0 10px 0; | |
| padding: 8px 0 6px 0; | |
| border-bottom: 2px solid #E5E7EB; | |
| } | |
| h3 { color: #1E3A8A; font-size: 13pt; font-weight: 600; margin: 18px 0 8px 0; } | |
| p { margin: 8px 0; } | |
| ul { padding-left: 22px; margin: 8px 0 14px 0; } | |
| li { margin: 4px 0; } | |
| strong { font-weight: 600; color: #111827; } | |
| em { color: #4B5563; font-style: italic; } | |
| code { | |
| font-family: "SFMono-Regular", "Consolas", "Liberation Mono", monospace; | |
| background: #F3F4F6; | |
| color: #1E3A8A; | |
| padding: 1px 6px; | |
| border-radius: 4px; | |
| font-size: 10pt; | |
| } | |
| table { | |
| width: 100%; | |
| border-collapse: collapse; | |
| margin: 12px 0 18px 0; | |
| font-size: 10.5pt; | |
| } | |
| th, td { | |
| text-align: left; | |
| padding: 8px 10px; | |
| border-bottom: 1px solid #E5E7EB; | |
| vertical-align: top; | |
| } | |
| th { | |
| background: #F8FAFC; | |
| color: #1E3A8A; | |
| font-weight: 600; | |
| } | |
| hr { border: 0; border-top: 1px solid #E5E7EB; margin: 18px 0; } | |
| .footer-note { | |
| margin-top: 24px; | |
| padding-top: 12px; | |
| border-top: 1px solid #E5E7EB; | |
| color: #6B7280; | |
| font-size: 9.5pt; | |
| text-align: center; | |
| font-style: italic; | |
| } | |
| """ | |
| def build() -> Path: | |
| if not MD_PATH.exists(): | |
| raise SystemExit(f"Could not find {MD_PATH}") | |
| raw = MD_PATH.read_text(encoding="utf-8") | |
| body = markdown.markdown(raw, extensions=["tables", "fenced_code"]) | |
| # Pull the first H1 + the following paragraph into the styled cover banner. | |
| lines = body.splitlines() | |
| title_html = "Comparer Tool — Quick Guide" | |
| subtitle_html = "Everything you need for your first batch (and the next hundred)." | |
| cleaned = [] | |
| pulled_title = False | |
| pulled_subtitle = False | |
| for line in lines: | |
| if not pulled_title and line.startswith("<h1>") and line.endswith("</h1>"): | |
| title_html = line[len("<h1>"):-len("</h1>")] | |
| pulled_title = True | |
| continue | |
| if pulled_title and not pulled_subtitle and line.startswith("<p>") and line.endswith("</p>"): | |
| subtitle_html = line[len("<p>"):-len("</p>")] | |
| pulled_subtitle = True | |
| continue | |
| cleaned.append(line) | |
| body = "\n".join(cleaned) | |
| html = f"""<!doctype html> | |
| <html><head><meta charset="utf-8"></head> | |
| <body> | |
| <div class="cover"> | |
| <h1>{title_html}</h1> | |
| <p>{subtitle_html}</p> | |
| </div> | |
| {body} | |
| <div class="footer-note">BNAmericas internal tool · generated from GUIDE_FOR_RESEARCHERS.md</div> | |
| </body></html>""" | |
| HTML(string=html).write_pdf(str(PDF_PATH), stylesheets=[CSS(string=STYLE)]) | |
| print(f"Wrote {PDF_PATH}") | |
| return PDF_PATH | |
| if __name__ == "__main__": | |
| build() | |