forked from bamos/cv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·570 lines (492 loc) · 19.5 KB
/
generate.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/env python3
"""Generates LaTeX, markdown, and plaintext copies of my cv."""
__author__ = [
'Brandon Amos <http://bamos.github.io>',
'Ellis Michael <http://ellismichael.com>',
]
import argparse
import copy
import os
import re
import yaml
import requests
from requests_html import HTMLSession
from bs4 import BeautifulSoup
import shelve
import bibtexparser.customization as bc
from bibtexparser.bparser import BibTexParser
from datetime import date
from itertools import groupby
from jinja2 import Environment, FileSystemLoader
# TODO: Could really be cleaned up
def get_pub_md(context, config):
def _get_author_str(immut_author_list):
authors = copy.copy(immut_author_list)
if len(authors) > 1:
authors[-1] = "and " + authors[-1]
sep = ", " if len(authors) > 2 else " "
authors = sep.join(authors)
# Hacky fix for special characters.
authors = authors.replace('\\"o', 'ö')
return authors
# [First Initial]. [Last Name]
def _format_author_list(immut_author_list):
formatted_authors = []
for author in immut_author_list:
if 'zico' in author.lower():
new_auth = 'J. Z. Kolter'
if '*' in author:
new_auth += '*'
else:
new_auth = author.split(", ")
new_auth = new_auth[1][0] + ". " + new_auth[0]
if config['name'] in new_auth:
new_auth = "<strong>" + new_auth + "</strong>"
formatted_authors.append(new_auth)
return formatted_authors
def _get_pub_str(pub, prefix, gidx, includeImage):
author_str = _get_author_str(pub['author'])
# prefix = category['prefix']
title = pub['title']
# if title[-1] not in ("?", ".", "!"):
# title += ","
# title = '"{}"'.format(title)
# if 'link' in pub:
# title = "<a href=\'{}\'>{}</a>".format(
# pub['link'], title)
title = title.replace("\n", " ")
assert('_venue' in pub and 'year' in pub)
yearVenue = "{} {}".format(pub['_venue'], pub['year'])
highlight = 'selected' in pub
# if highlight:
imgStr = '<img src="images/publications/{}.png" onerror="this.style.display=\'none\'" style=\'border: none;\' />'.format(pub['ID'], pub['ID'])
# else:
# imgStr = ''
links = []
abstract = ''
if 'abstract' in pub:
links.append("""
[<a href='javascript:;'
onclick=\'$(\"#abs_{}{}\").toggle()\'>abs</a>]""".format(pub['ID'], prefix))
abstract = context.make_replacements(pub['abstract'])
if 'link' in pub:
imgStr = "<a href=\'{}\' target='_blank'>{}</a> ".format(
pub['link'], imgStr)
links.append(
"[<a href=\'{}\' target='_blank'>pdf</a>] ".format(pub['link']))
if 'codeurl' in pub:
links.append(
"[<a href=\'{}\' target='_blank'>code</a>] ".format(pub['codeurl']))
if 'slideurl' in pub:
links.append(
"[<a href=\'{}\' target='_blank'>slide</a>] ".format(pub['slideurl']))
if 'posterurl' in pub:
links.append(
"[<a href=\'{}\' target='_blank'>poster</a>] ".format(pub['posterurl']))
if 'videourl' in pub:
links.append(
"[<a href=\'{}\' target='_blank'>video</a>] ".format(pub['videourl']))
links = ' '.join(links)
if abstract:
abstract = '''
<div id="abs_{}{}" style="text-align: justify; display: none" markdown="1">
{}
</div>
'''.format(pub['ID'], prefix, abstract)
if '_note' in pub:
note_str = f"({pub['_note']})"
else:
note_str = ''
tr_style = 'style="background-color: #ffffd0"' if highlight else ''
if includeImage:
return '''
<tr id="tr-{}" {}>
<td class="col-md-3">{}</td>
<td>
<em>{}</em><br>
{}<br>
{} {} <br>
[{}{}] {}<br>
{}
</td>
</tr>
'''.format(
pub['ID'], tr_style, imgStr, title, author_str, yearVenue, note_str, prefix, gidx, links, abstract
)
else:
return '''
<tr>
<td>
[{}{}] <strong>{}</strong><br>
{}<br>
{}<br>
{}
{}<br>
{}
</td>
</tr>
'''.format(prefix, gidx, title, author_str, yearVenue, note_str, links, abstract)
def load_and_replace(bibtex_file):
with open(os.path.join('publications', bibtex_file), 'r') as f:
p = BibTexParser(f.read(), bc.author).get_entry_list()
for pub in p:
for field in pub:
if field is not 'link':
pub[field] = context.make_replacements(pub[field])
pub['author'] = _format_author_list(pub['author'])
return p
# if 'categories' in config:
# contents = []
# for category in config['categories']:
# type_content = {}
# type_content['title'] = category['heading']
# pubs = load_and_replace(category['file'])
# details = ""
# # sep = "<br><br>\n"
# sep = "\n"
# for i, pub in enumerate(pubs):
# details += _get_pub_str(pub, category['prefix'],
# i + 1, includeImage=False) + sep
# type_content['details'] = details
# type_content['file'] = category['file']
# contents.append(type_content)
# else:
include_image = config['include_image']
sort_bib = config['sort_bib']
group_by_year = config['group_by_year']
contents = {}
pubs = load_and_replace(config['file'])
sep = "\n"
if sort_bib:
pubs = sorted(pubs, key=lambda pub: int(pub['year']), reverse=True)
if group_by_year:
for pub in pubs:
m = re.search('(\d{4})', pub['year'])
assert m is not None
pub['year_int'] = int(m.group(1))
details = ''
gidx = 1
for year, year_pubs in groupby(pubs, lambda pub: pub['year_int']):
details += f'<h2>{year}</h2>\n'
details += '<table class="table table-hover">\n'
for i, pub in enumerate(year_pubs):
details += _get_pub_str(
pub, '', gidx, includeImage=include_image) + sep
gidx += 1
details += '</table>\n'
else:
details = '<table class="table table-hover">'
for i, pub in enumerate(pubs):
details += _get_pub_str(pub, '', i + 1, includeImage=include_image) + sep
details += '</table>'
contents['details'] = details
contents['file'] = config['file']
return contents
# TODO: Could really be cleaned up
def get_pub_latex(context, config):
def _get_author_str(immut_author_list):
authors = copy.copy(immut_author_list)
if len(authors) > 1:
authors[-1] = "and " + authors[-1]
sep = ", " if len(authors) > 2 else " "
authors = sep.join(authors)
return authors
# [First Initial]. [Last Name]
def _format_author_list(immut_author_list):
formatted_authors = []
for author in immut_author_list:
if 'zico' in author.lower():
new_auth = 'J. Z. Kolter'
if '*' in author:
new_auth += '*'
else:
new_auth = author.split(", ")
new_auth = new_auth[1][0] + ". " + new_auth[0]
if config['name'] in new_auth:
new_auth = r"\textbf{" + new_auth + r"}"
new_auth = new_auth.replace('. ', '.~')
formatted_authors.append(new_auth)
return formatted_authors
def _get_pub_str(pub, prefix, gidx):
author_str = _get_author_str(pub['author'])
# prefix = category['prefix']
title = pub['title']
# if title[-1] not in ("?", ".", "!"):
# title += ","
# title = '"{}"'.format(title)
# if 'link' in pub:
# title = "<a href=\'{}\'>{}</a>".format(
# pub['link'], title)
title = title.replace("\n", " ")
if 'link' in pub:
title = r"\href{{{}}}{{{}}} ".format(pub['link'], title)
assert('_venue' in pub and 'year' in pub)
yearVenue = "{} {}".format(pub['_venue'], pub['year'])
links = []
# if 'link' in pub:
# links.append(
# r"[\href{{{}}}{{pdf}}] ".format(pub['link']))
if 'codeurl' in pub:
links.append(
r"[\href{{{}}}{{code}}] ".format(pub['codeurl']))
if 'slideurl' in pub:
links.append(
r"[\href{{{}}}{{slide}}] ".format(pub['slideurl']))
links = ' '.join(links)
highlight_color = '\cellcolor{tab_highlight}' if 'selected' in pub else ''
if '_note' in pub:
# note_str = r'{} && \textbf{{{}}} \\'.format(
note_str = f"({pub['_note']})"
else:
note_str = ''
return rf'''
\begin{{minipage}}{{\textwidth}}
\begin{{tabular}}{{R{{8mm}}p{{1mm}}p{{6.5in}}}}
{highlight_color} {prefix}{gidx}.\hspace*{{1mm}} && \textit{{{title}}} {links} \\
{highlight_color} && {author_str} \\
{highlight_color} && {yearVenue} {note_str} \\
\end{{tabular}} \\[2mm]
\end{{minipage}}'''
def load_and_replace(bibtex_file):
with open(os.path.join('publications', bibtex_file), 'r') as f:
p = BibTexParser(f.read(), bc.author).get_entry_list()
for pub in p:
for field in pub:
if field is not 'link':
pub[field] = context.make_replacements(pub[field])
pub['author'] = _format_author_list(pub['author'])
return p
sort_bib = config['sort_bib']
group_by_year = config['group_by_year']
contents = {}
pubs = load_and_replace(config['file'])
sep = "\n"
if sort_bib:
pubs = sorted(pubs, key=lambda pub: int(pub['year']), reverse=True)
if group_by_year:
for pub in pubs:
m = re.search('(\d{4})', pub['year'])
assert m is not None
pub['year_int'] = int(m.group(1))
details = ''
gidx = 1
for year, year_pubs in groupby(pubs, lambda pub: pub['year_int']):
details += rf'\subsection{{{year}}}' + '\n'
for i, pub in enumerate(year_pubs):
details += _get_pub_str(pub, '', gidx) + sep
gidx += 1
else:
assert False
contents['details'] = details
contents['file'] = config['file']
return contents
def add_repo_data(context, config):
repo_htmls = shelve.open('repo_htmls.shelf')
for item in config:
assert 'repo_url' in item
assert 'year' in item
assert 'github' in item['repo_url']
short_name = re.search('.*github\.com/(.*)', item['repo_url'])[1]
if 'name' not in item:
item['name'] = short_name
# Scrape the repo HTML instead of using the GitHub API
# to avoid being rate-limited (sorry), and be nice by
# caching to disk.
if short_name not in repo_htmls:
r = requests.get(item['repo_url'])
repo_htmls[short_name] = r.content
ht = requests.get(item['repo_url']).text
soup = BeautifulSoup(ht, 'lxml')
item['stars'] = soup.find(
'a', class_="social-count js-social-count"
).text.strip()
if 'desc' not in item:
item['desc'] = soup.find('p', class_='f4 mt-3').text.strip()
# import ipdb; ipdb.set_trace()
class RenderContext(object):
BUILD_DIR = 'build'
TEMPLATES_DIR = 'templates'
SECTIONS_DIR = 'sections'
DEFAULT_SECTION = 'items'
BASE_FILE_NAME = 'cv'
def __init__(self, context_name, file_ending, jinja_options, replacements):
self._file_ending = file_ending
self._replacements = replacements
context_templates_dir = os.path.join(self.TEMPLATES_DIR, context_name)
self._output_file = os.path.join(
self.BUILD_DIR, self.BASE_FILE_NAME + self._file_ending)
self._base_template = self.BASE_FILE_NAME + self._file_ending
self._context_type_name = context_name + 'type'
self._jinja_options = jinja_options.copy()
self._jinja_options['loader'] = FileSystemLoader(
searchpath=context_templates_dir)
self._jinja_env = Environment(**self._jinja_options)
def make_replacements(self, yaml_data):
# Make a copy of the yaml_data so that this function is idempotent
yaml_data = copy.copy(yaml_data)
if isinstance(yaml_data, str):
for o, r in self._replacements:
yaml_data = re.sub(o, r, yaml_data)
elif isinstance(yaml_data, dict):
for k, v in yaml_data.items():
# TODO: Better way of handling exceptions?
if k == 'advising':
continue
yaml_data[k] = self.make_replacements(v)
elif isinstance(yaml_data, list):
for idx, item in enumerate(yaml_data):
yaml_data[idx] = self.make_replacements(item)
return yaml_data
def _render_template(self, template_name, yaml_data):
template_name = template_name.replace(os.path.sep, '/') # Fixes #11.
return self._jinja_env.get_template(template_name).render(yaml_data)
@staticmethod
def _make_double_list(items):
groups = []
items_temp = list(items)
while len(items_temp):
group = {}
group['first'] = items_temp.pop(0)
if len(items_temp):
group['second'] = items_temp.pop(0)
groups.append(group)
return groups
def render_resume(self, yaml_data):
# Make the replacements first on the yaml_data
yaml_data = self.make_replacements(yaml_data)
body = ''
for section_tag, section_title in yaml_data['order']:
print(" + Processing section: {}".format(section_tag))
section_data = {'name': section_title}
section_content = None if section_tag == "NEWPAGE" else yaml_data[section_tag]
if section_tag == 'about':
if self._file_ending == '.tex':
continue
section_template_name = "section" + self._file_ending
section_data['data'] = section_content
elif section_tag == 'news':
if self._file_ending == '.tex':
continue
section_template_name = os.path.join(self.SECTIONS_DIR, 'news.md')
section_data['items'] = section_content
elif section_tag == 'repos':
add_repo_data(self, section_content)
section_data['items'] = section_content
section_template_name = os.path.join(
self.SECTIONS_DIR, section_tag + self._file_ending)
elif section_tag in ['coursework', 'education', 'honors',
'industry', 'summer_schools', 'skills', 'service',
'talks', 'advising' ]:
#'research','teaching',
section_data['items'] = section_content
section_template_name = os.path.join(
self.SECTIONS_DIR, section_tag + self._file_ending)
elif 'publications' in section_tag:
if self._file_ending == ".tex":
# section_data['content'] = section_content
section_data['content'] = get_pub_latex(self, section_content)
elif self._file_ending == ".md":
section_data['content'] = get_pub_md(self, section_content)
section_data['scholar_id'] = yaml_data['social']['google_scholar']
section_template_name = os.path.join(
self.SECTIONS_DIR, section_tag + self._file_ending)
elif section_tag == 'NEWPAGE':
pass
else:
print("Error: Unrecognized section tag: {}".format(section_tag))
# sys.exit(-1) TODO
continue
if section_tag == 'NEWPAGE':
if self._file_ending == ".tex":
body += "\n\n\\newpage\n"
elif self._file_ending == ".md":
pass
else:
rendered_section = self._render_template(
section_template_name, section_data)
body += rendered_section.rstrip() + '\n\n\n'
yaml_data['body'] = body
yaml_data['today'] = date.today().strftime("%B %d, %Y")
return self._render_template(
self._base_template, yaml_data).rstrip() + '\n'
def write_to_outfile(self, output_data):
with open(self._output_file, 'wb') as out:
output_data = output_data.encode('utf-8')
out.write(output_data)
LATEX_CONTEXT = RenderContext(
'latex',
'.tex',
dict(
block_start_string='~<',
block_end_string='>~',
variable_start_string='<<',
variable_end_string='>>',
comment_start_string='<#',
comment_end_string='#>',
trim_blocks=True,
lstrip_blocks=True
),
[]
)
MARKDOWN_CONTEXT = RenderContext(
'markdown',
'.md',
dict(
trim_blocks=True,
lstrip_blocks=True
),
[
(r'\\\\\[[^\]]*]', '\n'), # newlines
(r'~', ' '), # spaces
(r'\\ ', ' '), # spaces
(r'\\&', '&'), # unescape &
(r'\\\$', '\$'), # unescape $
(r'\\%', '%'), # unescape %
(r'\\textbf{(.*)}', r'**\1**'), # bold text
(r'\{ *\\bf *(.*)\}', r'**\1**'),
(r'\\textit{(.*)}', r'*\1*'), # italic text
(r'\{ *\\it *(.*)\}', r'*\1*'),
(r'\\LaTeX', 'LaTeX'), # \LaTeX to boring old LaTeX
(r'\\TeX', 'TeX'), # \TeX to boring old TeX
('---', '-'), # em dash
('--', '-'), # en dash
(r'``([^\']*)\'\'', r'"\1"'), # quotes
(r'\\url{([^}]*)}', r'[\1](\1)'), # urls
(r'\\href{([^}]*)}{([^}]*)}', r'[\2](\1)'), # urls
(r'\{([^}]*)\}', r'\1'), # Brackets.
]
)
def process_resume(context, yaml_data, preview):
rendered_resume = context.render_resume(yaml_data)
if preview:
print(rendered_resume)
else:
context.write_to_outfile(rendered_resume)
def main():
# Parse the command line arguments
parser = argparse.ArgumentParser(description='Generates HTML, LaTeX, and Markdown resumes from data in YAML files.')
parser.add_argument('yamls', metavar='YAML_FILE', nargs='+',
help='The YAML files that contain the resume/cv'
'details, in order of increasing precedence')
parser.add_argument('-p', '--preview', action='store_true',
help='prints generated content to stdout instead of writing to file')
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--latex', action='store_true',
help='only generate LaTeX resume/cv')
group.add_argument('-m', '--markdown', action='store_true',
help='only generate Markdown resume/cv')
args = parser.parse_args()
yaml_data = {}
for yaml_file in args.yamls:
with open(yaml_file) as f:
yaml_data.update(yaml.load(f))
if args.latex or args.markdown:
if args.latex:
process_resume(LATEX_CONTEXT, yaml_data, args.preview)
elif args.markdown:
process_resume(MARKDOWN_CONTEXT, yaml_data, args.preview)
else:
process_resume(LATEX_CONTEXT, yaml_data, args.preview)
process_resume(MARKDOWN_CONTEXT, yaml_data, args.preview)
if __name__ == "__main__":
main()