-
Notifications
You must be signed in to change notification settings - Fork 36
/
macros.py
253 lines (219 loc) · 8.62 KB
/
macros.py
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
import csv
import datetime
import json
from html import escape
from pathlib import Path
from git.repo import Repo
repo = Repo(Path(__file__).parent, search_parent_directories=True)
tree = repo.heads.main.commit.tree
GITHUB_BASE = "https://github.com/vEnhance/web.evanchen.cc"
def handout_link(name: str, filename=None) -> str:
filename = filename or name
return (
f'<a href="handouts/{name}/{filename}.pdf">(pdf)</a>'
" "
f'<a href="handouts/{name}/{filename}.tex">(tex)</a>'
"<br>"
)
def handout_link_src(name: str, filename=None) -> str:
filename = filename or name
return (
f'<a href="handouts/{name}/{filename}.pdf">(pdf)</a>'
" "
f'<a href="{GITHUB_BASE}/tree/main/handouts/{name}">(git)</a>'
"<br>"
)
def page_footer(page: dict[str, str]) -> str:
input_name: str = page["url"].replace(".html", ".md")
input_path = Path("input") / input_name
try:
blob = tree[str(input_path)]
except KeyError:
return (
'<div class="text-muted">\n'
f'View the <a href="{GITHUB_BASE}">source repository</a>.\n'
"</div>\n"
'<div class="font-italic text-muted">\n'
"This hidden page not under public version control.\n"
"</div>"
)
else:
commit = next(repo.iter_commits(paths=blob.path, max_count=1))
last_update_dt = datetime.datetime.utcfromtimestamp(commit.committed_date)
last_update_str = last_update_dt.strftime("%a %-d %b %Y, %H:%M:%S UTC")
return (
"<div>\n"
f'<a href="{GITHUB_BASE}">Source repository (git)</a> •\n'
f'<a href="{GITHUB_BASE}/commits/main/{input_path}">Revision history</a> •\n'
f'<a href="{GITHUB_BASE}/edit/main/{input_path}">Suggest edit</a>\n'
"</div>\n"
f'<div class="text-muted">Updated {last_update_str} by\n'
f'<a href="{GITHUB_BASE}/commit/{commit.hexsha}"><code>{commit.hexsha[0:12]}</code></a>\n'
"</div>"
)
def get_twitch_table() -> str:
if (data_directory := Path.home() / "ProGamer/Writeups/").exists():
tsv_path = data_directory / "data.tsv"
json_path = data_directory / "urls.json"
elif (data_directory := Path("data/twitch/")).exists():
tsv_path = data_directory / "data.tsv"
json_path = data_directory / "urls.json"
else:
return "(Error: could not read Twitch table)"
with open(json_path) as f:
url_dict = json.load(f)
data = []
with open(tsv_path) as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
data.append(row)
data.sort(key=lambda row: (-int(row["N"]), row["Source"]))
out = ""
out += r'<table cellpadding="5">' + "\n"
out += (
r"<tr><th>Episode</th><th>Problem</th><th>PDF</th><th>Src</th><th>YouTube</th></tr>"
+ "\n"
)
for row in data:
n = row["N"]
key: str = row["Source"]
youtube_url = row["YouTube"]
if key.startswith("!"): # coding problem
pdf_url = None
src_type = "git"
if key.startswith("!AtCoder"):
pid = key[8:].strip().replace(" ", "")
contest = pid[:-1].lower()
src_url = f"https://github.com/vEnhance/evan-learns-ioi/tree/main/AtCoder/{pid}"
url = f"https://atcoder.jp/contests/{contest}/tasks/{contest}_{pid[-1].lower()}"
elif key.startswith("!CodeForces"):
pid = key[11:].strip()
src_url = f"https://github.com/vEnhance/evan-learns-ioi/tree/main/CodeForces/{pid}"
url = f"https://codeforces.com/contest/{pid[:-1]}/problem/{pid[-1]}"
elif key.startswith("!Kattis"):
pid = key[7:].strip().lower().replace(" ", "")
src_url = f"https://github.com/vEnhance/evan-learns-ioi/tree/main/Kattis/{pid}"
url = f"https://open.kattis.com/problems/{pid}"
else:
url = None
src_url = None
key = "💻" + key[1:]
else:
url = url_dict.get(key, None)
basename = "Ep%03d" % int(n) + "-"
basename += key.replace(" ", "-").replace("/", "-").replace(".", "-")
basename += "-Solution"
basename_tex = basename + ".tex"
basename_pdf = basename + ".pdf"
filename = Path.home() / "youtube-tex" / basename_tex
if filename.exists():
pdf_url = f"twitch/{basename_pdf}"
src_url = f"twitch/{basename_tex}"
src_type = "tex"
else:
pdf_url = None
src_url = None
src_type = None
out += "<tr>"
out += "<td>Ep %s</td>" % n
if url is not None:
out += '<td><a href="%s">%s</a></td>' % (url, key)
else:
out += "<td>%s</td>" % key
if pdf_url is not None:
out += f'<td><a href="{escape(pdf_url)}">(pdf)</a></td>'
else:
out += "<td></td>"
if src_url is not None:
out += f'<td><a href="{escape(src_url)}">({src_type})</a></td>'
else:
out += "<td></td>"
if youtube_url:
out += f'<td><a href="{escape(youtube_url)}">(video)</a></td>'
else:
out += "<td></td>"
out += "</tr>" + "\n"
out += "</table>"
return out
def get_card_trick() -> str:
option_string = r"""<option value="{value}">{value}</option>"""
ranks = [
"...",
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
]
suits = ["...", "♣Clubs", "♦Diamonds", "♥Hearts", "♠Spades"]
out = ""
for n in range(0, 4):
out += f"<h2>Card {n+1}</h2>" + "\n"
out += r'<div class="container trick-container">' + "\n"
out += f'<select class="form-select rank-select" id="rank-{n}">' + "\n"
for r in ranks:
out += option_string.format(type="rank", name=n, value=r) + "\n"
out += r"</select>" + "\n"
out += f'<select class="form-select suit-select" id="suit-{n}">' + "\n"
for s in suits:
out += option_string.format(type="suit", name=n, value=s) + "\n"
out += r"</select>" + "\n"
out += r"</div>" + "\n"
out += r"<hr />" + "\n"
out += r"<h2>Card 5</h2>" + "\n"
out += r'<h1 id="answer">...</h1>' + "\n"
out += r'<button type="button" class="btn" id="trick-button"></button>' + "\n"
out += r'<script type="text/javascript" src="/static/trick.min.js"></script>' + "\n"
out += (
r'<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>'
+ "\n"
)
out += r'<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" />'
return out
def faq(label: str, question: str) -> str:
return (
f'<a id="{label}" href="#{label}" style="color:#004824;">{label}.</a> {question}'
f'<a href="#{label}" class="hash-link">#</a>'
)
def hl(link: str, text: str) -> str:
return f'<a id="{link}"></a>{text}<a href="#{link}" class="hash-link">#</a>'
def tshirt(year: str, alt: str | None = None, ext=".png") -> str:
location = f"static/mop/shirts/{year}{ext}"
if alt is None:
alt = f"{year}."
return (
f'<a href="{location}" title="{alt}" class="tshirt-link">'
f'<img src="{location}" alt="{alt}" class="tshirt" /></a>'
)
def clickable_selfie_image(basename: str, alt: str) -> str:
image_path = "static/headshots/" + basename + "-full.jpg"
thumb_path = "static/headshots/" + basename + "-thumb.jpg"
return f'<a href="{image_path}"><img src="{thumb_path}" alt="{alt}" /></a>'
def clickable_asy_image(basename: str) -> str:
image_path = "static/asy-gallery/" + basename + ".png"
thumb_path = image_path
alt = basename
return f'<a href="{image_path}"><img src="{thumb_path}" alt="{alt}" /></a>'
def chooser_link(dirname: str, header_str: str) -> str:
s = r'<div class="chooser empty-chooser"></div>' + "\n"
paths = list(Path(dirname).glob("*.md"))
assert len(paths) > 0, Path(dirname)
paths.sort()
paths.reverse()
for p in paths:
s += (
f'<div data-year="{p.stem}" data-header="{header_str % p.stem}" class="hidden" markdown="block">'
+ "\n"
)
with open(p) as f:
s += "".join(f.readlines())
s += "</div>" + "\n"
return s