Repository navigation
Expand file tree
/
Copy pathbundle.py
More file actions
83 lines (75 loc) · 3.42 KB
/
Copy pathbundle.py
File metadata and controls
83 lines (75 loc) · 3.42 KB
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
#!/usr/bin/env python3
"""Saare chapters ko ek self-contained HTML file mein jodo (print/PDF ke liye bhi)."""
import os, re
from toc import PARTS, CHAPTERS
from build import num, CSS, FONTS
HERE = os.path.dirname(os.path.abspath(__file__))
SRC, DIST = os.path.join(HERE, "src"), os.path.join(HERE, "dist")
PRINT_CSS = """
/* bundle: ek lambi file, har chapter naye page par */
nav.rail, .topbar, .pager { display: none !important; }
.cols { display: block !important; }
main { max-width: 74ch; margin: 0 auto; padding-block: 0 0; }
.chapter { padding-block: 48px 8px; border-top: 2px solid var(--rule); }
.chapter:first-of-type { border-top: none; }
.bk-toc { padding-block: 24px 48px; }
.bk-toc ol { font-family: var(--font-ui); font-size: 14px; columns: 2; column-gap: 40px; }
.bk-toc li { break-inside: avoid; margin: .22em 0; }
.bk-toc a { text-decoration: none; }
.bk-toc .pt { font-family: var(--font-head); font-weight: 600; font-size: 15px;
margin: 14px 0 4px; column-span: all; color: var(--marker); }
@media print {
:root { --paper: #fff; --card: #fff; }
body { font-size: 10.5pt; }
.chapter { break-before: page; padding-block: 0 0; border-top: none; }
.chapter:first-of-type { break-before: auto; }
details { break-inside: avoid; }
details > summary { font-weight: 600; }
details[open] > summary { margin-bottom: .4em; }
figure, .box, .tw { break-inside: avoid; }
h2 { break-after: avoid; }
a { color: inherit; text-decoration: none; }
}
"""
def main():
parts = []
parts.append('<!doctype html><html lang="en"><head><meta charset="utf-8">'
'<meta name="viewport" content="width=device-width,initial-scale=1">')
parts.append("<title>FastAPI: Basics to Production</title>")
parts.append(FONTS)
parts.append(f"<style>{CSS}{PRINT_CSS}</style>")
parts.append('</head><body><div class="wrap"><main>')
# cover
parts.append(
'<div class="cover"><div class="kicker">A working engineer’s textbook</div>'
"<h1>FastAPI: Basics to Production</h1>"
f"<p>Abhishek Verma · {len(CHAPTERS)} chapters · updated 9 November 2026</p>"
"<p>Har topic ki clear definition, asli code, aur diagram jo mechanism dikhate hain. "
"Har chapter mein ek tool atlas, industry applications, worked interview problems, "
"common mistakes, practice aur ek Hindi recap.</p></div>")
# contents
toc = ['<div class="bk-toc"><ol style="list-style:none;padding:0">']
for tag, name, _sub, chs in PARTS:
toc.append(f'<li class="pt">{tag}. {name}</li>')
for n, t, _d in chs:
if os.path.exists(os.path.join(SRC, f"ch{n}.html")):
toc.append(f'<li><a href="#ch{n}">{num(n)}. {t}</a></li>')
toc.append("</ol></div>")
parts.append("".join(toc))
# chapters
count = 0
for n, t, _d in CHAPTERS:
p = os.path.join(SRC, f"ch{n}.html")
if not os.path.exists(p):
continue
body = open(p).read()
# anchor ids are unique per chapter, details open for print
body = body.replace("<details>", "<details open>")
parts.append(f'<section class="chapter" id="ch{n}">{body}</section>')
count += 1
parts.append("</main></div></body></html>")
out = os.path.join(DIST, "book.html")
open(out, "w").write("".join(parts))
print(f"bundled {count} chapters -> {out} ({os.path.getsize(out)/1024:.0f} KB)")
if __name__ == "__main__":
main()