|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Add downloads configuration to frontmatter of all notebook markdown files. |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def add_downloads_to_frontmatter(file_path): |
| 10 | + """Add downloads configuration to a markdown file's frontmatter.""" |
| 11 | + with open(file_path, 'r') as f: |
| 12 | + content = f.read() |
| 13 | + |
| 14 | + # Extract the frontmatter |
| 15 | + frontmatter_match = re.match(r'^---\n(.*?)\n---\n(.*)$', content, re.DOTALL) |
| 16 | + if not frontmatter_match: |
| 17 | + print(f"⚠️ No frontmatter found in {file_path}") |
| 18 | + return False |
| 19 | + |
| 20 | + frontmatter = frontmatter_match.group(1) |
| 21 | + body = frontmatter_match.group(2) |
| 22 | + |
| 23 | + # Check if downloads already exists |
| 24 | + if 'downloads:' in frontmatter: |
| 25 | + print(f"ℹ️ Downloads already configured in {file_path}") |
| 26 | + return False |
| 27 | + |
| 28 | + # Generate the ipynb filename |
| 29 | + stem = file_path.stem |
| 30 | + ipynb_file = f"notebooks/{stem}.ipynb" |
| 31 | + |
| 32 | + # Add downloads configuration |
| 33 | + downloads_config = f"\ndownloads:\n - file: {ipynb_file}\n" |
| 34 | + new_frontmatter = frontmatter + downloads_config |
| 35 | + |
| 36 | + # Write back |
| 37 | + new_content = f"---\n{new_frontmatter}---\n{body}" |
| 38 | + with open(file_path, 'w') as f: |
| 39 | + f.write(new_content) |
| 40 | + |
| 41 | + print(f"✓ Added downloads to {file_path}") |
| 42 | + return True |
| 43 | + |
| 44 | +def main(): |
| 45 | + """Add downloads configuration to all notebook files.""" |
| 46 | + print("=" * 60) |
| 47 | + print("Adding Downloads Configuration to Frontmatter") |
| 48 | + print("=" * 60) |
| 49 | + |
| 50 | + # Get all markdown notebook files |
| 51 | + files = [] |
| 52 | + |
| 53 | + # Chapter files |
| 54 | + for i in range(1, 22): |
| 55 | + file = Path(f"chapter_{i:02d}.md") |
| 56 | + if file.exists(): |
| 57 | + files.append(file) |
| 58 | + |
| 59 | + # Lab files |
| 60 | + for lab_file in ["chapter_04_lab.md", "chapter_05_lab.md"]: |
| 61 | + file = Path(lab_file) |
| 62 | + if file.exists(): |
| 63 | + files.append(file) |
| 64 | + |
| 65 | + # Exercise files |
| 66 | + for exercise_file in ["chapter_04_exercises_a.md", "chapter_04_exercises_b.md"]: |
| 67 | + file = Path(exercise_file) |
| 68 | + if file.exists(): |
| 69 | + files.append(file) |
| 70 | + |
| 71 | + # Appendix files |
| 72 | + for letter in ['a', 'b', 'c', 'd', 'e']: |
| 73 | + file = Path(f"appendix_{letter}.md") |
| 74 | + if file.exists(): |
| 75 | + files.append(file) |
| 76 | + |
| 77 | + print(f"\nProcessing {len(files)} files...\n") |
| 78 | + |
| 79 | + updated = 0 |
| 80 | + for file in files: |
| 81 | + if add_downloads_to_frontmatter(file): |
| 82 | + updated += 1 |
| 83 | + |
| 84 | + print(f"\n✓ Updated {updated} files") |
| 85 | + print("=" * 60) |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments