-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfix_code_blocks.py
More file actions
46 lines (35 loc) · 1.39 KB
/
fix_code_blocks.py
File metadata and controls
46 lines (35 loc) · 1.39 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
import os
import re
import glob
def fix_code_blocks(file_path):
print(f'Processing: {file_path}')
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Count existing python blocks without title
python_blocks = re.findall(r'```python(?!\s+title=)', content)
if not python_blocks:
return False
print(f' Found {len(python_blocks)} python blocks without title')
# Get the filename for the title
filename = os.path.basename(file_path).replace('.mdx', '.py')
# Replace python blocks without title
# Pattern to match ```python without title= following it
pattern = r'```python(?!\s+title=)'
replacement = f'```python title="{filename}" showLineNumbers{{1}}'
new_content = re.sub(pattern, replacement, content)
# Check if any changes were made
if new_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
# Find all mdx files in projects folder
project_files = glob.glob('src/content/docs/projects/**/*.mdx', recursive=True)
print(f'Found {len(project_files)} MDX files')
updated_files = []
for file_path in project_files:
if fix_code_blocks(file_path):
updated_files.append(file_path)
print(f'\nUpdated {len(updated_files)} files:')
for file in updated_files:
print(f' ✓ {file}')