-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchwebsite.py
More file actions
executable file
·142 lines (133 loc) · 4.83 KB
/
patchwebsite.py
File metadata and controls
executable file
·142 lines (133 loc) · 4.83 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
#!/usr/bin/env python3
import os
import re
def extract_categories_and_nav(index_php_path):
with open(index_php_path, 'r', encoding='utf-8') as f:
php = f.read()
# Extract $cats array
cats_match = re.search(r'\$cats\s*=\s*array\s*\((.*?)\);', php, re.DOTALL)
cats = []
if cats_match:
cats = [c.strip().strip('"').strip("'") for c in cats_match.group(1).split(',')]
# Extract navigation block
nav_match = re.search(r'<div class="navigation">(.*?)</div>', php, re.DOTALL)
nav_html = nav_match.group(1) if nav_match else ''
return cats, nav_html
def generate_navigation_html(nav_html):
# Replace PHP logic with static highlighting using id attributes
nav_html = re.sub(
r"<a href=\"index\.php(\?cat=([a-zA-Z0-9_]+))?\"[^\>]*\>(.*?)</a>",
lambda m: f"<a href='{m.group(2) + '.html' if m.group(2) else 'index.html'}' id='{m.group(2) if m.group(2) else 'index'}'>{m.group(3)}</a>",
nav_html
)
return nav_html
def get_dynamic_title(content, fallback):
# Try to extract the first <h1> or <h2> as the title
m = re.search(r'<h1[^>]*>(.*?)</h1>', content, re.IGNORECASE|re.DOTALL)
if not m:
m = re.search(r'<h2[^>]*>(.*?)</h2>', content, re.IGNORECASE|re.DOTALL)
if m:
return re.sub('<.*?>', '', m.group(1)).strip() # Remove any tags inside
return fallback
def patch_page(filename, nav_id):
if not os.path.exists(filename):
print(f"Skipping {filename} (not found)")
return
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
# Remove any existing html/head/body tags
content = re.sub(r'(?is)<!DOCTYPE.*?<body.*?>', '', content)
content = re.sub(r'(?is)</body>.*?</html>', '', content)
content = content.strip()
# Fix internal links
content = re.sub(r'index\\.php\\?cat=([a-zA-Z0-9_]+)', r'\1.html', content)
# Dynamic title
page_title = get_dynamic_title(content, nav_id.capitalize())
# Build new HTML
new_html = f'''<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{page_title} - SAT Competition</title>
<link rel="stylesheet" href="main.css" type="text/css">
<link rel="icon" type="image/x-icon" href="doge2.ico">
<script src="https://www.w3schools.com/lib/w3.js"></script>
<style>a#{nav_id} {{ color:#c20114; }}</style>
</head>
<body>
<div class="main">
<div class="navigation" w3-include-html="navigation.html"></div>
<script>w3.includeHTML();</script>
<div class="content">
{content}
</div>
</div>
</body>
</html>
'''
with open(filename, 'w', encoding='utf-8') as f:
f.write(new_html)
print(f"Patched {filename}")
def main():
index_php = 'index.php'
welcome_html = 'welcome.html'
cats, nav_html = extract_categories_and_nav(index_php)
# Generate navigation.html
nav_out = generate_navigation_html(nav_html)
with open('navigation.html', 'w', encoding='utf-8') as f:
f.write(nav_out)
print("Generated navigation.html")
# Generate index.html from welcome.html
with open(welcome_html, 'r', encoding='utf-8') as f:
welcome_content = f.read()
index_title = get_dynamic_title(welcome_content, "Overview")
index_out = f"""<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{index_title} - SAT Competition</title>
<link rel="stylesheet" href="main.css" type="text/css">
<link rel="icon" type="image/x-icon" href="doge2.ico">
<script src="https://www.w3schools.com/lib/w3.js"></script>
<style>a#index {{ color:#c20114; }}</style>
</head>
<body>
<div class="main">
<div class="navigation" w3-include-html="navigation.html"></div>
<script>w3.includeHTML();</script>
<div class="content">
<h1>SAT Competition</h1>
{welcome_content}
</div>
</div>
</body>
</html>
"""
with open('index.html', 'w', encoding='utf-8') as f:
f.write(index_out)
print("Generated index.html")
# Patch all other pages
for cat in cats:
fname = f"{cat}.html" if cat != 'welcome' else 'index.html'
nav_id = cat if cat != 'welcome' else 'index'
if fname == 'index.html':
continue # already generated
patch_page(fname, nav_id)
def fix_all_html_links():
html_files = [f for f in os.listdir('.') if f.endswith('.html')]
for fname in html_files:
with open(fname, 'r', encoding='utf-8') as f:
content = f.read()
# Replace all index.php?cat=... (with or without quotes) with ...html
new_content = re.sub(
r'index\.php\?cat=([a-zA-Z0-9_]+)',
r'\1.html',
content
)
if new_content != content:
with open(fname, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Fixed links in {fname}")
if __name__ == '__main__':
# main()
fix_all_html_links()