forked from jdossgollin/my-cv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_cv.py
More file actions
119 lines (99 loc) · 3.12 KB
/
generate_cv.py
File metadata and controls
119 lines (99 loc) · 3.12 KB
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
"""
This script builds the CV file from a template
"""
import os
import sys
import re
import filters # filters for Jinja
import jinja2
import yaml
# definitions
CONFIG_FILE = "_config.yml"
TEMPLATE_FILE = "cv.latextemplate" # NOT relative
TEMPLATE_DIR = "templates"
OUTPUT_TEX = os.path.join("docs", "Pollack-CV.tex")
class CV(object):
"""
Build CV in LaTeX and Markdown formats from YAML and BiBTeX inputs
"""
def __init__(self, config_file, filters=None, templates=None):
self.filters = filters
# read config file
with open(config_file, "r") as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
# read in section data files
sections = [
{
"title": k["title"],
"entries": yaml.load(
open(os.path.join(config["paths"]["yaml_path"], k["file"]), "r"),
Loader=yaml.BaseLoader,
),
}
for k in config["sections"]
]
# combine personal, sectional data, and publication data
self.data = {
"person": config["person"],
"paths": config["paths"],
"publications": config["publications"],
"sections": sections,
}
# load templates
templates = {} if templates is None else templates
self.loader = jinja2.loaders.DictLoader(templates)
@property
def jenv_md(self):
"""
Set up HTML/Markdown Jinja environment
"""
loader = jinja2.loaders.ChoiceLoader(
[
self.loader,
jinja2.FileSystemLoader(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "templates"
)
),
]
)
jenv = jinja2.Environment(loader=loader)
for f in self.filters:
jenv.filters[f.__name__] = f
return jenv
@property
def jenv_tex(self,):
"""
Set up TeX environment
"""
loader = jinja2.ChoiceLoader(
[self.loader, jinja2.FileSystemLoader(TEMPLATE_DIR)]
)
jenv = jinja2.Environment(loader=loader)
# define new delimiters to avoid TeX conflicts
jenv.block_start_string = "((*"
jenv.block_end_string = "*))"
jenv.variable_start_string = "((("
jenv.variable_end_string = ")))"
jenv.comment_start_string = "((="
jenv.comment_end_string = "=))"
for f in self.filters:
jenv.filters[f.__name__] = f
return jenv
def render_tex(self, template, **kwargs):
return self.jenv_tex.get_template(template).render(data=self.data, **kwargs)
def main():
my_filters = [
filters.escape_tex,
filters.select_by_attr_name,
filters.sort_by_attr,
filters.sort_first_year,
filters.sort_advisees
]
cv = CV(CONFIG_FILE, filters=my_filters)
if not os.path.isdir("docs"):
os.mkdir("docs")
with open(OUTPUT_TEX, "w") as f:
f.write(cv.render_tex(TEMPLATE_FILE))
if __name__ == "__main__":
main()