-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-cheatsheet.py
89 lines (83 loc) · 1.76 KB
/
generate-cheatsheet.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
import re
from pathlib import Path
import utils
out_file = Path('index.html')
prefix = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Kangxi Radicals 中文部首</title>
<style>
html {
max-width: 70ch;
padding: 3em 1em;
margin: auto;
line-height: 1.75;
font-size: 1.25em;
}
table {
border-collapse: collapse;
}
td {
border: 1px solid gray;
padding: 1em;
text-align: center;
}
td.char {
font-size: 3em;
padding: 0;
position: relative;
font-family: monospace;
}
.char-border {
border: 1px dashed gray;
}
.number {
font-size: 0.3em;
color: gray;
position: absolute;
top: 0;
left: 0;
padding: 0 0.2em;
}
</style>
</head>
<body>
<h1>Kangxi Radicals 中文部首</h1>
<p><a href="radical-anki-cards.txt">Download Anki cards</a></p>
<table>
<thead>
<th>Radical</th>
<th># Strokes</th>
<th>Meaning</th>
</thead>
<tbody>
"""
suffix = """\
</tbody>
</body>
</html>
"""
def get_rows():
for numbers, char, strokes, meaning in utils.get_cards(only_important=False):
if m := re.match(r'\|(.*)\|', char):
char = m.group(1)
border_class = 'char-border'
else:
border_class = ''
numbers_links = ', '.join(f'<a target="_blank" href="https://en.wikipedia.org/wiki/Radical_{n}">{n}</a>' for n in numbers)
yield f"""<tr>
<td class="char">
<div class="number">{numbers_links}</div>
<span class="{border_class}">{char}</span>
</td>
<td>{strokes}</td>
<td>{meaning}</td>
</tr>"""
with out_file.open('w') as fp:
fp.write(prefix)
for row in get_rows():
fp.write(row + '\n')
fp.write(suffix)