-
Notifications
You must be signed in to change notification settings - Fork 5
/
migrate.py
executable file
·76 lines (55 loc) · 2.77 KB
/
migrate.py
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
import os
import re
def rename_to_mdx(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
new_file_path = os.path.join(root, file.replace('.md', '.mdx'))
os.rename(file_path, new_file_path)
def update_frontmatter(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.mdx'):
file_path = os.path.join(root, file)
with open(file_path, 'r') as f:
content = f.read()
pattern = r'^---\n(.*?)\n---\n'
match = re.search(pattern, content, re.DOTALL)
if match:
frontmatter = match.group(1)
new_frontmatter = 'layout: src/layouts/'
if '/builds/' in root:
new_frontmatter += 'Build'
new_frontmatter = frontmatter + f'\n{new_frontmatter}.astro'
elif '/guides/' in root:
new_frontmatter += 'Guide'
new_frontmatter = frontmatter + f'\n{new_frontmatter}.astro'
elif '/fractals/' in root:
new_frontmatter = frontmatter
new_content = content.replace(frontmatter, new_frontmatter).replace("\"legends\"", "\"skills\":[],\"legends\"")
with open(file_path, 'w') as f:
f.write(new_content)
def update_images(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.mdx'):
file_path = os.path.join(root, file)
with open(file_path, 'r') as f:
content = f.read()
pattern = r'!\[(.*?)\]\((.*?)\)'
matches = re.findall(pattern, content)
for match in matches:
original_path = match[1]
original_caption = match[0]
new_path = f'{os.path.dirname(original_path)}/{os.path.basename(original_path).split(".")[0]}.astro'
new_content = f'<Image src={{import("./{original_path}")}} caption="{original_caption}" />'
content = content.replace(f'![{original_caption}]({original_path})', new_content)
with open(file_path, 'w') as f:
f.write(content)
if os.path.isfile(original_path):
os.rename(original_path, new_path)
if __name__ == '__main__':
rename_to_mdx('discretize-guides/')
update_frontmatter('discretize-guides/')
update_images('discretize-guides/')