Add a script for generating a static site
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -174,3 +174,6 @@ cython_debug/
|
|||||||
.pypirc
|
.pypirc
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# Generated build output
|
||||||
|
public/
|
||||||
|
|||||||
267
build.py
Normal file
267
build.py
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Build script: scans shared/, generates zip downloads and public/index.html."""
|
||||||
|
|
||||||
|
import html
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import zipfile
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SHARED_DIR = Path("shared")
|
||||||
|
PUBLIC_DIR = Path("public")
|
||||||
|
ZIPS_DIR = PUBLIC_DIR / "zips"
|
||||||
|
|
||||||
|
EXCLUDE_NAMES = {".DS_Store", "__pycache__", ".git", "Thumbs.db"}
|
||||||
|
EXCLUDE_SUFFIXES = {".pyc"}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SubSection:
|
||||||
|
name: str
|
||||||
|
zip_url: str
|
||||||
|
files: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Section:
|
||||||
|
title: str
|
||||||
|
subsections: list[SubSection] = field(default_factory=list)
|
||||||
|
zip_url: str | None = None
|
||||||
|
files: list[str] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# --- File filtering ---
|
||||||
|
|
||||||
|
def should_include(path: Path) -> bool:
|
||||||
|
return path.name not in EXCLUDE_NAMES and path.suffix not in EXCLUDE_SUFFIXES
|
||||||
|
|
||||||
|
|
||||||
|
# --- Zip and file listing ---
|
||||||
|
|
||||||
|
def create_zip(source_dir: Path, zip_path: Path) -> None:
|
||||||
|
"""Zip source_dir into zip_path; contents extract under a named folder."""
|
||||||
|
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||||
|
for file_path in sorted(source_dir.rglob("*")):
|
||||||
|
if not file_path.is_file():
|
||||||
|
continue
|
||||||
|
relative = file_path.relative_to(source_dir.parent)
|
||||||
|
if any(part in EXCLUDE_NAMES for part in relative.parts):
|
||||||
|
continue
|
||||||
|
if not should_include(file_path):
|
||||||
|
continue
|
||||||
|
zf.write(file_path, arcname=relative)
|
||||||
|
|
||||||
|
|
||||||
|
def list_files(source_dir: Path) -> list[str]:
|
||||||
|
"""Return sorted list of filtered relative file paths for display."""
|
||||||
|
results = []
|
||||||
|
for file_path in sorted(source_dir.rglob("*")):
|
||||||
|
if not file_path.is_file():
|
||||||
|
continue
|
||||||
|
relative = file_path.relative_to(source_dir)
|
||||||
|
if any(part in EXCLUDE_NAMES for part in relative.parts):
|
||||||
|
continue
|
||||||
|
if not should_include(file_path):
|
||||||
|
continue
|
||||||
|
results.append(str(relative))
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
# --- Data model construction ---
|
||||||
|
|
||||||
|
def scan_shared() -> list[Section]:
|
||||||
|
sections = []
|
||||||
|
|
||||||
|
assignments_dir = SHARED_DIR / "assignments"
|
||||||
|
if assignments_dir.exists():
|
||||||
|
zip_name = "assignments.zip"
|
||||||
|
create_zip(assignments_dir, ZIPS_DIR / zip_name)
|
||||||
|
sections.append(Section(
|
||||||
|
title="Assignments",
|
||||||
|
zip_url=f"zips/{zip_name}",
|
||||||
|
files=list_files(assignments_dir),
|
||||||
|
))
|
||||||
|
|
||||||
|
notes_dir = SHARED_DIR / "notes-and-examples"
|
||||||
|
if notes_dir.exists():
|
||||||
|
date_dirs = sorted(
|
||||||
|
d for d in notes_dir.iterdir()
|
||||||
|
if d.is_dir() and should_include(d)
|
||||||
|
)
|
||||||
|
subsections = []
|
||||||
|
for date_dir in date_dirs:
|
||||||
|
zip_name = f"notes-and-examples-{date_dir.name}.zip"
|
||||||
|
create_zip(date_dir, ZIPS_DIR / zip_name)
|
||||||
|
subsections.append(SubSection(
|
||||||
|
name=date_dir.name,
|
||||||
|
zip_url=f"zips/{zip_name}",
|
||||||
|
files=list_files(date_dir),
|
||||||
|
))
|
||||||
|
sections.append(Section(title="Notes and Examples", subsections=subsections))
|
||||||
|
|
||||||
|
return sections
|
||||||
|
|
||||||
|
|
||||||
|
# --- HTML rendering ---
|
||||||
|
|
||||||
|
HTML_TEMPLATE = """\
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>DSA Tutoring</title>
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after {{ box-sizing: border-box; }}
|
||||||
|
body {{
|
||||||
|
font-family: system-ui, sans-serif;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 2rem 1rem;
|
||||||
|
color: #1a1a1a;
|
||||||
|
background: #f5f5f5;
|
||||||
|
}}
|
||||||
|
h1 {{ font-size: 1.75rem; margin-bottom: 0.25rem; }}
|
||||||
|
.subtitle {{ color: #666; margin-bottom: 2rem; font-size: 0.95rem; }}
|
||||||
|
.section {{
|
||||||
|
background: white;
|
||||||
|
border: 1px solid #e0e0e0;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 1.5rem;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}}
|
||||||
|
.section > h2 {{
|
||||||
|
margin: 0 0 1rem 0;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
padding-bottom: 0.75rem;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}}
|
||||||
|
.section-flat {{
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}}
|
||||||
|
.subsection {{
|
||||||
|
padding-top: 0.75rem;
|
||||||
|
border-top: 1px solid #f0f0f0;
|
||||||
|
}}
|
||||||
|
.subsection:first-child {{ border-top: none; padding-top: 0; }}
|
||||||
|
.subsection-header {{
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}}
|
||||||
|
.subsection-header h3 {{
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-family: monospace;
|
||||||
|
color: #333;
|
||||||
|
}}
|
||||||
|
.download-btn {{
|
||||||
|
display: inline-block;
|
||||||
|
padding: 0.35rem 0.85rem;
|
||||||
|
background: #2563eb;
|
||||||
|
color: white;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
white-space: nowrap;
|
||||||
|
}}
|
||||||
|
.download-btn:hover {{ background: #1d4ed8; }}
|
||||||
|
.file-list {{
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
}}
|
||||||
|
.file-list li {{
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 0.82rem;
|
||||||
|
color: #555;
|
||||||
|
padding: 0.15rem 0;
|
||||||
|
}}
|
||||||
|
.file-list li::before {{ content: "— "; color: #bbb; }}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>DSA Tutoring</h1>
|
||||||
|
<p class="subtitle">Lesson files and assignments. Click a download button to get a .zip archive.</p>
|
||||||
|
{body}
|
||||||
|
</body>
|
||||||
|
</html>"""
|
||||||
|
|
||||||
|
|
||||||
|
def render_file_list(files: list[str]) -> str:
|
||||||
|
items = "".join(f" <li>{html.escape(f)}</li>\n" for f in files)
|
||||||
|
return f" <ul class=\"file-list\">\n{items} </ul>"
|
||||||
|
|
||||||
|
|
||||||
|
def render_download_btn(zip_url: str, label: str) -> str:
|
||||||
|
return (
|
||||||
|
f"<a class=\"download-btn\" href=\"{html.escape(zip_url)}\" download>"
|
||||||
|
f"Download {html.escape(label)} (.zip)</a>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def render_section(section: Section) -> str:
|
||||||
|
if section.subsections:
|
||||||
|
parts = []
|
||||||
|
for sub in section.subsections:
|
||||||
|
parts.append(
|
||||||
|
f" <div class=\"subsection\">\n"
|
||||||
|
f" <div class=\"subsection-header\">\n"
|
||||||
|
f" <h3>{html.escape(sub.name)}</h3>\n"
|
||||||
|
f" {render_download_btn(sub.zip_url, sub.name)}\n"
|
||||||
|
f" </div>\n"
|
||||||
|
f"{render_file_list(sub.files)}\n"
|
||||||
|
f" </div>"
|
||||||
|
)
|
||||||
|
inner = "\n".join(parts)
|
||||||
|
else:
|
||||||
|
inner = (
|
||||||
|
f" <div class=\"section-flat\">\n"
|
||||||
|
f" {render_download_btn(section.zip_url, section.title)}\n"
|
||||||
|
f"{render_file_list(section.files or [])}\n"
|
||||||
|
f" </div>"
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
f" <section class=\"section\">\n"
|
||||||
|
f" <h2>{html.escape(section.title)}</h2>\n"
|
||||||
|
f"{inner}\n"
|
||||||
|
f" </section>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def render_html(sections: list[Section]) -> str:
|
||||||
|
body = "\n".join(render_section(s) for s in sections)
|
||||||
|
return HTML_TEMPLATE.format(body=body)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Entry point ---
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
os.chdir(Path(__file__).parent)
|
||||||
|
|
||||||
|
if PUBLIC_DIR.exists():
|
||||||
|
shutil.rmtree(PUBLIC_DIR)
|
||||||
|
PUBLIC_DIR.mkdir()
|
||||||
|
ZIPS_DIR.mkdir()
|
||||||
|
|
||||||
|
sections = scan_shared()
|
||||||
|
html_content = render_html(sections)
|
||||||
|
|
||||||
|
output = PUBLIC_DIR / "index.html"
|
||||||
|
output.write_text(html_content, encoding="utf-8")
|
||||||
|
print(f"Built {output}")
|
||||||
|
|
||||||
|
print(f"Zips in {ZIPS_DIR}:")
|
||||||
|
for z in sorted(ZIPS_DIR.iterdir()):
|
||||||
|
print(f" {z.name}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user