-
Notifications
You must be signed in to change notification settings - Fork 0
/
resumeRenderer.ts
472 lines (396 loc) · 11.5 KB
/
resumeRenderer.ts
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import { match } from 'ts-pattern';
import {
BareResume,
ResumeBasics,
ResumeEducation,
ResumeExperience,
ResumeProject,
ResumeSkill,
} from './types';
type RenderConfig = {
lineHeight: number;
};
const indent = (num: number): string => `\n${' '.repeat(num)}`;
const renderResume = (lineHeight: number, sections: string[]): string =>
`\
%-------------------------
% Resume<T> in Latex
% Author : Andrew Leung
% Based off of: https://github.com/sb2nov/resume
% License : MIT
%------------------------
\\documentclass[letterpaper, 11pt]{article}
\\usepackage{latexsym}
\\usepackage[empty]{fullpage}
\\usepackage{titlesec}
\\usepackage{marvosym}
\\usepackage[usenames,dvipsnames]{color}
\\usepackage{verbatim}
\\usepackage{enumitem}
\\usepackage[hidelinks]{hyperref}
\\usepackage{fancyhdr}
\\usepackage[english]{babel}
\\usepackage{tabularx}
\\usepackage{setspace}
\\input{glyphtounicode}
%----------FONT OPTIONS----------
% sans-serif
% \\usepackage[sfdefault]{FiraSans}
% \\usepackage[sfdefault]{roboto}
% \\usepackage[sfdefault]{noto-sans}
% \\usepackage[default]{sourcesanspro}
\\usepackage[sfdefault]{carlito}
% serif
% \\usepackage{CormorantGaramond}
% \\usepackage{charter}
\\pagestyle{fancy}
\\fancyhf{} % clear all header and footer fields
\\fancyfoot{}
\\renewcommand{\\headrulewidth}{0pt}
\\renewcommand{\\footrulewidth}{0pt}
% Adjust margins
\\addtolength{\\oddsidemargin}{-0.5in}
\\addtolength{\\evensidemargin}{-0.5in}
\\addtolength{\\textwidth}{1in}
\\addtolength{\\topmargin}{-0.5in}
\\addtolength{\\textheight}{1.0in}
\\urlstyle{same}
\\raggedbottom
\\raggedright
\\setlength{\\tabcolsep}{0in}
% Sections formatting
\\titleformat{\\section}{
\\vspace{-4pt}\\scshape\\raggedright\\large
}{}{0em}{}[\\color{black}\\titlerule \\vspace{-5pt}]
% Ensure that generate pdf is machine readable/ATS parsable
\\pdfgentounicode=1
%-------------------------
% Custom commands
\\newcommand{\\resumeItem}[1]{
\\item\\small{
{#1 \\vspace{-2pt}}
}
}
\\newcommand{\\resumeSubheading}[4]{
\\vspace{-2pt}\\item
\\begin{tabular*}{1\\textwidth}[t]{l@{\\extracolsep{\\fill}}r}
\\small\\textbf{#1}\\space\\space\\small#2 | \\small#3 & \\small#4\\\\
\\end{tabular*}\\vspace{-6pt}
}
\\newcommand{\\educationSubHeading}[5]{
\\vspace{-2pt}\\item
\\begin{tabular*}{1\\textwidth}[t]{l@{\\extracolsep{\\fill}}r}
\\small\\textbf{#1}\\space\\space\\small#3, \\small#4 | \\small#5 & \\small#2
\\end{tabular*}
}
\\newcommand{\\resumeJobSubheading}[3]{
\\vspace{-2pt}\\item
\\begin{tabular*}{1\\textwidth}[t]{l@{\\extracolsep{\\fill}}r}
\\small\\textbf{#1}, \\small#3 & #2\\\\
\\end{tabular*}
}
\\newcommand{\\resumeSubSubheading}[2]{
\\item
\\begin{tabular*}{1\\textwidth}{l@{\\extracolsep{\\fill}}r}
\\small#1 & \\small #2\\\\
\\end{tabular*}
}
\\newcommand{\\resumeProjectHeading}[3]{
\\vspace{-2pt}\\item
\\begin{tabular*}{1\\textwidth}{l@{\\extracolsep{\\fill}}r}
\\small#1\\space\\space\\small#3 & \\small#2\\vspace{-1pt}\\\\
\\end{tabular*}\\vspace{-5pt}
}
\\newcommand{\\resumeSubItem}[1]{\\resumeItem{#1}\\vspace{-4pt}}
\\renewcommand\\labelitemii{$\\vcenter{\\hbox{\\tiny$\\bullet$}}$}
\\newcommand{\\resumeSubHeadingListStart}{\\begin{itemize}[leftmargin=0in, label={}]}
\\newcommand{\\resumeSubHeadingListEnd}{\\end{itemize}}
\\newcommand{\\resumeItemListStart}{\\begin{itemize}[leftmargin=1.5em]}
\\newcommand{\\resumeItemListEnd}{\\end{itemize}\\vspace{-8pt}}
\\setstretch{${lineHeight}}
%-------------------------------------------
%%%%%% RESUME STARTS HERE %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\\begin{document}
${sections.join('')}
%-------------------------------------------
\\end{document}
`;
const renderHeading = ({
basics: {
name,
email,
phone,
url,
profiles,
location: { city, region },
},
}: {
basics: ResumeBasics;
}): string => {
const profileUrls = profiles.map((profile) => profile.url);
const sites = profileUrls.map((site) => site.slice('https://'.length));
const personalUrl = url.slice('https://'.length);
return `
\\begin{center}
\\begin{minipage}[b]{0.5\\textwidth}
\\raggedright
\\textbf{\\Huge \\scshape ${name}} \\vspace{0pt} \\\\
${email}
\\end{minipage}%
\\begin{minipage}[b]{0.5\\textwidth}
\\raggedleft
\\small ${personalUrl}\\\\${sites.join('\\\\')} \\vspace{0pt}
\\end{minipage}%
\\end{center}
`;
};
const renderMonthDate = (dateString: string): string => {
if (dateString === 'Present') {
return dateString;
}
const date = new Date(dateString);
date.setDate(date.getDate() + 1);
const month = date.toLocaleString('default', { month: 'short' });
return `${month} ${date.getFullYear()}`;
};
const renderEducation = ({
education,
basics: {
location: { city, region },
},
}: { education: ResumeEducation[] } & { basics: ResumeBasics }): string => {
if (education.length === 0) {
return '';
}
const { institution, area, studyType, startDate, endDate, score } =
education[0];
// TODO: The resume.json standard doesn't include location for education...
return `
\\section{Education}
\\resumeSubHeadingListStart
\\educationSubHeading
{${institution}}{${renderMonthDate(startDate)} -- ${renderMonthDate(
endDate,
)}}
{${studyType} ${area}}{${score} GPA}{${city}, ${region}}
\\resumeSubHeadingListEnd
`;
};
const renderSkills = ({ skills }: { skills: ResumeSkill[] }): string => {
if (skills.length === 0) {
return '';
}
const renderCategory = ({
name,
keywords,
}: (typeof skills)[number]): string => {
return `\\textbf{${name.replace('&', '\\&')}}{: ${keywords.join(
', ',
)}}\\\\`;
};
// TODO: Figure out a sane way to work out proper composable indentation.
const categories = skills.map(renderCategory).join(indent(6));
return `
\\section{Skills}
\\begin{itemize}[leftmargin=0in, label={}]
\\small{\\item{
${categories}
}}
\\end{itemize}\\vspace{-16pt}
`;
};
const renderExperience = ({
name,
location,
position,
startDate,
endDate,
highlights,
}: ResumeExperience): string => {
const renderHighlight = (highlight: string) => `\\resumeItem{${highlight}}`;
return `\\resumeSubheading{${name}}{${position}}{${location}}{${renderMonthDate(startDate)} -- ${renderMonthDate(endDate)}}
\\resumeItemListStart
${highlights.map(renderHighlight).join(indent(8))}
\\resumeItemListEnd`;
};
const renderExperiences = ({ work }: { work: ResumeExperience[] }): string => {
if (work.length < 1) {
return '';
}
const experiences = work
.sort((a, b) => Date.parse(b.startDate) - Date.parse(a.startDate))
.map(renderExperience)
.join(`\n${indent(4)}`);
return `
\\section{Work Experience}
\\resumeSubHeadingListStart
${experiences}
\\resumeSubHeadingListEnd
`;
};
const renderProject = ({
name,
keywords,
highlights,
startDate,
endDate,
url,
}: ResumeProject): string => {
const renderHighlight = (highlight: string): string =>
`\\resumeItem{${highlight}}`;
const renderedHighlights = highlights.map(renderHighlight).join(indent(8));
const renderedUrl =
url === undefined ? 'Closed Source' : url.slice('https://'.length);
const dateRange =
startDate === endDate
? startDate
: endDate === undefined
? `${startDate} -- Present`
: `${startDate} -- ${endDate}`;
return `\\resumeProjectHeading
{\\textbf{${name}}}{${dateRange}}{${renderedUrl}}
\\resumeItemListStart
${renderedHighlights}
\\resumeItemListEnd`;
};
const renderProjects = ({
projects,
}: {
projects: ResumeProject[];
}): string => {
if (projects.length < 1) {
return '';
}
return `
\\section{Projects}
\\resumeSubHeadingListStart
${projects.map(renderProject).join(indent(4))}
\\resumeSubHeadingListEnd
`;
};
const renderSection = (section: ResumeSection, resume: BareResume): string =>
match(section)
.returnType<string>()
.with('education', () => renderEducation(resume))
.with('experiences', () => renderExperiences(resume))
.with('projects', () => renderProjects(resume))
.with('skills', () => renderSkills(resume))
.exhaustive();
type Filters = {
include?: string[];
exclude?: string[];
after?: Date;
};
type ArbitrarilyNested =
| string
| string[]
| {
[key: string]: ArbitrarilyNested;
};
type ResumeSection = 'skills' | 'experiences' | 'projects' | 'education';
type Filterable = { name: string; startDate: string };
type CheckError = { path: string; error: string };
class ResumeRenderer {
private resume: BareResume;
private shouldRunChecks: boolean = false;
private experiencesConfig?: Filters;
private projectsConfig?: Filters;
constructor(resume: BareResume) {
this.resume = { ...resume };
}
render(config: RenderConfig, sections: ResumeSection[]): string {
if (this.shouldRunChecks) this.runChecks();
this.applyFilters();
const body = sections.map((section) => renderSection(section, this.resume));
return renderResume(config.lineHeight, [
renderHeading(this.resume),
...body,
]);
}
experiences(ic: Filters): ResumeRenderer {
this.experiencesConfig = ic;
return this;
}
projects(ic: Filters): ResumeRenderer {
this.projectsConfig = ic;
return this;
}
withChecks() {
this.shouldRunChecks = true;
return this;
}
private findErrors(
node: ArbitrarilyNested,
errors: CheckError[],
path: string[],
cb: (text: string) => string | null,
) {
if (typeof node === 'string') {
const maybeError = cb(node);
if (maybeError !== null)
errors.push({ path: path.join('.'), error: maybeError });
} else if (Array.isArray(node)) {
node.forEach((v, i) => {
path.push(i.toString());
this.findErrors(v, errors, path, cb);
path.pop();
});
} else {
Object.entries(node).forEach(([key, value]) => {
path.push(key);
this.findErrors(value, errors, path, cb);
path.pop();
});
}
}
private check(cb: (text: string) => string | null) {
const errors: CheckError[] = [];
const path: string[] = [];
this.findErrors(
this.resume as unknown as ArbitrarilyNested,
errors,
path,
cb,
);
return errors;
}
private filter<T extends Filterable>(
filterables: T[],
filters: Filters,
): T[] {
const { include, exclude, after } = filters;
return filterables.filter(
({ name, startDate }) =>
(include === undefined || include.some((n) => name.includes(n))) &&
(exclude === undefined || !exclude.some((n) => name.includes(n))) &&
(after === undefined ||
(startDate !== undefined && after <= new Date(startDate))),
);
}
private applyFilters() {
if (this.resume.projects && this.projectsConfig) {
this.resume.projects = this.filter(
this.resume.projects,
this.projectsConfig,
);
}
if (this.resume.work && this.experiencesConfig) {
this.resume.work = this.filter(this.resume.work, this.experiencesConfig);
}
}
private runChecks() {
const errors = this.check((text: string) => {
if (text.match(/[^\\][\$\%]/)) {
return 'Unescaped dollar sign ($)';
}
return null;
});
// TODO: Return warnings instead of logging them.
if (errors.length > 0) {
console.warn('\nWarnings:');
console.warn(errors);
console.warn();
}
}
}
export const resume = (resume: BareResume) => new ResumeRenderer(resume);