-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_markdown_to_json.py
More file actions
executable file
·429 lines (336 loc) · 14.1 KB
/
Copy pathcv_markdown_to_json.py
File metadata and controls
executable file
·429 lines (336 loc) · 14.1 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
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
#!/usr/bin/env python3
"""
Script to convert markdown CV to JSON format
Author: Yuan Chen
"""
import os
import re
import json
import yaml
import argparse
from datetime import datetime, date
from pathlib import Path
import glob
# Custom JSON encoder to handle date objects
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
return super().default(obj)
def parse_markdown_cv(md_file):
"""Parse the markdown CV file and extract sections."""
with open(md_file, 'r', encoding='utf-8') as file:
content = file.read()
# Remove YAML front matter
content = re.sub(r'^---.*?---\s*', '', content, flags=re.DOTALL)
# Extract sections
sections = {}
current_section = None
section_content = []
for line in content.split('\n'):
if re.match(r'^=+$', line):
continue
section_match = re.match(r'^([A-Za-z\s]+)$', line.strip())
if section_match and len(line.strip()) > 0:
if current_section:
sections[current_section] = '\n'.join(section_content).strip()
section_content = []
current_section = section_match.group(1).strip()
elif current_section:
section_content.append(line)
# Add the last section
if current_section and section_content:
sections[current_section] = '\n'.join(section_content).strip()
return sections
def parse_config(config_file):
"""Parse the Jekyll _config.yml file for additional information."""
if not os.path.exists(config_file):
return {}
with open(config_file, 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
return config
def extract_author_info(config):
"""Extract author information from the config file."""
author_info = {
"name": config.get('name', ''),
"email": "",
"phone": "",
"website": config.get('url', ''),
"summary": "",
"location": {
"address": "",
"postalCode": "",
"city": "",
"countryCode": "US",
"region": ""
},
"profiles": []
}
# Extract author details if available
if 'author' in config:
author = config.get('author', {})
# Override name if author name is available
if author.get('name'):
author_info['name'] = author.get('name')
# Add email
if author.get('email'):
author_info['email'] = author.get('email')
# Add location
if author.get('location'):
author_info['location']['city'] = author.get('location', '')
# Add employer as part of summary
if author.get('employer'):
author_info['summary'] = f"Currently employed at {author.get('employer')}"
# Add bio to summary if available
if author.get('bio'):
if author_info['summary']:
author_info['summary'] += f". {author.get('bio')}"
else:
author_info['summary'] = author.get('bio')
# Add social profiles
profiles = []
# Academic profiles
if author.get('googlescholar'):
profiles.append({
"network": "Google Scholar",
"username": "",
"url": author.get('googlescholar')
})
if author.get('orcid'):
profiles.append({
"network": "ORCID",
"username": "",
"url": author.get('orcid')
})
if author.get('researchgate'):
profiles.append({
"network": "ResearchGate",
"username": "",
"url": author.get('researchgate')
})
# Social media profiles
if author.get('github'):
profiles.append({
"network": "GitHub",
"username": author.get('github'),
"url": f"https://github.com/{author.get('github')}"
})
if author.get('linkedin'):
profiles.append({
"network": "LinkedIn",
"username": author.get('linkedin'),
"url": f"https://www.linkedin.com/in/{author.get('linkedin')}"
})
if author.get('twitter'):
profiles.append({
"network": "Twitter",
"username": author.get('twitter'),
"url": f"https://twitter.com/{author.get('twitter')}"
})
author_info['profiles'] = profiles
return author_info
def parse_education(education_text):
"""Parse education section from markdown."""
education_entries = []
# Extract education entries
entries = re.findall(r'\* (.*?)(?=\n\*|\Z)', education_text, re.DOTALL)
for entry in entries:
# Parse degree, institution, and year
match = re.match(r'([^,]+), ([^,]+), (\d{4})(.*)', entry.strip())
if match:
degree, institution, year, additional = match.groups()
# Extract GPA if available
gpa_match = re.search(r'GPA: ([\d\.]+)', additional)
gpa = gpa_match.group(1) if gpa_match else None
education_entries.append({
"institution": institution.strip(),
"area": degree.strip(),
"studyType": "",
"startDate": "",
"endDate": year.strip(),
"gpa": gpa,
"courses": []
})
return education_entries
def parse_work_experience(work_text):
"""Parse work experience section from markdown."""
work_entries = []
# Extract work entries
entries = re.findall(r'\* (.*?)(?=\n\*|\Z)', work_text, re.DOTALL)
for entry in entries:
lines = entry.strip().split('\n')
if not lines:
continue
# Parse position and company
first_line = lines[0].strip()
position_match = re.match(r'(.*?), (.*?)(?:, |$)', first_line)
if position_match:
position, company = position_match.groups()
# Extract dates if available
date_match = re.search(r'(\d{4})\s*-\s*(\d{4}|present)', entry, re.IGNORECASE)
start_date = date_match.group(1) if date_match else ""
end_date = date_match.group(2) if date_match else ""
# Extract highlights
highlights = []
for line in lines[1:]:
if line.strip().startswith('*') or line.strip().startswith('-'):
highlights.append(line.strip()[1:].strip())
work_entries.append({
"company": company.strip(),
"position": position.strip(),
"website": "",
"startDate": start_date,
"endDate": end_date,
"summary": "",
"highlights": highlights
})
return work_entries
def parse_skills(skills_text):
"""Parse skills section from markdown."""
skills_entries = []
# Extract skill categories
categories = re.findall(r'(?:^|\n)(\w+.*?):\s*(.*?)(?=\n\w+.*?:|\Z)', skills_text, re.DOTALL)
for category, skills in categories:
# Extract individual skills
skill_list = [s.strip() for s in re.split(r',|\n', skills) if s.strip()]
skills_entries.append({
"name": category.strip(),
"level": "",
"keywords": skill_list
})
return skills_entries
def parse_publications(pub_dir):
"""Parse publications from the _publications directory."""
publications = []
if not os.path.exists(pub_dir):
return publications
for pub_file in sorted(glob.glob(os.path.join(pub_dir, "*.md"))):
with open(pub_file, 'r', encoding='utf-8') as file:
content = file.read()
# Extract front matter
front_matter_match = re.match(r'^---\s*(.*?)\s*---', content, re.DOTALL)
if front_matter_match:
front_matter = yaml.safe_load(front_matter_match.group(1))
# Extract publication details
pub_entry = {
"name": front_matter.get('title', ''),
"publisher": front_matter.get('venue', ''),
"releaseDate": front_matter.get('date', ''),
"website": front_matter.get('paperurl', ''),
"summary": front_matter.get('excerpt', '')
}
publications.append(pub_entry)
return publications
def parse_talks(talks_dir):
"""Parse talks from the _talks directory."""
talks = []
if not os.path.exists(talks_dir):
return talks
for talk_file in sorted(glob.glob(os.path.join(talks_dir, "*.md"))):
with open(talk_file, 'r', encoding='utf-8') as file:
content = file.read()
# Extract front matter
front_matter_match = re.match(r'^---\s*(.*?)\s*---', content, re.DOTALL)
if front_matter_match:
front_matter = yaml.safe_load(front_matter_match.group(1))
# Extract talk details
talk_entry = {
"name": front_matter.get('title', ''),
"event": front_matter.get('venue', ''),
"date": front_matter.get('date', ''),
"location": front_matter.get('location', ''),
"description": front_matter.get('excerpt', '')
}
talks.append(talk_entry)
return talks
def parse_teaching(teaching_dir):
"""Parse teaching from the _teaching directory."""
teaching = []
if not os.path.exists(teaching_dir):
return teaching
for teaching_file in sorted(glob.glob(os.path.join(teaching_dir, "*.md"))):
with open(teaching_file, 'r', encoding='utf-8') as file:
content = file.read()
# Extract front matter
front_matter_match = re.match(r'^---\s*(.*?)\s*---', content, re.DOTALL)
if front_matter_match:
front_matter = yaml.safe_load(front_matter_match.group(1))
# Extract teaching details
teaching_entry = {
"course": front_matter.get('title', ''),
"institution": front_matter.get('venue', ''),
"date": front_matter.get('date', ''),
"role": front_matter.get('type', ''),
"description": front_matter.get('excerpt', '')
}
teaching.append(teaching_entry)
return teaching
def parse_portfolio(portfolio_dir):
"""Parse portfolio items from the _portfolio directory."""
portfolio = []
if not os.path.exists(portfolio_dir):
return portfolio
for portfolio_file in sorted(glob.glob(os.path.join(portfolio_dir, "*.md"))):
with open(portfolio_file, 'r', encoding='utf-8') as file:
content = file.read()
# Extract front matter
front_matter_match = re.match(r'^---\s*(.*?)\s*---', content, re.DOTALL)
if front_matter_match:
front_matter = yaml.safe_load(front_matter_match.group(1))
# Extract portfolio details
portfolio_entry = {
"name": front_matter.get('title', ''),
"category": front_matter.get('collection', 'portfolio'),
"date": front_matter.get('date', ''),
"url": front_matter.get('permalink', ''),
"description": front_matter.get('excerpt', '')
}
portfolio.append(portfolio_entry)
return portfolio
def create_cv_json(md_file, config_file, repo_root, output_file):
"""Create a JSON CV from markdown and other repository data."""
# Parse the markdown CV
sections = parse_markdown_cv(md_file)
# Parse config file
config = parse_config(config_file)
# Extract author information
author_info = extract_author_info(config)
# Create the JSON structure
cv_json = {
"basics": author_info,
"work": parse_work_experience(sections.get('Work experience', '')),
"education": parse_education(sections.get('Education', '')),
"skills": parse_skills(sections.get('Skills', '')),
"languages": [],
"interests": [],
"references": []
}
# Add publications
cv_json["publications"] = parse_publications(os.path.join(repo_root, "_publications"))
# Add talks
cv_json["presentations"] = parse_talks(os.path.join(repo_root, "_talks"))
# Add teaching
cv_json["teaching"] = parse_teaching(os.path.join(repo_root, "_teaching"))
# Add portfolio
cv_json["portfolio"] = parse_portfolio(os.path.join(repo_root, "_portfolio"))
# Extract languages and interests from config if available
if 'languages' in config:
cv_json["languages"] = config.get('languages', [])
if 'interests' in config:
cv_json["interests"] = config.get('interests', [])
# Write the JSON to a file
with open(output_file, 'w', encoding='utf-8') as file:
json.dump(cv_json, file, indent=2, cls=DateTimeEncoder)
print(f"Successfully converted {md_file} to {output_file}")
def main():
"""Main function to parse arguments and run the conversion."""
parser = argparse.ArgumentParser(description='Convert markdown CV to JSON format')
parser.add_argument('--input', '-i', required=True, help='Input markdown CV file')
parser.add_argument('--output', '-o', required=True, help='Output JSON file')
parser.add_argument('--config', '-c', help='Jekyll _config.yml file')
args = parser.parse_args()
# Get repository root (parent directory of the input file's directory)
repo_root = str(Path(args.input).parent.parent)
create_cv_json(args.input, args.config, repo_root, args.output)
if __name__ == '__main__':
main()