-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorganize_docs.py
More file actions
executable file
·227 lines (185 loc) · 7.35 KB
/
reorganize_docs.py
File metadata and controls
executable file
·227 lines (185 loc) · 7.35 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
import os
import re
import shutil
from pathlib import Path
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("reorganize_docs.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Define the base directory containing the documentation
BASE_DIR = "thirdweb_typescript_docs"
# Define the target category directories
CATEGORIES = {
"UI Components": "UI Components",
"React Hooks": "React Hooks",
"Core Functions": "Core Functions",
"Advanced Topics": "Advanced Topics",
}
# Create a backup of the original directory
def backup_directory():
backup_dir = f"{BASE_DIR}_backup"
if os.path.exists(backup_dir):
logger.info(f"Backup directory {backup_dir} already exists, skipping backup.")
else:
logger.info(f"Creating backup of {BASE_DIR} to {backup_dir}")
shutil.copytree(BASE_DIR, backup_dir)
logger.info("Backup completed.")
# Ensure all category directories exist
def create_category_directories():
for category in CATEGORIES.values():
os.makedirs(os.path.join(BASE_DIR, category), exist_ok=True)
logger.info(f"Ensured directory exists: {os.path.join(BASE_DIR, category)}")
# Function to determine which category a file belongs to
def determine_category(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
filename = os.path.basename(file_path)
filename_without_ext = os.path.splitext(filename)[0]
# Check for UI Components
ui_component_patterns = [
r'\b(?:Component|Button|Provider|Modal|Renderer|ConnectButton|ThirdwebProvider)\b',
r'\bUI\b.*\bcomponent\b',
r'\bRender',
r'<.*>' # HTML/JSX tag pattern
]
# Check for React Hooks
react_hook_patterns = [
r'\buse[A-Z]\w+', # Functions starting with "use" followed by capital letter
r'\bhook\b',
r'\buseEffect\b|\buseState\b|\buseContext\b|\buseReducer\b'
]
# Check for Core Functions
core_function_patterns = [
r'\bcreate\w+\b(?!.*hook)',
r'\bget\w+\b(?!.*hook)',
r'\bfetch\w+\b',
r'\bsend\w+\b',
r'\bconnect\b',
r'\btransact\b',
r'\baccount\b',
r'\bcontract\b'
]
# Check for Advanced Topics
advanced_topic_patterns = [
r'\baccount.?abstraction\b',
r'\bextension\b',
r'\bmodular\b',
r'\bdeploy\b',
r'\bbridge\b',
r'\bswap\b',
r'\bfiat\b',
r'\bpermission\b'
]
# Check filename patterns first (most reliable)
if filename_without_ext.startswith('use') and filename_without_ext[3:4].isupper():
return CATEGORIES["React Hooks"]
if any(re.search(rf'\b{re.escape(kw)}\b', filename_without_ext, re.IGNORECASE) for kw in [
'Button', 'Provider', 'Component', 'Modal', 'UI', 'Connect', 'Renderer'
]):
return CATEGORIES["UI Components"]
# Check content patterns
for pattern in ui_component_patterns:
if re.search(pattern, content, re.IGNORECASE):
return CATEGORIES["UI Components"]
for pattern in react_hook_patterns:
if re.search(pattern, content, re.IGNORECASE):
return CATEGORIES["React Hooks"]
for pattern in advanced_topic_patterns:
if re.search(pattern, content, re.IGNORECASE):
return CATEGORIES["Advanced Topics"]
# Default to Core Functions for anything else
return CATEGORIES["Core Functions"]
# Process a single file
def process_file(file_path):
# Skip if not a markdown file
if not file_path.endswith('.md'):
return
# Determine the appropriate category
category = determine_category(file_path)
# Get the filename
filename = os.path.basename(file_path)
# Create the destination path
dest_path = os.path.join(BASE_DIR, category, filename)
# Skip if the file is already in the correct category
if file_path == dest_path:
logger.info(f"File already in correct category: {file_path}")
return
# If the destination file already exists, use a uniquified filename
if os.path.exists(dest_path):
base, ext = os.path.splitext(filename)
counter = 1
while os.path.exists(dest_path):
new_filename = f"{base}_{counter}{ext}"
dest_path = os.path.join(BASE_DIR, category, new_filename)
counter += 1
# Move the file
logger.info(f"Moving {file_path} to {dest_path}")
shutil.move(file_path, dest_path)
# Reorganize all files in the directory
def reorganize_files():
# Walk through all subdirectories
for root, dirs, files in os.walk(BASE_DIR):
# Skip the main category directories to avoid moving files back and forth
if root == BASE_DIR or os.path.basename(root) in CATEGORIES.values():
continue
# Process each file
for file in files:
file_path = os.path.join(root, file)
process_file(file_path)
# Clean up empty directories
for root, dirs, files in os.walk(BASE_DIR, topdown=False):
for dir in dirs:
dir_path = os.path.join(root, dir)
try:
# Skip our main category directories
if os.path.join(BASE_DIR, dir) in [os.path.join(BASE_DIR, cat) for cat in CATEGORIES.values()]:
continue
# Remove empty directories
if not os.listdir(dir_path):
os.rmdir(dir_path)
logger.info(f"Removed empty directory: {dir_path}")
except Exception as e:
logger.warning(f"Could not remove directory {dir_path}: {e}")
# Create an index file for each category
def create_index_files():
for category, directory in CATEGORIES.items():
index_path = os.path.join(BASE_DIR, directory, "00_index.md")
# Get all files in the category directory
files = [f for f in os.listdir(os.path.join(BASE_DIR, directory))
if f.endswith('.md') and not f.startswith('00_')]
# Create the index content
content = f"# {category} Index\n\nThis index contains links to all documentation in the {category} category.\n\n"
# Add links to each file
for file in sorted(files):
file_path = os.path.join(directory, file)
file_name_without_ext = os.path.splitext(file)[0]
content += f"- [{file_name_without_ext}]({file})\n"
# Write the index file
with open(index_path, 'w', encoding='utf-8') as f:
f.write(content)
logger.info(f"Created index file: {index_path}")
# Main function to run the script
def main():
logger.info("Starting reorganization of Thirdweb documentation")
if not os.path.exists(BASE_DIR):
logger.error(f"Directory {BASE_DIR} does not exist. Please check the path.")
return
# Create a backup
backup_directory()
# Create category directories
create_category_directories()
# Reorganize files
reorganize_files()
# Create index files
create_index_files()
logger.info("Reorganization completed successfully.")
if __name__ == "__main__":
main()