forked from lucafrance/luca-cv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd_to_tex.py
More file actions
170 lines (139 loc) · 5.21 KB
/
md_to_tex.py
File metadata and controls
170 lines (139 loc) · 5.21 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
import os.path
import sys
def personal_info_from_md_line(txt):
"""Return personal info for lines with links
e.g.
input: "`email` [abc@example.com](mailto:abc@example.com)"
output: "abc@example.com"
"""
txt = txt.split("[")[1]
txt = txt.split("]")[0]
return txt
def split_cv_line(txt):
"""Return a tuple of the side bar text and title
e.g.
input: "*Gen 2000 -- Dec 2020* Example GmbH -- Intern"
output: ("Gen 2000 -- Dec 2020", "Example GmbH -- Intern")
"""
if "*" not in txt:
return "", txt
txt = txt.split("*", 1)[1]
txt = txt.split("*")
side_text = txt[0].strip()
title = txt[1].strip()
return side_text, title
class CvSection():
def __init__(self, title):
self.title = title
def to_tex(self):
return "\\section{{{}}}".format(self.title)
class CvEntry():
def __init__(self, side_txt, title):
self.side_txt = side_txt
self.title = title
def to_tex(self):
return "\\cventry{{{}}}{{{}}}{{}}{{}}{{}}{{}}".format(self.side_txt, self.title)
class CvItem():
def __init__(self, side_txt, title):
self.side_txt = side_txt
self.title = title
def to_tex(self):
title = self.title
if title.startswith("-"):
title = "\\( \\circ \\)" + title[1:]
return "\\cvitem{{{}}}{{{}}}".format(self.side_txt, title)
class CurriculumVitae():
def __init__(self, language=None):
if language is None:
language = "english"
self.language = language
self.name = ("John", "Doe")
self.title = None
self.content = None
def from_markdown(self, markdown_src):
self.content = []
lines = markdown_src.splitlines()
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("# "):
line = line.removeprefix("# ")
self.name = line.split(" ", 1)
i += 1
# The first non-empty line after the name is the title
while lines[i] == "":
i += 1
self.title = lines[i]
i += 1
continue
if line.startswith("![]"):
self.photo = line.split("(")[1].split(")")[0]
i += 1
continue
if line.startswith("`email`"):
self.email = personal_info_from_md_line(line)
i += 1
continue
if line.startswith("`homepage`"):
self.homepage = personal_info_from_md_line(line)
i += 1
continue
if line.startswith("`linkedin`"):
self.linkedin = personal_info_from_md_line(line)
i += 1
continue
if line.startswith("`github`"):
self.github = personal_info_from_md_line(line)
i += 1
continue
if line.startswith("## "):
self.content.append(CvSection(line.removeprefix("## ")))
i += 1
continue
if line.startswith("### "):
txt = line.removeprefix("### ")
side_text, title = split_cv_line(txt)
self.content.append(CvEntry(side_text, title))
i += 1
continue
if line.strip() != "":
side_text, title = split_cv_line(line)
self.content.append(CvItem(side_text, title))
i += 1
continue
i += 1
def personal_data_to_tex(self):
tex_out = []
tex_out.append("\\name {{{}}}{{{}}}".format(self.name[0], self.name[1]))
if self.title is not None:
tex_out.append("\\title{{{}}}".format(self.title))
if self.photo is not None:
tex_out.append("\\photo[80pt][0pt]{{{}}}".format(self.photo))
if self.email is not None:
tex_out.append("\\email{{{}}}".format(self.email))
if self.homepage is not None:
tex_out.append("\\homepage{{{}}}".format(self.homepage))
if self.linkedin is not None:
tex_out.append("\\social[linkedin]{{{}}}".format(self.linkedin))
if self.github is not None:
tex_out.append("\\social[github]{{{}}}".format(self.github))
tex_out = "\n".join(tex_out)
return tex_out
def content_to_tex(self):
content_lines = [item.to_tex() for item in self.content]
return "\n".join(content_lines)
def to_tex(self):
tex_template = open("cv_template.tex", "rt").read()
tex_template = tex_template.replace("$language", self.language)
tex_template = tex_template.replace("$personal_data", self.personal_data_to_tex())
tex_template = tex_template.replace("$content", self.content_to_tex())
return tex_template
if __name__ == "__main__":
src_filename = sys.argv[1]
if len(sys.argv) > 2:
language = sys.argv[2]
else:
language = None
cv = CurriculumVitae(language)
cv.from_markdown(open(src_filename, "rt").read())
open(os.path.splitext(src_filename)[0] + ".tex", "wt").write(cv.to_tex())