-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakefile
104 lines (96 loc) · 3.52 KB
/
Snakefile
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
from os.path import isfile
from sys import stderr
from os import remove
from re import sub
from htmlmin import minify
from glob import glob
from csscompressor import compress
from base64 import b64encode
from urllib.parse import quote_from_bytes
from shutil import which
from subprocess import call
configfile: "src/config.yaml"
rule all:
input: "index.html", "cv/index.html"
rule reset:
run:
for filename in rules.all.input:
if isfile(filename):
stderr.write("Removing {}\n".format(filename))
remove(filename)
rule min_html:
input: html="temp/{name}.htm"
output: html="{name}.html"
run:
with open(input.html) as html_in:
raw_uncompressed = html_in.read()
if not config.get("keep_protocols", False):
mask = r"([\"\'])(http:|https:)"
uncompressed = sub(mask, r"\1", raw_uncompressed)
else:
uncompressed = raw_uncompressed
if not config.get("keep_long_ids", False):
for identifier in "container", "fullname", "vignette", "links":
uncompressed = uncompressed.replace(identifier, identifier[0])
raw_compressed = minify(
uncompressed,
remove_comments=True,
reduce_boolean_attributes=True,
remove_all_empty_space=True
)
compressed = raw_compressed.replace("‍", " ")
with open(output.html, "w") as html_out:
html_out.write(compressed)
rule intermediate_index:
input:
html="src/html/index.html",
css="temp/min.css"
output: html=temp("temp/index.htm")
params:
marker="<style id='inline_all' type='text/css'></style>",
start_tag="<style type='text/css'>",
end_tag="</style>"
run:
with open(input.html) as html_in, open(output.html, "w") as html_out:
for line in html_in:
if line.strip() == params.marker:
with open(input.css) as css_in:
min_css = css_in.read()
html_out.write(params.start_tag + min_css + params.end_tag)
else:
html_out.write(line)
rule min_css:
input: css=glob("src/css/*.css")
output: css=temp("temp/min.css")
run:
with open(output.css, "w") as css_out:
for css_file in input.css:
with open(css_file) as css_in:
min_css = compress(css_in.read())
css_out.write(min_css)
rule intermediate_cv_index:
input:
html="src/html/cv/index.html",
png="temp/preview-blurred.png"
output: html=temp("temp/cv/index.htm")
params:
marker="<img id='inline_all'>",
replacement="<img src='data:image/png;base64,{}'/>"
run:
with open(input.html) as html_in, open(output.html, "w") as html_out:
for line in html_in:
if line.strip() == params.marker:
with open(input.png, "rb") as png_handle:
raw_b64data = b64encode(png_handle.read())
b64data = quote_from_bytes(raw_b64data)
html_out.write(params.replacement.format(b64data))
else:
html_out.write(line)
rule compress_png:
input: png="src/img/preview-blurred.png"
output: png=temp("temp/preview-blurred.png")
run:
if which("optipng"):
call(["optipng", "-o7", "-zm1-9", "-out", output.png, input.png])
else:
call(["cp", input.png, output.png])