-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.py
217 lines (172 loc) · 6.11 KB
/
cv.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
#!/usr/bin/env python3
import os
import re
# isort skip line
import yaml
cv_details = os.getenv("cv_file")
template = os.getenv("template_file")
out_html = os.getenv("output_file")
template_html = open(template, "r").read()
cv_yaml = open(cv_details, "r").read()
cv_yaml = yaml.safe_load(cv_yaml)
def section_replace_scalars(section, replacements):
if "{list_item}" in section:
if type(replacements) == str:
section = section.replace("{list_item}", replacements + "\n")
else:
details = ""
for X in replacements:
copy = section
copy = copy.replace("{list_item}", X.strip() + "\n")
details += copy
section = details
else:
matches = re.findall("{(.*?)}", section, re.M | re.S)
for X in matches:
section = re.sub(
"{" + X + "}",
replacements[X].strip() + "\n",
section,
flags=re.I | re.M | re.S,
)
return section
def references():
global template_html
match = re.search("<references>(.*?)</references>", template_html, re.M | re.S)
match = match.group(1).strip()
replacement = ""
for ref in cv_yaml["references"]:
replacement += section_replace_scalars(match, ref)
template_html = re.sub(
"<references>(.*?)</references>",
replacement,
template_html,
flags=re.I | re.M | re.S,
)
def pages():
global template_html
for count, value in enumerate(cv_yaml["pages"]):
X = str(count + 1)
match = re.search(
"<page" + X + ">(.*?)</page" + X + ">", template_html, re.M | re.S
)
match = match.group(1).strip()
page = cv_yaml["pages"][count]
jobs = page["jobs"]
replacement = ""
for job in jobs:
job_section = match
role_match = re.search("<roles>(.*?)</roles>", job_section, re.M | re.S)
role_match = role_match.group(1).strip()
roles_replacement = ""
for role in job["roles"]:
role_section = role_match
details_match = re.search(
"<details>(.*?)</details>", role_section, re.M | re.S
)
details_match = details_match.group(1).strip()
details_replacement = ""
for detail in role["details"]:
detail_copy = section_replace_scalars(details_match, detail)
details_replacement += detail_copy
role_section = re.sub(
"<details>(.*?)</details>",
details_replacement,
role_section,
flags=re.I | re.M | re.S,
)
role_section = section_replace_scalars(role_section, role)
roles_replacement += role_section
job_section = re.sub(
"<roles>(.*?)</roles>",
roles_replacement,
job_section,
flags=re.I | re.M | re.S,
)
job_section = section_replace_scalars(job_section, job)
replacement += job_section
template_html = re.sub(
"<page" + X + ">(.*?)</page" + X + ">",
replacement,
template_html,
flags=re.I | re.M | re.S,
)
def roles():
global template_html
template_html = re.sub(
"{current-role}",
cv_yaml["roles"][0],
template_html,
flags=re.I | re.M | re.S,
)
match = re.search(
"<previous-roles>(.*?)</previous-roles>", template_html, re.M | re.S
)
match = match.group(1).strip()
rows = ""
for count, value in enumerate(cv_yaml["roles"]):
if count == 0:
continue
row = match
row = section_replace_scalars(row, value)
rows += row
template_html = re.sub(
"<previous-roles>(.*?)</previous-roles>",
rows,
template_html,
flags=re.I | re.M | re.S,
)
def replace():
global template_html
references()
pages()
roles()
for X in cv_yaml.keys():
if type(cv_yaml[X]) == str:
replacement = cv_yaml[X]
if "\\n" in replacement:
replacement = replacement.replace("\\n", "<br/>")
template_html = template_html.replace("{" + X + "}", cv_yaml[X])
elif type(cv_yaml[X]) == list:
match = re.search(
"<" + X + ">(.*?)</" + X + ">", template_html, re.M | re.S
)
if match == None:
continue
match_instance = match.group(1).strip()
list_replacement = ""
for y in cv_yaml[X]:
copy = match_instance
if type(y) == dict:
if "list" in y:
match = re.search(
"<children>(.*?)</children>", copy, re.M | re.S
)
match = match.group(1).strip()
sublist = section_replace_scalars(match, y["list"])
copy = re.sub(
"<children>(.*?)</children>",
sublist,
copy,
flags=re.I | re.M | re.S,
)
copy = section_replace_scalars(copy, y["item"])
else:
copy = re.sub(
"{list_item}.*?<ul.*?<\/ul>",
y["item"],
copy,
flags=re.I | re.M | re.S,
)
list_replacement += copy
elif type(y) == str:
copy = section_replace_scalars(copy, y)
list_replacement += copy
template_html = re.sub(
"<" + X + ">(.*?)<\/" + X + ">",
list_replacement,
template_html,
flags=re.I | re.M | re.S,
)
replace()
open(out_html, "w").write(template_html)