-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
79 lines (65 loc) · 2.43 KB
/
Copy pathbuild.py
File metadata and controls
79 lines (65 loc) · 2.43 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
import os
import re
import html
def build_file(html_file, js_file=None, css_file=None, file_name='app', use_html=False):
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
head_match = re.search(r'<head>\n(.*?)\n</head>', html_content, re.DOTALL)
body_match = re.search(r'<body>\n(.*?)\n</body>', html_content, re.DOTALL)
head_content = head_match.group(1) if head_match else ''
body_content = body_match.group(1) if body_match else ''
css_content = ''
if css_file:
with open(css_file, 'r', encoding='utf-8') as file:
css_content = file.read()
js_content = ''
if js_file:
with open(js_file, 'r', encoding='utf-8') as file:
js_content = file.read()
if use_html:
create_html(head_content, body_content, js_file, css_content, file_name)
else:
escaped_head = re.sub(r'\\', r'\\\\', head_content)
escaped_head = re.sub(r'"', r'\\"', escaped_head)
escaped_body = re.sub(r'\\', r'\\\\', body_content)
escaped_body = re.sub(r'"', r'\\"', escaped_body)
create_javascript(escaped_head, escaped_body, js_content, css_content, file_name)
print(f'Converted files: {html_file}, {js_file}, {css_file}.\nTo {"HTML" if use_html else "JavaScript"} inside of {file_name}{".html" if use_html else ".js"}.')
def create_html(html_head, html_body, js_file, css, file_name):
output_html = f'''<!DOCTYPE html>
<html lang="en">
<head>
{html_head}
<style>
{css}
</style>
</head>
<body>
{html_body}
<script src="{js_file}"></script>
</body>
</html>'''
if output_html:
with open(f'{file_name}.html', 'w', encoding='utf-8') as file:
file.write(output_html)
def create_javascript(html_head, html_body, javascript, css, file_name):
output_javascript = f'''document.documentElement.innerHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
{html_head}
<style>
{css}
</style>
</head>
<body>
{html_body}
</body>
</html>
`;
{javascript}'''
if output_javascript:
with open(f'{file_name}.js', 'w', encoding='utf-8') as file:
file.write(output_javascript)
build_file('game.html', 'game.js', 'game.css')
build_file('game.html', 'game.js', 'game.css', "offline-version", True)