/
compile.py
494 lines (382 loc) · 15.5 KB
/
compile.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import datetime
import json
import os
import re
import shutil
import sys
import time
import urllib
import urllib2
import webbrowser
import markdown as md
from jinja2 import FileSystemLoader, Environment
# This file is crazy inefficient, however I didn't care because it's only run
# once every few weeks. Eventually I'll go through and clean it up.
# So, don't judge me for some of the horrible, horrible things I do here.
# TODO:
# - Abstract the jinja stuff; move it to another file
# - Don't keep loading settings!
FOLDER_TEMP = 'app-temp'
FOLDER_REAL = 'app'
settings_new = {}
def compile_home():
args = {'page': 'home'}
# Notepads
to_compile = get_list('notepad_src')[:5]
notes = []
template = get_template('notepad_preview.html', render=False)
for note in to_compile:
args_np={'template': template, 'filename': note['filename'],
'date': note['date'], 'slug': note['slug'], 'preview': True}
notes.append(render_notepad(note['filename'], args=args_np))
args['notes'] = notes
# Articles
to_compile_article = get_list('magazine_src')[:4]
articles = []
template = get_template('magazine_preview.html', render=False)
for article in to_compile_article:
articles.append(render_magazine(article['filename'], args={
'filename': article['filename'],
'date': article['date'],
'file': article['filename'],
'slug': article['slug'],
'template': template}))
args['articles'] = articles
get_template('home.html', args, '%s/index.html' % FOLDER_TEMP)
def compile_page(file):
args = {'page': file}
get_template('%s.html' % file, args, '%s/%s.html' % (FOLDER_TEMP, file))
def render_magazine(template, args={}, output=None, render=True):
if not 'template' in args:
args['template'] = get_template('magazine.html', render=False)
if 'slug' in args:
args['ns'] = "article-%s" % args['slug']
args['file'] = template
out_file = None
if output:
out_file = re.sub('^/?[-0-9]{11}', '', output)
out_file = '%s/magazine/%s' % (FOLDER_TEMP, out_file)
return get_template(('magazine_src', template), args, render=render,
output=out_file)
def render_notepad(file, args={}, output=None):
if 'template' not in args:
args['template'] = get_template('notepad_single.html', render=False)
out_file = None
if output:
out_file = re.sub('^/?[-0-9]{11}', '', output)
out_file = '%s/notepad/%s' % (FOLDER_TEMP, out_file)
return get_template(('notepad_src', file), args=args, output=out_file)
def render_portfolio(file, args={}, output=None):
#if 'template' not in args:
# args['template'] = get_template('notepad_full.html', render=False)
out_file = None
if output:
out_file = re.sub('^/?[-0-9]{11}', '', output)
out_file = '%s/portfolio/%s.html' % (FOLDER_TEMP, out_file)
# Move assets
template_name = output
if os.path.exists('portfolio_src/%s' % template_name):
shutil.copytree('portfolio_src/%s' % template_name,
'%s/portfolio/%s' % (FOLDER_TEMP, template_name))
return get_template(('portfolio_src/%s' % file[0], file[1]), args=args, output=out_file)
def generate_bitly(url):
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
if('bitly_key' not in settings):
return url
url_base = "http://api.bitly.com/v3/shorten"
bitly_url = "%s?longUrl=%s&login=%s&apiKey=%s&format=txt" % (
url_base, urllib.quote(url), settings['bitly_login'],
settings['bitly_key'])
r = urllib2.urlopen(bitly_url)
return r.readline().strip()
def datetimeformat(value, format='%B %d, %Y'):
return value.strftime(format)
def markdown(content):
return md.markdown(content)
def footnoter(value):
i = 1
match = "\(\(\(((?:.|\n)*?)\)\)\)"
link = "<sup id='fnc-%s'>[<a href='#fn-%s'>%s</a>]</sup>"
footnote = "<li id='fn-%s'>%s [<a href='#fnc-%s'>↩</a>]</li>"
fns = []
while re.search(match, value):
footnote_text = re.search(match, value)
value = re.sub(match, link % (i, i, i), value, 1)
fns.append(footnote % (i, footnote_text.group(1), i))
i += 1
if fns:
value = value + ("<ol class='footnotes'>%s</ol>" % '\n'.join(fns))
return value
def namespacer(value, namespace, f, section="magazine"):
value = re.sub('#namespace', '#%s' % namespace, value)
value = re.sub('assets/', '%s/%s/' % (section, f.split('.')[0]), value)
return value
def escaper(text):
codes = [
('&', '&'),
('<', '<'),
('>', '>'),
('"', '"'),
("'", '''),
]
for code in codes:
text = text.replace(code[0], code[1])
return text
def url(url, use_base=False, bitly=False):
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
url = re.sub('[-0-9]{11}', '', url)
if settings['prod']:
url = re.sub('\.html', '', url)
url = re.sub('^/?magazine/', '/m/', url)
url = re.sub('^/?notepad/', '/n/', url)
url = '/%s' % re.sub('^/', '', url)
if (use_base or bitly) and 'base' in settings:
base = re.sub('/$', '', settings['base'])
url = re.sub('^/', '', url)
url = ('%s/%s' % (base, url))
if bitly:
url = generate_bitly(url)
return url
# By weight, not date. TODO: Combine with get_list. Too lazy now :)
def get_list_weight(*folders):
return_list = []
for folder in folders:
to_compile = os.listdir(folder)
to_compile = [f for f in to_compile
if re.match("\d+-(.*)(.html)?", f)]
for f in to_compile:
d = re.search('(\d+)-(.*)(.html)?', f)
slug = d.group(2)
return_list.append({'weight': int(d.group(1)), 'slug':slug, 'filename':f,
'folder': folder})
return_list.sort(key=lambda d: d['weight'])
return return_list
def get_list(*folders):
return_list = []
for folder in folders:
to_compile = os.listdir(folder)
to_compile = [f for f in to_compile
if re.match("\d{4}-\d{2}-\d{2}-(.*).html", f)]
for f in to_compile:
d = re.search('(\d{4})-(\d{2})-(\d{2})-(.*).html', f)
date = datetime.datetime(int(d.group(1)), int(d.group(2)), int(d.group(3)))
slug = d.group(4)
date_sort = time.mktime(date.timetuple())
return_list.append({'date':date, 'slug':slug, 'filename':f,
'folder': folder})
return_list.sort(reverse=True, key=lambda d: time.mktime(d['date'].timetuple()))
return return_list
def compile_magazines():
to_compile = get_list('magazine_src')
for (i, article) in enumerate(to_compile):
nav_prev = nav_next = ""
# gosh this is crazy inefficient.
if i > 0:
template = get_template('magazine_nav.html', render=False)
a = to_compile[i - 1]
nav_next = render_magazine(a['filename'], args={
'filename': a['filename'],
'date':a['date'],
'file': a['filename'],
'slug': a['slug'],
'type': 'next',
'template': template})
if i < len(to_compile) - 1:
template = get_template('magazine_nav.html', render=False)
a = to_compile[i + 1]
nav_prev = render_magazine(a['filename'], args={
'filename': a['filename'],
'date': a['date'],
'file': a['filename'],
'slug': a['slug'],
'type': 'prev',
'template': template})
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
args = {'date': article['date'],
'slug': article['slug'],
'page': 'magazine',
'filename': article['filename'],
'nav_next': nav_next,
'nav_prev': nav_prev,
'is_prod': settings['prod']}
# Move assets
template_name = article['filename'].split('.')[0]
if os.path.exists('magazine_src/%s' % template_name):
shutil.copytree('magazine_src/%s' % template_name,
'%s/magazine/%s' % (FOLDER_TEMP, template_name))
render_magazine(article['filename'], args, article['filename'])
def get_block(template, block=''):
return ''.join([i for i in template.blocks.get(block)({})])
def compile_rss(category=None, desc=None, title=None):
to_compile = get_list('notepad_src', 'magazine_src')
notes = []
first_date = False
for note in to_compile:
if not first_date:
first_date = note['date']
template = get_template('feed-item.rss', render=False)
if note['folder'] == 'notepad_src':
f = render_notepad(note['filename'], args={'template': template,
'filename': note['filename'], 'date': note['date'],
'slug': note['slug'], 'folder': 'notepad' })
else:
f = render_magazine(note['filename'], args={'template': template,
'filename': note['filename'], 'date': note['date'],
'slug': note['slug'], 'folder': 'magazine' })
# Should use pyquery...
if not category or re.search('<category>%s<\/category>' % category, f):
notes.append(f)
name = category if category else 'feed'
if not desc:
desc = 'Blog posts and articles by Gregory Koberger'
get_template('feed.rss', args={'notes': notes, 'date': first_date,
'title': title, 'desc': desc},
output='%s/%s.rss' % (FOLDER_TEMP, name))
def compile_notepads():
to_compile = get_list('notepad_src')
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
notes = []
template_full = get_template('notepad_full.html', render=False)
for (i, note) in enumerate(to_compile):
nav_prev = nav_next = ""
# gosh this is crazy inefficient.
if i > 0:
template = get_template('notepad_nav.html', render=False)
a = to_compile[i - 1]
nav_next = render_notepad(a['filename'], args={
'filename': a['filename'],
'date':a['date'],
'file': a['filename'],
'slug': a['slug'],
'type': 'next',
'template': template})
if i < len(to_compile) - 1:
template = get_template('notepad_nav.html', render=False)
a = to_compile[i + 1]
nav_prev = render_notepad(a['filename'], args={
'filename': a['filename'],
'date': a['date'],
'file': a['filename'],
'slug': a['slug'],
'type': 'prev',
'template': template})
args = {'date': note['date'], 'slug': note['slug'],
'is_prod': settings['prod'], 'filename': note['filename'],
'nav_prev': nav_prev, 'nav_next': nav_next}
f = render_notepad(note['filename'], args=args)
notes.append(f)
args['page'] = 'notebook'
args['template'] = template_full
render_notepad(note['filename'], args, note['filename'])
get_template('notepad.html', args={'notes': notes, 'page': 'notebook'}, output='%s/notepad.html' % FOLDER_TEMP)
def compile_portfolio():
to_compile = get_list_weight('portfolio_src')
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
portfolios = []
template_full = get_template('portfolio_full.html', render=False)
for (i, portfolio) in enumerate(to_compile):
nav_prev = nav_next = ""
args = {'slug': portfolio['slug'],
'is_prod': settings['prod'], 'filename': portfolio['filename']}
#f = render_portfolio(portfolio['filename'], args=args)
#portfolios.append(f)
args['page'] = 'portfolio'
args['template'] = template_full
render_portfolio((portfolio['filename'], "index.html"), args, portfolio['filename'])
#get_template('portfolio.html', args={'notes': notes, 'page': 'notebook'}, output='%s/notepad.html' % FOLDER_TEMP)
def get_template(template, args={}, output=None, render=True, stop=False):
loader = False
template_file = False
if stop:
import pdb; pdb.set_trace()
if isinstance(template, tuple):
loader = FileSystemLoader('%s/' % template[0])
template_file = template[1]
else:
loader = FileSystemLoader('templates/')
template_file = template
env = Environment(loader=loader)
env.filters['footnoter'] = footnoter
env.filters['markdown'] = markdown
env.filters['url'] = url
env.filters['escaper'] = escaper
env.filters['namespacer'] = namespacer
env.filters['datetimeformat'] = datetimeformat
env.filters['urlencode'] = urllib.quote
settings = {}
with open('settings.json', 'r') as f:
settings = json.load(f)
env.globals = {'base': settings['base'],
'year': 2011,
'article_url': get_list('magazine_src')[0]['filename']}
template_object = env.get_template(template_file)
if not render:
return template_object
rendered = template_object.render(args)
if output:
with open(output, 'w') as o:
rendered = unicode(rendered)
rendered = rendered.replace(u"\xe9", " ")
rendered = rendered.replace(u"\u2019", " ")
rendered = rendered.replace(u"\u00A0", " ")
o.write(unicode(rendered))
return rendered
#def render_tempate(template, args):
#args['base'] = 'file:///Users/gkoberger/Sites/gkoberger/'
#args['year'] = 2011
#return template.render(args)
def swap_folder(src, dst):
# First, clean out the destination folder
for the_file in os.listdir(dst):
file_path = os.path.join(dst, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
else:
shutil.rmtree(file_path)
except Exception, e:
print e
# Now, copy everything over
for the_file in os.listdir(src):
src_path = os.path.join(src, the_file)
dst_path = os.path.join(dst, the_file)
try:
if os.path.isfile(src_path):
shutil.copy(src_path, dst_path)
else:
shutil.copytree(src_path, dst_path)
except Exception, e:
print e
def move_base():
swap_folder('app-base', FOLDER_TEMP)
shutil.copytree('js', '%s/js' % FOLDER_TEMP)
shutil.copytree('css', '%s/css' % FOLDER_TEMP)
shutil.copytree('images', '%s/images' % FOLDER_TEMP)
if __name__ == '__main__':
shutil.rmtree(FOLDER_TEMP, True)
os.mkdir(FOLDER_TEMP)
move_base()
compile_notepads()
compile_magazines()
compile_portfolio()
compile_home()
compile_page('p404')
compile_page('about')
compile_page('about_new')
compile_page('portfolio')
compile_page('resume')
compile_rss()
compile_rss('mozilla', desc="Mozilla-related blog posts", title="Mozilla")
# Move from temp folder and delete it
swap_folder(FOLDER_TEMP, FOLDER_REAL)
shutil.rmtree(FOLDER_TEMP, True)