-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
322 lines (265 loc) · 9.19 KB
/
Copy pathtemplate.py
File metadata and controls
322 lines (265 loc) · 9.19 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
import subprocess
import json
from pathlib import Path
from typing import Optional
from pydantic import BaseModel, Field
from dataclasses import asdict
class ContactInfo(BaseModel):
"""Contact information for the applicant."""
name: str
email: str
phone: str
address: str
linkedin: str = ""
github: str = ""
role: str = ""
class CompanyInfo(BaseModel):
"""Company and position information."""
company: str
position: str
recipient: str
source: str = ""
company_news: str = ""
class CoverLetterContent(BaseModel):
"""Main content sections of the cover letter."""
opening_salutation: str
opening_paragraph: str
second_paragraph: str
closing_paragraph: str
closing_salutation: str
signoff_paragraph: str
class CoverLetterData(BaseModel):
"""Complete cover letter structured data."""
contact: ContactInfo
company: CompanyInfo
content: CoverLetterContent
email_content: str = Field(description="A professional email body to accompany the application")
def to_dict(self) -> dict:
"""Convert to dictionary for LaTeX template."""
return {
**self.contact.model_dump(),
**self.company.model_dump(),
**self.content.model_dump(),
}
def to_json(self, filepath: str = "cover_letter_data.json") -> Path:
"""Save structured data to JSON file."""
path = Path(filepath)
with open(path, 'w', encoding='utf-8') as f:
json.dump(self.model_dump(), f, indent=2, ensure_ascii=False)
print(f"✓ Data saved to JSON: {path}")
return path
@classmethod
def from_json(cls, filepath: str) -> "CoverLetterData":
"""Load structured data from JSON file."""
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
return cls(
contact=ContactInfo(**data['contact']),
company=CompanyInfo(**data['company']),
content=CoverLetterContent(**data['content'])
)
class CoverLetterGenerator:
"""Generate LaTeX cover letters and compile to PDF using local pdflatex."""
LATEX_TEMPLATE = r"""
\documentclass[10pt,letter]{letter}
\usepackage[utf8]{inputenc}
%%================================
%% FROM TLCcoverletter.sty
%%================================
\usepackage[T1]{fontenc}
\usepackage[default,semibold]{sourcesanspro}
\usepackage[12pt]{moresize}
\usepackage{anyfontsize}
\usepackage{csquotes}
\usepackage[margin=.5in]{geometry}
\usepackage{xcolor}
\definecolor{highlight}{RGB}{61, 90, 128}
\usepackage{hyperref}
\hypersetup{colorlinks=true,urlcolor=highlight}
\newcommand{\bold}[1]{ {\bfseries #1}}
\pagenumbering{gobble}
\usepackage{standalone}
\usepackage{import}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{fancyhdr}
%%====================
%% CONTACT INFORMATION (used by header)
%%====================
\def\name{%(name)s}
\signature{\name}
\address{%(address)s \\ %(phone)s \\ \detokenize{%(email)s}}
\def\phone{%(phone)s}
\def\email{%(email)s}
\def\LinkedIn{%(linkedin)s}
\def\github{%(github)s}
\def\role{%(role)s}
%%====================
%% Company Info
%%====================
\def\hm{}
\def\position{%(position)s}
\def\company{%(company)s}
\def\source{%(source)s}
\def\companynews{%(company_news)s}
%%==================
%% Header file (_header)
%%==================
\fancypagestyle{plain}{%%
\fancyhf{}
\lhead{\phone \\
\href{mailto:\email}{\email}}
\chead{%%
\centering {\Large \bold\name} \\
{\color{highlight} \large{\role}}}
\rhead{
%%Portfolio: \href{https://yassinekader.dev}{yassinekader.dev}\\
\href{https://github.com/\github}{github.com/\github} \\
\href{https://www.linkedin.com/in/\LinkedIn}{linkedin.com/in/\LinkedIn}}
\renewcommand{\headrulewidth}{2pt}%%
\renewcommand{\headrule}{\hbox to\headwidth{%%
\color{highlight}\leaders\hrule height \headrulewidth\hfill}}
}
\pagestyle{plain}
\setlength{\headheight}{90pt}
\setlength{\headsep}{0pt}
\makeatletter
\let\ps@empty\ps@plain
\let\ps@firstpage\ps@plain
\makeatother
%%==================
%% Document begins
%%==================
\begin{document}
\begin{letter}{%(recipient)s}
\opening{%(opening_salutation)s}
\setlength\parindent{.5in}
%(opening_paragraph)s
%(second_paragraph)s
%(closing_paragraph)s
%(signoff_paragraph)s
\closing{%(closing_salutation)s}
\end{letter}
\end{document}
"""
def __init__(self):
"""Initialize the cover letter generator."""
self.output_dir = Path("cover_letters")
self.output_dir.mkdir(exist_ok=True)
def _escape_latex_safe(self, text: str) -> str:
"""Escape special LaTeX characters (but NOT '%')."""
replacements = {
'&': r'\&',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\textasciicircum{}',
'%': r'\%'
}
for char, replacement in replacements.items():
text = text.replace(char, replacement)
return text
def generate_latex(self, cover_letter: CoverLetterData) -> str:
"""
Generate LaTeX content from structured data.
Args:
cover_letter: CoverLetterData object
Returns:
Formatted LaTeX string
"""
data = cover_letter.to_dict()
# safe_keys = set(cover_letter.content.model_dump().keys())
# print(safe_keys)
# safe_keys.add('github')
# safe_keys.add('linkedin')
# safe_keys.add('email')
safe_keys = {'github', 'linkedin', 'email'}
processed_data = {}
for key, value in data.items():
if not isinstance(value, str):
processed_data[key] = value
continue
temp_value = value
if key not in safe_keys:
temp_value = self._escape_latex_safe(temp_value)
processed_data[key] = temp_value.replace('%', '%%')
return self.LATEX_TEMPLATE % processed_data
def save_latex(self, latex_content: str, filename: str) -> Path:
"""Save LaTeX content to a file."""
filepath = self.output_dir / f"{filename}.tex"
with open(filepath, 'w', encoding='utf-8') as f:
f.write(latex_content)
print(f"✓ LaTeX file saved: {filepath.resolve()}")
return filepath
def compile_to_pdf_pdflatex(self, latex_file: Path) -> Optional[Path]:
"""
Compile LaTeX to PDF using local pdflatex.
Args:
latex_file: Path to the .tex file
Returns:
Path to the generated PDF, or None if failed
"""
try:
print(f"Compiling {latex_file.name} to PDF using pdflatex...")
result = subprocess.run(
[
'pdflatex',
'-interaction=nonstopmode',
'-output-directory', str(self.output_dir),
str(latex_file)
],
capture_output=True,
text=True,
timeout=30,
encoding='utf-8',
errors='ignore'
)
pdf_path = self.output_dir / f"{latex_file.stem}.pdf"
if result.returncode == 0 and pdf_path.exists():
print(f"✓ PDF generated successfully: {pdf_path}")
subprocess.run(
['pdflatex', '-interaction=nonstopmode', '-output-directory', str(self.output_dir), str(latex_file)],
capture_output=True, text=True, timeout=30, encoding='utf-8', errors='ignore'
)
return pdf_path
else:
print("✗ PDF generation failed")
print("--- pdflatex Error Output ---")
print(result.stdout)
print("-----------------------------")
return None
except FileNotFoundError:
print("✗ pdflatex not found. Please install a LaTeX distribution:")
print(" Windows: https://miktex.org/download")
print(" macOS: brew install --cask mactex")
print(" Linux: sudo apt-get install texlive-latex-base texlive-latex-extra")
return None
except subprocess.TimeoutExpired:
print("✗ pdflatex compilation timed out")
return None
except Exception as e:
print(f"✗ An unexpected error occurred: {e}")
return None
def create(self, cover_letter: CoverLetterData, filename: str,
export_pdf: bool = True) -> dict:
"""
Create a cover letter from structured data.
Args:
cover_letter: CoverLetterData object
filename: Base filename for output files
export_pdf: Whether to generate PDF
Returns:
Dictionary with paths to generated files
"""
results = {}
latex_content = self.generate_latex(cover_letter)
latex_path = self.save_latex(latex_content, filename)
results['latex'] = str(latex_path)
if export_pdf:
pdf_path = self.compile_to_pdf_pdflatex(latex_path)
if pdf_path:
results['pdf'] = str(pdf_path.resolve())
return results