forked from pupubird/Python_email_marketing_Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_external_html.py
More file actions
73 lines (57 loc) · 2.25 KB
/
compile_external_html.py
File metadata and controls
73 lines (57 loc) · 2.25 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
import re
import os
ROOT = os.path.dirname(os.path.realpath(__file__)) + \
'\\root\\'
OUTPUT = os.path.dirname(os.path.realpath(__file__)) + '\\static\\'
def replace_element(search_result, html_filename):
filename = search_result.group().replace("\"", "")
start, end = search_result.span()
data = ""
with open(ROOT+filename, 'r') as f:
for line in f.readlines():
data += line
target = OUTPUT + \
html_filename if os.path.isfile(
OUTPUT+html_filename) else ROOT+html_filename
with open(target, 'r') as g:
html = ""
for line in g.readlines():
html += line
css_result = re.search(f'<link.*"{filename}".*>', html)
js_result = re.search(f'<script.*"{filename}".*>', html)
result = css_result or js_result
if not result:
return
start, end = result.span()
with open(ROOT+filename, 'r') as h:
h_string = ""
for line in h.readlines():
h_string += line
if '.css' in filename:
h_string = "<style>"+h_string+"</style>"
elif '.js' in filename:
h_string = "<script>"+h_string+"</script>"
html = html.replace(html[start:end], h_string)
print("Compiling", ROOT+filename, "into",
OUTPUT+html_filename)
with open(OUTPUT+html_filename, 'w') as j:
j.writelines(html)
def compile_html(html_filename):
with open(ROOT+html_filename, 'r') as f:
for line in f.readlines():
start, end, result = '', '', ''
css_tag = '<link'
js_tag = '<script'
if css_tag in line or js_tag in line:
css_search_result = re.search('"\w{1,}.css"', line)
js_search_result = re.search(
'"\w{1,}.js"', line)
result = css_search_result or js_search_result
if result:
replace_element(result, html_filename)
def main():
for filename in os.listdir(ROOT):
if '.html' in filename:
compile_html(filename)
if __name__ == "__main__":
main()