|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import re |
| 4 | +import shutil |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from jinja2 import Environment, FileSystemLoader |
| 8 | +import markdown |
| 9 | + |
| 10 | +from preprocessor import split_slides, process_slide, rewrite_image_paths |
| 11 | + |
| 12 | + |
| 13 | +def build_site(output_dir: str = "_site"): |
| 14 | + """Build the entire static site.""" |
| 15 | + root = Path(__file__).parent |
| 16 | + out = Path(output_dir) |
| 17 | + |
| 18 | + # Clean output |
| 19 | + if out.exists(): |
| 20 | + shutil.rmtree(out) |
| 21 | + out.mkdir(parents=True) |
| 22 | + |
| 23 | + # Set up Jinja2 — autoescape=False since templates use {{ content }} |
| 24 | + # with pre-rendered HTML and this is a static site generator, not a web app |
| 25 | + env = Environment(loader=FileSystemLoader(root / "templates"), autoescape=False) |
| 26 | + |
| 27 | + # Copy static assets |
| 28 | + shutil.copytree(root / "static" / "css", out / "css") |
| 29 | + shutil.copytree(root / "static" / "js", out / "js") |
| 30 | + shutil.copytree(root / "examples", out / "examples") |
| 31 | + shutil.copytree(root / "lecture_notes" / "images", out / "lecture_notes" / "images") |
| 32 | + |
| 33 | + # Find week files (exclude _old files) |
| 34 | + week_files = sorted(glob.glob(str(root / "lecture_notes" / "week_*.md"))) |
| 35 | + week_files = [f for f in week_files if "_old" not in f] |
| 36 | + |
| 37 | + weeks = [] |
| 38 | + |
| 39 | + for week_file in week_files: |
| 40 | + week_num = int(re.search(r"week_(\d+)", week_file).group(1)) |
| 41 | + weeks.append(week_num) |
| 42 | + |
| 43 | + md_content = Path(week_file).read_text() |
| 44 | + slide_strings = split_slides(md_content) |
| 45 | + |
| 46 | + total_slides = len(slide_strings) |
| 47 | + week_dir = out / "lecture_notes" / f"week_{week_num}" |
| 48 | + |
| 49 | + for i, slide_md in enumerate(slide_strings, 1): |
| 50 | + html_content, classes = process_slide(slide_md) |
| 51 | + html_content = rewrite_image_paths(html_content) |
| 52 | + |
| 53 | + prev_url = f"/lecture_notes/week_{week_num}/{i - 1}/" if i > 1 else "" |
| 54 | + next_url = f"/lecture_notes/week_{week_num}/{i + 1}/" if i < total_slides else "" |
| 55 | + first_url = f"/lecture_notes/week_{week_num}/1/" |
| 56 | + |
| 57 | + slide_html = env.get_template("slide.html").render( |
| 58 | + content=html_content, |
| 59 | + classes=classes, |
| 60 | + week=week_num, |
| 61 | + slide_num=i, |
| 62 | + total_slides=total_slides, |
| 63 | + prev_url=prev_url, |
| 64 | + next_url=next_url, |
| 65 | + first_url=first_url, |
| 66 | + ) |
| 67 | + |
| 68 | + slide_dir = week_dir / str(i) |
| 69 | + slide_dir.mkdir(parents=True, exist_ok=True) |
| 70 | + (slide_dir / "index.html").write_text(slide_html) |
| 71 | + |
| 72 | + # Lecture notes index |
| 73 | + lecture_index_html = env.get_template("lecture_index.html").render(weeks=weeks) |
| 74 | + lectures_dir = out / "lecture_notes" |
| 75 | + lectures_dir.mkdir(parents=True, exist_ok=True) |
| 76 | + (lectures_dir / "index.html").write_text(lecture_index_html) |
| 77 | + |
| 78 | + # Syllabus |
| 79 | + syllabus_md = (root / "syllabus.md").read_text() |
| 80 | + md_renderer = markdown.Markdown( |
| 81 | + extensions=["fenced_code", "codehilite", "tables"], |
| 82 | + extension_configs={ |
| 83 | + "codehilite": {"css_class": "highlight", "guess_lang": False} |
| 84 | + }, |
| 85 | + ) |
| 86 | + syllabus_html = md_renderer.convert(syllabus_md) |
| 87 | + page_html = env.get_template("page.html").render( |
| 88 | + title="Syllabus", content=syllabus_html |
| 89 | + ) |
| 90 | + syllabus_dir = out / "syllabus" |
| 91 | + syllabus_dir.mkdir(parents=True, exist_ok=True) |
| 92 | + (syllabus_dir / "index.html").write_text(page_html) |
| 93 | + |
| 94 | + # Homepage |
| 95 | + homepage_html = env.get_template("index.html").render() |
| 96 | + (out / "index.html").write_text(homepage_html) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + build_site() |
0 commit comments