-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-tex.ts
More file actions
159 lines (136 loc) · 4.73 KB
/
Copy pathrender-tex.ts
File metadata and controls
159 lines (136 loc) · 4.73 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
import { resolveResume } from "../data/resolve.js";
import type {
CV,
ResumeProfile,
PersonalInfo,
Experience,
Education,
Skill,
Project,
Certification,
} from "../data/schema.js";
function escapeLatex(text: string): string {
// Order matters: escape backslash last to avoid double-escaping braces
return text
.replace(/[&%$#_]/g, match => `\\${match}`)
.replace(/~/g, "\\textasciitilde{}")
.replace(/\^/g, "\\textasciicircum{}")
.replace(/\\/g, "\\textbackslash{}")
.replace(/[{}]/g, match => `\\${match}`);
}
function formatDate(date: string | null): string {
if (!date) return "Present";
const [year, month] = date.split("-");
const months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
return month ? `${months[parseInt(month) - 1]}. ${year}` : year;
}
function stripProtocol(url: string): string {
return url.replace(/^https?:\/\//, "");
}
export function renderHeader(personal: PersonalInfo): string {
const e = escapeLatex;
return `\\begin{center}
\\textbf{\\Huge \\scshape ${e(personal.name)}} \\\\ \\vspace{1pt}
\\small ${e(personal.phone)} $|$
\\href{mailto:${personal.email}}{\\underline{${e(personal.email)}}} $|$
\\href{${personal.links.linkedin}}{\\underline{${stripProtocol(personal.links.linkedin)}}} $|$
\\href{${personal.links.github}}{\\underline{${stripProtocol(personal.links.github)}}}
\\end{center}`;
}
export function renderExperience(experience: Experience[]): string {
if (experience.length === 0) return "";
const e = escapeLatex;
const items = experience.map(job => {
const highlights = job.highlights
.map(h => ` \\resumeItem{${e(h)}}`)
.join("\n");
return ` \\resumeSubheading
{${e(job.title)}}{${formatDate(job.start)} -- ${formatDate(job.end)}}
{${e(job.company)}}{${e(job.location)}}
\\resumeItemListStart
${highlights}
\\resumeItemListEnd`;
}).join("\n");
return `\\section{Experience}
\\resumeSubHeadingListStart
${items}
\\resumeSubHeadingListEnd`;
}
export function renderEducation(education: Education[]): string {
if (education.length === 0) return "";
const e = escapeLatex;
const items = education.map(edu =>
` \\resumeSubheading
{${e(edu.institution)}}{${formatDate(edu.start)} -- ${formatDate(edu.end)}}
{${e(edu.degree)} in ${e(edu.field)}}{}`
).join("\n");
return `\\section{Education}
\\resumeSubHeadingListStart
${items}
\\resumeSubHeadingListEnd`;
}
export function renderSkills(skills: Skill[]): string {
if (skills.length === 0) return "";
const e = escapeLatex;
const rows = skills
.map(s => ` \\textbf{${e(s.category)}}{: ${e(s.items.join(", "))}} \\\\`)
.join("\n");
return `\\section{Skills}
\\begin{itemize}[leftmargin=0.15in, label={}]
\\small{\\item{
${rows}
}}
\\end{itemize}`;
}
export function renderProjects(projects: Project[]): string {
if (projects.length === 0) return "";
const e = escapeLatex;
const items = projects.map(proj => {
const highlights = proj.highlights
.map(h => ` \\resumeItem{${e(h)}}`)
.join("\n");
return ` \\resumeProjectHeading
{\\textbf{${e(proj.name)}} $|$ \\href{${proj.url}}{\\underline{${stripProtocol(proj.url)}}}}{}
\\resumeItemListStart
${highlights}
\\resumeItemListEnd`;
}).join("\n");
return `\\section{Projects}
\\resumeSubHeadingListStart
${items}
\\resumeSubHeadingListEnd`;
}
export function renderCertifications(certifications: Certification[]): string {
if (certifications.length === 0) return "";
const e = escapeLatex;
const items = certifications
.map(c => ` \\resumeItem{\\href{${c.url}}{\\underline{${e(c.name)}}} -- ${e(c.issuer)} (${formatDate(c.date)})}`)
.join("\n");
return `\\section{Certifications}
\\begin{itemize}[leftmargin=0.15in, label={}]
\\small{\\item{
${items}
}}
\\end{itemize}`;
}
/** Accepts template content as a string (not a file path) for testability.
* The CLI wrapper (render-tex-cli.ts) handles file I/O and validation.
* Design decision: validation is done at the boundary (CLI entry point),
* not inside renderTex. This keeps renderTex pure and testable. */
export function renderTex(
cv: CV,
profile: ResumeProfile,
templateContent: string,
): string {
const resolved = resolveResume(cv, profile);
return templateContent
.replace("%%HEADER%%", renderHeader(resolved.personal))
.replace("%%EXPERIENCE%%", renderExperience(resolved.experience))
.replace("%%EDUCATION%%", renderEducation(resolved.education))
.replace("%%SKILLS%%", renderSkills(resolved.skills))
.replace("%%PROJECTS%%", renderProjects(resolved.projects))
.replace("%%CERTIFICATIONS%%", renderCertifications(resolved.certifications));
}