-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot_to_flask.py
More file actions
50 lines (47 loc) · 1.74 KB
/
boot_to_flask.py
File metadata and controls
50 lines (47 loc) · 1.74 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
import os, sys, re
def get_files(temp, cwd):
ret = []
for item in temp:
if os.path.isdir(cwd+item):
new_cwd = cwd + item; new_temp = []
get_temp = os.listdir(cwd+item)
for file in get_temp:
get = item + "/" + file
new_temp.append(get)
temp.pop(temp.index(item))
return get_files(temp+new_temp, new_cwd)
else:
ret.append(item)
return ret
try:
export_dir = sys.argv[1].strip("/\\") + '/'
except:
export_dir = "portfolio/templates/"
templates = os.listdir(export_dir)
templates = get_files(templates, export_dir)
for page in templates:
template = export_dir + page
with open(template, "r+") as f:
html = f.read().replace(""", '"')
static_paths = re.findall(r'(?:(?<=href=")|(?<=src=")|(?<=url\())"?(?:\.\./)?assets/.+?"', html)
pages = re.findall(r'(?<=href=").+?.html"', html)
for path in static_paths:
path = re.sub(r"^\.\./", '', path)
if path[0] == '"':
path = path.strip('"')
html = re.sub(fr'"(\.\./)?{path}"', f"{{{{ url_for('static',filename='{path}') }}}}", html)
else:
path = path.strip('"')
html = re.sub(fr"(\.\./)?{path}", f"{{{{ url_for('static',filename='{path}') }}}}", html)
for page in pages:
page = re.sub(r"^\.\./", '', page)
page = page.strip('"')
route = re.sub(r'\.html', '', page)
if 'projects/' in route:
comp = route.split('/')
html = re.sub(fr"(\.\./)?{page}", f"{{{{ url_for('{comp[0]}', project='{comp[1]}') }}}}", html)
else:
html = re.sub(fr"(\.\./)?{page}", f"{{{{ url_for('{route}') }}}}", html)
f.seek(0)
f.write(html)
f.truncate()