-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile
executable file
·45 lines (38 loc) · 1.59 KB
/
compile
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
#!/usr/bin/env python3
import os, subprocess
"""
List of processes used in compilation. Order is arbitrary, commands are executed asynchronously.
Each dictionary in the list should contain:
command list or string that will be passed to subprocess.Popen
replacements list of tuples where each tuple contains a needle string and a replacement function
"""
procs = [
{
"command": ["compile-js", "js/app.js", "js/date.js"],
"replacements": [
("""<script src="js/app.js"></script>""", lambda compiled : f"""<script>{compiled}</script>"""),
("""<script src="js/date.js"></script>""", lambda _ : ''),
],
}, {
"command": ["compile-css", "css/main.css"],
"replacements": [
("""<link rel="stylesheet" type="text/css" href="css/main.css">""", lambda compiled : f"""<style>{compiled}</style>"""),
],
},
]
os.chdir(os.path.dirname(os.path.abspath(__file__)))
for proc in procs:
proc["Popen"] = subprocess.Popen(proc["command"], stdout=subprocess.PIPE)
html_code = open("source.html").read()
for proc in procs:
proc["Popen"].wait()
for proc in procs:
compiled = proc["Popen"].stdout.read().decode()
for replacement in proc["replacements"]:
html_code = html_code.replace(replacement[0], replacement[1](compiled))
html_proc = subprocess.Popen(["html-minifier", "--collapse-whitespace"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
html_proc.stdin.write(html_code.encode())
html_proc.stdin.close()
html_proc.wait()
result = html_proc.stdout.read().decode()
open("index.html", "w").write(result)