-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild
executable file
·135 lines (116 loc) · 3.85 KB
/
build
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
#!/usr/bin/env python3
import argparse
import base64
import mimetypes
import os
import re
import time
import csscompressor
import htmlmin
import jsmin
def generate_data_uri(path):
"""Generates a data URI for a file.
Parameters
----------
path : str
Path to a file.
Returns
-------
data_uri : str
File contents represented as a data URI.
"""
mime_type, encoding = mimetypes.guess_type(path)
if mime_type == "image/svg+xml":
with open(path, "r", encoding="utf-8") as svg_file:
icon_bytes = str.encode("".join(svg_file.read().splitlines()))
elif mime_type:
with open(path, "rb") as binary_file:
icon_bytes = binary_file.read()
else:
raise ValueError("{} has an unknown mime type".format(path))
file_base64 = base64.b64encode(icon_bytes)
return "data:{};base64,{}".format(mime_type, str(file_base64, "utf-8"))
def tag_to_filename(tag):
"""Converts a placeholder tag to the name of the file it represents.
Parameters
----------
tag : str
Placeholder tag (e.g. "__NMEAGEN__JQUERY_JS__").
Returns
-------
filename : str
Name of file represented by placeholder tag (e.g. "jquery.js").
"""
return ".".join(
tag.replace("__NMEAGEN__", "").replace("__", "").lower().rsplit("_", 1)
).replace("_", "-")
def generate_html(compress=True):
"""Generates the HTML page for the NMEA Generator application.
Parameters
----------
compress : bool (default: True)
True if the generated page should be compressed, False otherwise.
Returns
-------
html : str
HTML string representing the NMEA Generator as a stand-alone
page, i.e., a page which can be opened directly in a browser.
"""
working_directory = os.getcwd()
os.chdir(os.path.dirname(__file__) + "/src")
html = "__NMEAGEN__MAIN_HTML__"
# Recursively replace all placeholder tags with the contents of the files
# they represent.
while True:
tags = re.findall(r"__NMEAGEN__\w*__", html)
if len(tags) == 0:
break
for tag in tags:
filename = tag_to_filename(tag)
if filename.endswith(".html"):
with open("html/" + filename, encoding="utf-8") as html_file:
text = html_file.read()
elif filename.endswith(".js"):
with open("js/" + filename, encoding="utf-8") as js_file:
text = (
jsmin.jsmin(js_file.read())
if compress
else js_file.read()
)
elif filename.endswith(".css"):
with open("css/" + filename, encoding="utf-8") as css_file:
text = (
csscompressor.compress(css_file.read())
if compress
else css_file.read()
)
else:
text = generate_data_uri("images/" + filename)
html = html.replace(tag, text)
os.chdir(working_directory)
return htmlmin.minify(html) if compress else html
if __name__ == "__main__":
start = time.time()
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--debug",
action="store_true",
default=False,
help="disable HTML/JavaScript/CSS compression (for debugging)",
)
parser.add_argument(
"-o",
"--output-file",
type=argparse.FileType("w"),
default="index.html",
help="output file (default: index.html)",
)
namespace = parser.parse_args()
namespace.output_file.write(generate_html(not namespace.debug))
end = time.time()
print(
"Generated '{0}' in {1:.2f} seconds.".format(
namespace.output_file.name, end - start
)
)