-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodo.py
214 lines (169 loc) · 5.21 KB
/
dodo.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from pathlib import Path
import hashlib
import os
import itertools
import shutil
from docutils.core import publish_parts
import sass
import cairosvg
from PIL import Image
from jinja2 import Environment, meta, FileSystemLoader, environmentfilter, Markup
from doit.tools import create_folder, result_dep
TEMPLATES_DIR = 'ballroom_theme/templates/'
OUTPUT_DIR = 'output/'
OUTPUT_STATIC = 'output/static/'
def _jinja_rst(f):
html = publish_parts(source=str(f), writer_name='html')['body']
return Markup(html)
def _jinja_resolve_asset(f):
path = Path(OUTPUT_DIR+f).resolve()
output = Path(OUTPUT_DIR).resolve()
return str(path.relative_to(output))
env = Environment(
loader=FileSystemLoader(TEMPLATES_DIR)
)
env.filters['static'] = _jinja_resolve_asset
env.filters['load_rst'] = _jinja_rst
def _minify_svg(task):
(filename,) = task.file_dep
(target,) = task.targets
with open(target, 'wb') as o:
o.write(
cairosvg.svg2svg(url=filename)
)
def _cp(task):
(filename,) = task.file_dep
(target,) = task.targets
shutil.copy(filename, target)
def _compile_jinja(filename, target):
# (filename,) = task.file_dep
# # (target,) = task.targets
# target = f
t = env.get_template(filename)
with open(target, 'w') as f:
f.write(t.render())
def _hash_asset(task):
(filename,) = task.file_dep
with open(filename, 'rb') as f:
m = hashlib.md5()
m.update(f.read())
digest = m.hexdigest()[:10]
file_path = Path(filename)
parent, name = str(file_path.parent), str(file_path.name)
a, b = name.split('.', 1)
link_filename = filename
new_filename = '{}/{}.{}.{}'.format(parent, a, digest, b)
old_filename = str(Path(link_filename).resolve())
shutil.move(old_filename, new_filename)
if os.path.islink(link_filename):
os.remove(link_filename)
os.symlink(Path(new_filename).name, link_filename)
def _convert_svg_png(task):
(filename,) = task.file_dep
(target,) = task.targets
with open(target, 'wb') as o, open(filename) as i:
o.write(
cairosvg.svg2png(url=filename)
)
def _convert_png_ico(task):
(filename,) = task.file_dep
(target,) = task.targets
im = Image.open(filename)
im.thumbnail((16, 16))
im.save(target, 'GIF')
def _compile_sass(task):
(filename,) = task.file_dep
(target,) = task.targets
with open(target, 'w') as f, open(filename) as i:
f.write(
sass.compile(string=i.read(), indented=True)
)
def task_sass_files():
OUTPUT = 'output/static/'
yield {
'name': None,
'actions': [create_folder(OUTPUT)]
}
yield {
'name': 'sass',
'actions': [_compile_sass],
'file_dep': ['ballroom_theme/static/css/main.sass'],
'targets': [OUTPUT+'main.css'],
}
def task_image_files():
OUTPUT = 'output/static/'
yield {
'name': None,
'actions': [create_folder(OUTPUT)]
}
yield {
'name': 'svg',
'actions': [_minify_svg],
'file_dep': ['ballroom_theme/static/img/dancers.svg'],
'targets': [OUTPUT+'dancers.svg'],
}
yield {
'name': 'png',
'actions': [_convert_svg_png],
'file_dep': ['ballroom_theme/static/img/dancers.svg'],
'targets': [OUTPUT+'dancers.png'],
}
yield {
'name': 'ico',
'actions': [_convert_png_ico],
'file_dep': ['ballroom_theme/static/img/dancers.png'],
'targets': [OUTPUT+'dancers.gif'],
}
yield {
'name': 'facebook',
'actions': [_minify_svg],
'file_dep': ['ballroom_theme/static/img/facebook.svg'],
'targets': [OUTPUT+'facebook.svg'],
}
yield {
'name': 'youtube',
'actions': [_minify_svg],
'file_dep': ['ballroom_theme/static/img/youtube.svg'],
'targets': [OUTPUT+'youtube.svg'],
}
yield {
'name': 'email',
'actions': [_minify_svg],
'file_dep': ['ballroom_theme/static/img/email.svg'],
'targets': [OUTPUT+'email.svg'],
}
def task_hash_assets():
for f in itertools.chain(task_image_files(), task_sass_files()):
for i in f.get('targets', []):
yield {
'name': i,
'actions': [_hash_asset],
'file_dep': [i],
}
def task_html_files():
OUTPUT = 'output/'
deps = [TEMPLATES_DIR+f for f in env.list_templates()]
yield {
'name': None,
'actions': [create_folder(OUTPUT)],
}
yield {
'name': 'index',
'actions': [(_compile_jinja, ['index.html', OUTPUT+'index.html'])],
'file_dep': deps,
'task_dep': ['hash_assets'],
'uptodate': [result_dep('sass_files'), result_dep('image_files')],
}
yield {
'name': 'faq',
'actions': [(_compile_jinja, ['faq.html', OUTPUT+'faq.html'])],
'file_dep': deps,
'task_dep': ['hash_assets'],
'uptodate': [result_dep('sass_files'), result_dep('image_files')],
}
# def task_pelican():
# yield {
# 'name': 'pelican',
# 'actions': ['pelican content -s config/local.py -o output'],
# 'task_dep': ['image_files', 'sass_files'],
# }