-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadd_download_frontmatter.py
More file actions
88 lines (69 loc) · 2.38 KB
/
add_download_frontmatter.py
File metadata and controls
88 lines (69 loc) · 2.38 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
84
85
86
87
88
#!/usr/bin/env python3
"""
Add downloads configuration to frontmatter of all notebook markdown files.
"""
import re
from pathlib import Path
def add_downloads_to_frontmatter(file_path):
"""Add downloads configuration to a markdown file's frontmatter."""
with open(file_path, 'r') as f:
content = f.read()
# Extract the frontmatter
frontmatter_match = re.match(r'^---\n(.*?)\n---\n(.*)$', content, re.DOTALL)
if not frontmatter_match:
print(f"⚠️ No frontmatter found in {file_path}")
return False
frontmatter = frontmatter_match.group(1)
body = frontmatter_match.group(2)
# Check if downloads already exists
if 'downloads:' in frontmatter:
print(f"ℹ️ Downloads already configured in {file_path}")
return False
# Generate the ipynb filename
stem = file_path.stem
ipynb_file = f"notebooks/{stem}.ipynb"
# Add downloads configuration
downloads_config = f"\ndownloads:\n - file: {ipynb_file}\n"
new_frontmatter = frontmatter + downloads_config
# Write back
new_content = f"---\n{new_frontmatter}---\n{body}"
with open(file_path, 'w') as f:
f.write(new_content)
print(f"✓ Added downloads to {file_path}")
return True
def main():
"""Add downloads configuration to all notebook files."""
print("=" * 60)
print("Adding Downloads Configuration to Frontmatter")
print("=" * 60)
# Get all markdown notebook files
files = []
# Chapter files
for i in range(1, 22):
file = Path(f"chapter_{i:02d}.md")
if file.exists():
files.append(file)
# Lab files
for lab_file in ["chapter_04_lab.md", "chapter_05_lab.md"]:
file = Path(lab_file)
if file.exists():
files.append(file)
# Exercise files
for exercise_file in ["chapter_04_exercises_a.md", "chapter_04_exercises_b.md"]:
file = Path(exercise_file)
if file.exists():
files.append(file)
# Appendix files
for letter in ['a', 'b', 'c', 'd', 'e']:
file = Path(f"appendix_{letter}.md")
if file.exists():
files.append(file)
print(f"\nProcessing {len(files)} files...\n")
updated = 0
for file in files:
if add_downloads_to_frontmatter(file):
updated += 1
print(f"\n✓ Updated {updated} files")
print("=" * 60)
if __name__ == "__main__":
main()