-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorganize_pages.py
More file actions
133 lines (104 loc) · 4.11 KB
/
reorganize_pages.py
File metadata and controls
133 lines (104 loc) · 4.11 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Reorganize mdx_pages to match the nav structure.
Also handles moving corresponding uploads from the uploads directory.
"""
import os
import re
import shutil
MDX_DIR = "mdx_pages"
uploads_DIR = os.path.join(MDX_DIR, "uploads")
# Define moves: (source, destination)
# Paths are relative to MDX_DIR
MOVES = [
# === ABOUT ===
# what-is-reactome -> about/what-is-reactome
("what-is-reactome", "about/what-is-reactome"),
# sab -> about/sab
("sab", "about/sab"),
# license -> about/license
("license", "about/license"),
# === CONTENT ===
# orcid -> content/orcid
("orcid", "content/orcid"),
# covid-19 -> content/covid-19
("covid-19", "content/covid-19"),
# === DOCUMENTATION ===
# userguide -> documentation/userguide
("userguide", "documentation/userguide"),
# dev -> documentation/dev
("dev", "documentation/dev"),
# icon-info -> documentation/icon-info
("icon-info", "documentation/icon-info"),
# linking-to-us -> documentation/linking-to-us
("linking-to-us", "documentation/linking-to-us"),
# cite -> documentation/cite
("cite", "documentation/cite"),
# user/guide -> documentation/userguide (merge with existing after move)
("user", "documentation/user"),
]
def move_uploads(src_rel, dst_rel):
"""Move uploads from old location to new location to match page reorganization."""
src_uploads = os.path.join(uploads_DIR, src_rel)
dst_uploads = os.path.join(uploads_DIR, dst_rel)
if not os.path.exists(src_uploads):
return False
if os.path.exists(dst_uploads):
print(f"Skip uploads (destination exists): {dst_uploads}")
return False
# Create parent directory if needed
os.makedirs(os.path.dirname(dst_uploads), exist_ok=True)
# Move the uploads directory
shutil.move(src_uploads, dst_uploads)
print(f"Moved uploads: {src_uploads} -> {dst_uploads}")
return True
def update_image_paths_in_mdx(dst_path, src_rel, dst_rel):
"""
Update image paths in MDX files after reorganization.
Image paths change from /uploads/<src_rel>/... to /uploads/<dst_rel>/...
"""
if not os.path.exists(dst_path):
return
# Process all MDX files in the destination
for root, dirs, files in os.walk(dst_path):
for filename in files:
if not filename.endswith('.mdx'):
continue
filepath = os.path.join(root, filename)
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Replace image paths: /uploads/<src_rel>/ -> /uploads/<dst_rel>/
old_path = f'/uploads/{src_rel}/'
new_path = f'/uploads/{dst_rel}/'
if old_path in content:
updated_content = content.replace(old_path, new_path)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(updated_content)
print(f"Updated image paths in: {filepath}")
except Exception as e:
print(f"Error updating {filepath}: {e}")
def main():
for src_rel, dst_rel in MOVES:
src = os.path.join(MDX_DIR, src_rel)
dst = os.path.join(MDX_DIR, dst_rel)
if not os.path.exists(src):
print(f"Skip (not found): {src}")
# Still try to move uploads even if pages don't exist
move_uploads(src_rel, dst_rel)
continue
if os.path.exists(dst):
print(f"Skip (destination exists): {dst}")
continue
# Create parent directory if needed
os.makedirs(os.path.dirname(dst), exist_ok=True)
# Move pages
shutil.move(src, dst)
print(f"Moved: {src} -> {dst}")
# Move corresponding uploads
move_uploads(src_rel, dst_rel)
# Update image paths in the moved MDX files
update_image_paths_in_mdx(dst, src_rel, dst_rel)
print("\nDone!")
if __name__ == '__main__':
main()