File size: 1,897 Bytes
b22f922
74164b2
b22f922
 
 
74164b2
b22f922
74164b2
 
 
 
b22f922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74164b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pypandoc
import typst
## stdlib
import subprocess
import json
import os
from typing import Optional
from string import Template

from datetime import datetime


def file_to_html(file_path: str) -> str:
    return pypandoc.convert_file(file_path, "html")


def extract_url(url: str) -> Optional[str]:
    cmd = f"""shot-scraper javascript -b firefox \
      "{url}" "
    async () => {{
      const readability = await import('https://cdn.skypack.dev/@mozilla/readability');
      return (new readability.Readability(document)).parse();
    }}"
"""
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    try:
        result.check_returncode()
    except:
        raise Exception(
            f"Please try copy-paste as input. Failed to extract content from url: {url}. Error: {result.stderr}"
        )
    result = json.loads(result.stdout)
    try:
        return result["textContent"]
    except:
        raise Exception(
            f"Please try copy-paste as input. Failed to extract content from: {url}. Didn't find content from given URL!"
        )

def date():
    current_date = datetime.now()
    return current_date.strftime(
        f"%B %d{'th' if 4 <= current_date.day <= 20 or 24 <= current_date.day <= 30 else ['st', 'nd', 'rd'][current_date.day % 10 - 1]} , %Y")

def typst_escape(s):
    return s.replace('@','\@').replace('#','\#')

def compile_pdf(context: dict, tmpl_path: str, output_path="/tmp/cover_letter.pdf"):
    with open(tmpl_path, "r", encoding='utf8') as f:
        tmpl = Template(f.read())
    context = {k: typst_escape(v) for k, v in context.items()}
    context.update({'date_string': date()})
    letter_typ = tmpl.safe_substitute(context)
    with open('letter.typ', 'w', encoding='utf8') as f:
        f.write(letter_typ)
    typst.compile('letter.typ', output=output_path)
    os.remove('letter.typ')
    return output_path