"""Build the public CV from CV.md. Requires reportlab 4.4.9 and Cyrillic fonts."""
import argparse
import re
from html import escape
from pathlib import Path
from urllib.parse import quote, urljoin
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import PageBreak, Paragraph, SimpleDocTemplate
ROOT = Path(__file__).resolve().parents[1]
BASE = "https://git.flowmasters.ru/portfolio/engineering-portfolio/src/branch/main/"
def font_path(bold=False):
filename = "Arial Bold.ttf" if bold else "Arial.ttf"
candidates = [
Path("/System/Library/Fonts/Supplemental") / filename,
Path("/usr/share/fonts/truetype/dejavu")
/ ("DejaVuSans-Bold.ttf" if bold else "DejaVuSans.ttf"),
]
return next((p for p in candidates if p.is_file()), None)
def inline(text):
# PDF uses ASCII hyphens consistently; content otherwise comes from CV.md.
text = text.replace("—", "-").replace("–", "-").replace("\u2011", "-")
text = escape(text)
def link(match):
label, target = match.groups()
if target.startswith("output/pdf/"):
return label
href = target if "://" in target else urljoin(BASE, quote(target, safe="/#"))
return f'{label}'
text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", link, text)
return re.sub(r"\*\*(.+?)\*\*", r"\1", text)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--font", type=Path, default=font_path())
parser.add_argument("--bold-font", type=Path, default=font_path(True))
parser.add_argument(
"--output", type=Path, default=ROOT / "output/pdf/Vikentiy_Zherebov_CV.pdf"
)
args = parser.parse_args()
if not args.font or not args.bold_font:
parser.error("Pass --font and --bold-font with Cyrillic TrueType fonts")
pdfmetrics.registerFont(TTFont("CV", str(args.font)))
pdfmetrics.registerFont(TTFont("CVBold", str(args.bold_font)))
pdfmetrics.registerFontFamily("CV", normal="CV", bold="CVBold")
base = ParagraphStyle(
"Body", fontName="CV", fontSize=9.7, leading=13.3,
textColor=colors.HexColor("#273440"), spaceAfter=7, alignment=TA_LEFT,
)
styles = {
1: ParagraphStyle("Name", parent=base, fontName="CVBold", fontSize=24,
leading=29, spaceAfter=9),
2: ParagraphStyle("Section", parent=base, fontName="CVBold", fontSize=12.5,
leading=16, spaceBefore=11, spaceAfter=7,
textColor=colors.HexColor("#155a68"), keepWithNext=True),
3: ParagraphStyle("Role", parent=base, fontName="CVBold", fontSize=10.3,
leading=14, spaceBefore=5, spaceAfter=5, keepWithNext=True),
}
bullet = ParagraphStyle("Bullet", parent=base, leftIndent=10, firstLineIndent=-8)
story = []
paragraph = []
is_bullet = False
def flush():
nonlocal is_bullet
if paragraph:
text = inline(" ".join(paragraph))
story.append(Paragraph(("- " if is_bullet else "") + text,
bullet if is_bullet else base))
paragraph.clear()
is_bullet = False
for line in (ROOT / "CV.md").read_text().splitlines():
if not line.strip():
flush()
elif match := re.match(r"^(#{1,3}) (.+)$", line):
flush()
if match[2] == "Избранные инженерные проекты":
story.append(PageBreak())
story.append(Paragraph(inline(match[2]), styles[len(match[1])]))
elif line.startswith("- "):
flush()
is_bullet = True
paragraph.append(line[2:])
elif line.startswith(" ") and paragraph:
paragraph.append(line.strip())
else:
if is_bullet:
flush()
paragraph.append(line)
flush()
args.output.parent.mkdir(parents=True, exist_ok=True)
def footer(canvas, doc):
canvas.saveState()
canvas.setStrokeColor(colors.HexColor("#d5dde2"))
canvas.line(42, 35, A4[0] - 42, 35)
canvas.setFont("CV", 8)
canvas.setFillColor(colors.HexColor("#667681"))
canvas.drawString(42, 22, "Викентий Жеребов | автоматизация и прикладной AI")
canvas.drawRightString(A4[0] - 42, 22, str(doc.page))
canvas.restoreState()
doc = SimpleDocTemplate(
str(args.output), pagesize=A4, leftMargin=42, rightMargin=42,
topMargin=34, bottomMargin=48, title="Викентий Жеребов - резюме",
author="Викентий Жеребов",
)
doc.build(story, onFirstPage=footer, onLaterPages=footer)
print(args.output)
if __name__ == "__main__":
main()