-
Notifications
You must be signed in to change notification settings - Fork 6
/
resume.py
executable file
·246 lines (196 loc) · 7.26 KB
/
resume.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
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# Build script for my resume
#
# This sources the "resume.yaml" and produces the resume in several formats
# from Jinja2 templates, including HTML, plain text, Markdown, and Gemini.
# -----------------------------------------------------------------------------
from jinja2 import Environment, FileSystemLoader, select_autoescape
import yaml
import json
import markdown
import datetime
import os
import re
import sys
# -----------------------------------------------------------------------------
# Config
# Environment variables with defaults.
# -----------------------------------------------------------------------------
class Config(dict):
"""Config class."""
def __getattr__(self, attr):
if attr in self:
item = self[attr]
if isinstance(item, dict):
return Config(item)
return item
raise AttributeError(f"Config has no attribute {attr}")
config = Config({
'template_dir': os.environ.get('RESUME_TEMPLATE_DIR', 'templates'),
'resume_yaml': os.environ.get('RESUME_YAML', 'resume.yaml'),
'html': {
'template': os.environ.get('RESUME_HTML_TEMPLATE', 'resume.html'),
'out': os.environ.get('RESUME_HTML_OUT', 'dist/index.html'),
'css_src': os.environ.get('RESUME_CSS_TEMPLATE', 'style.css'),
},
'markdown': {
'template': os.environ.get('RESUME_MD_TEMPLATE', 'resume.md'),
'out': os.environ.get('RESUME_MD_OUT', 'README.md'),
},
'gemini': {
'template': os.environ.get('RESUME_GMI_TEMPLATE', 'resume.gmi'),
'out': os.environ.get('RESUME_GMI_OUT', 'dist/resume.gmi'),
},
'txt': {
'template': os.environ.get('RESUME_TXT_TEMPLATE', 'resume.txt'),
'out': os.environ.get('RESUME_TXT_OUT', 'dist/resume.txt'),
'narrow_template': os.environ.get('RESUME_TXT_NARROW_TEMPLATE',
'resume-narrow.txt'),
'narrow_out': os.environ.get('RESUME_TXT_NARROW_OUT',
'dist/resume-narrow.txt'),
},
'json': {
'out': os.environ.get('RESUME_JSON_OUT', 'dist/resume.json'),
},
})
# -----------------------------------------------------------------------------
# Helpers
base_dir = os.path.dirname(os.path.realpath(__file__))
# Current date
current_date_time = datetime.datetime.now()
date = current_date_time.date()
year = date.strftime("%Y")
def md_strip(string: str):
"""Returns a string with Markdown URLs and emphasis removed.
Args:
string (str): The string to parse
Returns:
_s (str): A string with Markdown removed
"""
_s = re.sub(r"\[([\w\s]+)\]\([\w\d\/\-\.:]+\)", "\\1", string)
_s = re.sub(r"(\s+)__?(.*)__?(\s+)?", "\\1\\2\\3", _s)
return _s
def resume(theformat='plain'):
"""Parses resume YAML, munges, and returns it as a dict.
Args:
theformat (str): The format to return the resume in
Returns:
resume_content (dict): The resume content read from YAML as a dict
"""
with open(config.resume_yaml, 'r') as file:
resume_content = yaml.safe_load(file)
if theformat == 'raw':
return resume_content
for i, job in enumerate(resume_content['experience']):
if 'details' in job:
details = []
for detail in job['details']:
if theformat == 'plain':
details.append(md_strip(detail))
if theformat == 'html':
details.append(markdown.markdown(detail))
resume_content['experience'][i]['details'] = details
return resume_content
def css():
"""Loads CSS source file and returns it as a string.
Returns:
css_content (str): The CSS content as a string
"""
css_file = os.path.join(config.template_dir, config.html.css_src)
with open(css_file, 'r') as _file:
css_content = _file.read()
_file.close()
return css_content
def build_template(**kwargs):
"""Compile Jinja2 template and return it as a string.
Returns:
src_file.render (str): The rendered template as a string
Keyword Arguments:
source (str): The source template file
"""
src_dir = FileSystemLoader(config.template_dir)
env = Environment(loader=src_dir,
autoescape=select_autoescape(
enabled_extensions=('html')
))
src_file = env.get_template(kwargs['source'])
return src_file.render(
resume=resume(theformat=kwargs['theformat']), css=css(), year=year
)
def write_out(**kwargs):
"""Write a file to disk.
Keyword Arguments:
target (str): The target file to write
content (str): The content to write to the target file
"""
file_out = os.path.join(base_dir, kwargs['target'])
with open(file_out, 'w') as _file:
_file.write(kwargs['content'])
_file.close()
print(f"-> Wrote {kwargs['target']}")
def gen_html():
"""Generate HTML file from Jina2 template.
The HTML file is written to the `dist` directory.
"""
html = build_template(source=config.html.template, theformat='html')
write_out(target=config.html.out, content=html)
def gen_markdown():
"""Generate Markdown file from Jina2 template."""
md = build_template(source=config.markdown.template,
autoescape=True, theformat='raw')
write_out(target=config.markdown.out, content=md)
def gen_gemini():
"""Generate Gemini file from Jina2 template."""
gmi = build_template(source=config.gemini.template, theformat='plain')
write_out(target=config.gemini.out, content=gmi)
def gen_txt():
"""Generate plain text files from Jina2 template.
A regular-width (<70 chars) and a narrow-width (<45 chars) file is created.
"""
txt = build_template(source=config.txt.template, theformat='plain')
write_out(target=config.txt.out, content=txt)
narrow_txt = build_template(
source=config.txt.narrow_template, theformat='plain')
write_out(target=config.txt.narrow_out, content=narrow_txt)
def gen_json():
"""Generate JSON from converting the YAML source."""
the_json = json.dumps(resume(theformat='plain'), indent=2)
write_out(target=config.json.out, content=the_json)
def print_usage(formats=None):
"""Print script usage."""
print(f"{sys.argv[0]} [ " + " | ".join(formats) + " ]")
def build_all():
"""Build everything."""
gen_html()
gen_markdown()
gen_gemini()
gen_txt()
gen_json()
def main():
actions = {
'all': build_all,
'html': gen_html,
'md': gen_markdown,
'markdown': gen_markdown,
'gmi': gen_gemini,
'gemini': gen_gemini,
'txt': gen_txt,
'text': gen_txt,
'gopher': gen_txt,
'json': gen_json,
}
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg in actions:
actions[arg]()
elif arg in ['-h', '--help', 'help']:
print_usage(actions.keys())
else:
print(f"Unknown argument: {arg}")
print_usage(actions.keys())
else:
print("Building everything")
build_all()
if __name__ == '__main__':
main()