-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjango_template.py
328 lines (287 loc) · 14.2 KB
/
django_template.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import os
import re
import shutil
from bs4 import BeautifulSoup, Comment
from urllib.parse import urlparse
class DjangoTemplateConverter:
def __init__(self, app_name, index_file):
self.app_name = app_name
self.index_file = index_file
self.index_dir = os.path.dirname(index_file)
self.static_dir = os.path.join(app_name, 'static', app_name)
self.template_dir = os.path.join(app_name, 'templates', app_name)
self.setup_directories()
self.soup = None
self.sections = {}
self.menu_items = set() # To keep menu items
def setup_directories(self):
os.makedirs(self.static_dir, exist_ok=True)
os.makedirs(self.template_dir, exist_ok=True)
def sanitize_filename(self, filename):
# Replace invalid characters but preserve directory structure
return re.sub(r'[^\w\-_\.\/\\]', '_', filename)
def read_index_file(self):
with open(self.index_file, 'r', encoding='utf-8') as file:
self.soup = BeautifulSoup(file, 'html.parser')
def find_and_copy_static_files(self):
static_tags = self.soup.find_all(['link', 'script', 'img'])
for tag in static_tags:
if tag.name == 'link' and 'stylesheet' in tag.get('rel', []):
self.process_static_file(tag, 'href', 'css')
elif tag.name == 'script':
self.process_static_file(tag, 'src', 'js')
elif tag.name == 'img':
self.process_static_file(tag, 'src', 'img')
def process_static_file(self, tag, attr, file_type):
original_path = tag.get(attr)
if original_path and not urlparse(original_path).scheme:
local_path = os.path.normpath(os.path.join(self.index_dir, original_path))
print(f"Original reference: {original_path}")
print(f"Checking existence of file: {local_path}")
if os.path.exists(local_path):
sanitized_name = self.sanitize_filename(original_path)
new_path = os.path.normpath(os.path.join(self.static_dir, sanitized_name))
os.makedirs(os.path.dirname(new_path), exist_ok=True)
shutil.copy2(local_path, new_path)
tag[attr] = f"{{% static '{self.app_name}/{sanitized_name}' %}}"
print(f"Copied {local_path} to {new_path}")
# If it's a CSS file, scan for additional URLs inside it
if file_type == 'css':
self.scan_css_for_static_files(local_path, sanitized_name)
def scan_css_for_static_files(self, css_file_path, css_sanitized_name):
with open(css_file_path, 'r', encoding='utf-8') as css_file:
css_content = css_file.read()
# Regex to match URLs in various CSS properties, excluding gradient functions
css_content = re.sub(
r'url\(\s*["\']?(?!.*?(?:linear-gradient|radial-gradient|repeating-linear-gradient).*?)\s*(.*?)\s*["\']?\s*\)',
lambda match: self.replace_with_static(match, css_file_path),
css_content
)
updated_css_path = os.path.normpath(os.path.join(self.static_dir, css_sanitized_name))
with open(updated_css_path, 'w', encoding='utf-8') as css_file:
css_file.write(css_content)
print(f"Updated CSS file written to: {updated_css_path}")
def replace_with_static(self, match, css_file_path):
url = match.group(1).strip()
if url.startswith(('http://', 'https://', 'data:', '#')):
# Ignore absolute URLs, data URIs, and fragment identifiers
return match.group(0)
# Split URL into file path and parameters
url_parts = url.split('?', 1)
file_path = url_parts[0]
url_params = '?' + url_parts[1] if len(url_parts) > 1 else ''
static_image_path = os.path.normpath(os.path.join(os.path.dirname(css_file_path), file_path))
print(f"Found image URL in CSS: {static_image_path}")
if os.path.exists(static_image_path):
sanitized_name = self.sanitize_filename(os.path.relpath(static_image_path, self.index_dir))
new_path = os.path.normpath(os.path.join(self.static_dir, sanitized_name))
print("Debugging New path: " + new_path)
print("Debugging static image path: " + static_image_path)
os.makedirs(os.path.dirname(new_path), exist_ok=True)
shutil.copy2(static_image_path, new_path)
print(f"Copied {static_image_path} to {new_path}")
relative_path = new_path.replace(self.static_dir, '/static/' + self.app_name).replace("\\", "/")
# Return URL with parameters preserved
return f'url("{relative_path}{url_params}")'
else:
print(f"Warning: {static_image_path} does not exist.")
return match.group(0)
def analyze_template(self):
comments = self.soup.find_all(string=lambda text: isinstance(text, Comment))
print("All comments found in the HTML file:")
for comment in comments:
print(f"Comment: {comment}")
for i, comment in enumerate(comments):
comment = comment.strip() # Remove leading and trailing whitespace
if 'Start' in comment:
section_name = comment.split(' ')[0].strip()
if section_name: # Only consider non-empty section names
self.sections[section_name] = {'start': i}
print(f"Found section start: {section_name}")
elif 'End' in comment:
section_name = comment.split(' ')[0].strip()
if section_name and section_name in self.sections:
self.sections[section_name]['end'] = i
print(f"Found section end: {section_name}")
def extract_section(self, section_name):
start_comment = f'{section_name} Start'
end_comment = f'{section_name} End'
comments = self.soup.find_all(string=lambda text: isinstance(text, Comment))
start_idx = -1
for i, comment in enumerate(comments):
if start_comment in comment:
start_idx = i
break
if start_idx != -1:
comment = comments[start_idx]
section_content = ''
while comment:
comment = comment.next_sibling
if comment and isinstance(comment, Comment) and end_comment in comment:
break
if comment and not isinstance(comment, Comment):
section_content += str(comment)
return section_content
return ""
def create_template_file(self, filename, content):
template_path = os.path.join(self.template_dir, filename)
os.makedirs(os.path.dirname(template_path), exist_ok=True)
with open(template_path, 'w', encoding='utf-8') as file:
file.write(content)
def convert_to_django_templates(self):
# Extract sections and create individual section templates
for section_name in self.sections:
content = self.extract_section(section_name)
content = f"{{% load static %}}\n{{% block {section_name.lower()} %}}\n{content}\n{{% endblock %}}"
self.create_template_file(f'{section_name.lower()}.html', content)
# Create head template
head_content = self.soup.head.prettify()
head_template = f"""{{% load static %}}\n{head_content}"""
self.create_template_file('head.html', head_template)
# Create body scripts template
body_scripts = [str(script) for script in self.soup.body.find_all('script')]
body_script_tags = '\n'.join(body_scripts)
body_scripts_template = f"""{{% load static %}}\n{body_script_tags}"""
self.create_template_file('body_scripts.html', body_scripts_template)
# Create base template
base_template = \
"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}Your Site Title{% endblock %}</title>
{% include 'head.html' %}
</head>
<body>
{% include 'navbar.html' %}
{% block content %}{% endblock %}
{% include 'footer.html' %}
{% include 'body_scripts.html' %}
</body>
</html>
"""
self.create_template_file('base.html', base_template)
# Create index template
index_content = "{% extends 'base.html' %}\n{% block content %}\n"
for section_name in self.sections:
if section_name.lower() not in ['navbar', 'footer']:
index_content += f" {{% include '{section_name.lower()}.html' %}}\n"
index_content += "{% endblock %}\n"
self.create_template_file('index.html', index_content)
def update_navbar_links(self):
"""
Updates links in the navbar.html file.
"""
# Path to the navbar.html file
navbar_file_path = os.path.join(self.template_dir, 'navbar.html')
# Check if the file exists
if not os.path.exists(navbar_file_path):
print(f"File {navbar_file_path} does not exist.")
return
try:
# Read the content of the file
with open(navbar_file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Update the links
updated_content = self.update_navbar_links_in_content(content)
# Write the updated content back to the file
# Utilising the create_template_file method
self.create_template_file('navbar.html', updated_content)
print(f"Updated navbar links in {navbar_file_path}")
except IOError as e:
print(f"An error occurred while processing the file: {e}")
def update_navbar_links_in_content(self, content):
"""
Update links in the passed content.
Save the original link (like index.html, about.html, etc) in the self.menu_items list.
"""
def replace_href(match):
href = match.group(1).strip()
if href.endswith('.html'):
name = href.rsplit('.', 1)[0] # Remove ".html" from the link
new_href = f'href="{{% url \'{name}\' %}}"'
print(f"Replacing: href=\"{href}\" with {new_href}")
self.menu_items.add(href) # Save the original links to self.menu_items
return new_href
return match.group(0)
# Update only links that end with .html
updated_content = re.sub(r'href="([^"]+\.html)"', replace_href, content)
return updated_content
def create_urls_file(self):
urls_content = \
f"""from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
"""
for menu_item_name in self.menu_items:
if menu_item_name != 'index.html':
urls_content += f" path('{menu_item_name.lower().replace('.html', '')}/', views.{menu_item_name.lower().replace('.html', '')}, name='{menu_item_name.lower().replace('.html', '')}'),\n"
urls_content += "]\n"
urls_file = os.path.join(self.app_name, 'urls.py')
with open(urls_file, 'w', encoding='utf-8') as file:
file.write(urls_content)
print(f"URLs configuration written to: {urls_file}")
def create_views_file(self):
views_content = \
f"""from django.shortcuts import render
#def index(request):
# return render(request, '{self.app_name}/index.html')
"""
for menu_item_name in self.menu_items:
views_content += f"""
def {menu_item_name.lower().replace('.html', '')}(request):
return render(request, '{menu_item_name.lower()}')
"""
views_file = os.path.join(self.app_name, 'views.py')
with open(views_file, 'w', encoding='utf-8') as file:
file.write(views_content)
print(f"Views configuration written to: {views_file}")
def process_additional_files(self):
"""
Process additional HTML files in the index directory that are not the main index file.
- This method scans for all HTML files in the specified index directory except the index file itself.
- It checks if a corresponding Django template file already exists in the template directory.
If it does, the method skips processing to avoid redundancy.
- For each additional HTML file, it reads the file, processes and copies any static files (CSS, JS, images)
that are referenced, and saves the resulting content to the Django template directory.
- This helps ensure all relevant HTML files are converted into Django templates efficiently.
"""
# Find all HTML files in the index directory except the index file
additional_files = [os.path.join(root, file)
for root, _, files in os.walk(self.index_dir)
for file in files if file.endswith('.html') and file != os.path.basename(self.index_file)]
for file in additional_files:
template_file_name = os.path.basename(file)
template_path = os.path.join(self.template_dir, template_file_name)
if os.path.exists(template_path):
print(f"Skipping existing file: {template_path}")
continue
with open(file, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
self.soup = soup
self.find_and_copy_static_files()
# Save the updated content to the Django template directory
with open(template_path, 'w', encoding='utf-8') as f:
f.write(str(soup))
print(f"Processed additional file: {template_file_name}")
def run(self):
self.read_index_file()
self.find_and_copy_static_files()
self.analyze_template()
print(f"Template analysis complete. Sections found: {self.sections}")
self.convert_to_django_templates()
# Update navbar links
self.update_navbar_links()
# Create URLs and views files
self.create_urls_file()
self.create_views_file()
print(self.menu_items)
# Process additional HTML files, copying static files and updating links
self.process_additional_files()
# Usage
if __name__ == '__main__':
app_name = 'portfolio'
index_file = 'Kelly/index.html'
converter = DjangoTemplateConverter(app_name, index_file)
converter.run()