forked from Peng-YM/LeetCode-Anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer_cn.py
135 lines (110 loc) · 3.31 KB
/
renderer_cn.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
import random
import re
from genanki import Model, Deck, Note, Package
from markdown import markdown
from database_cn import Problem
from utils import parser as conf
def random_id():
return random.randrange(1 << 30, 1 << 31)
def markdown_to_html(content: str):
# 删除无效的语言标志
content = re.sub(
pattern=r"\[.*?\]",
repl="",
string=content
)
content = re.sub(
pattern="```",
repl="``` ",
string=content
)
# replace the math symbol "$x$" to "\[$]x\[/$]" to make it compatible with mathjax
content = re.sub(
pattern=r"\$(.*?)\$",
repl=r"[$]\1[/$]",
string=content
)
result = markdown(content, extensions=['mdx_math', 'toc', 'fenced_code', 'tables'])
# also need to load the mathjax and toc extensions
return result
def code_to_html(source, language):
content = f"```{language}\n{source}\n```"
return markdown(content, extensions=['fenced_code'])
def get_anki_model():
with open(conf.get("Anki", "front"), 'r') as f:
front_template = f.read()
with open(conf.get("Anki", 'back'), 'r') as f:
back_template = f.read()
with open(conf.get("Anki", 'css'), 'r') as f:
css = f.read()
anki_model = Model(
model_id=1048217874,
name="LeetCode",
fields=[
{"name": "ID"},
{"name": "Title"},
{"name": "TitleSlug"},
{"name": "Difficulty"},
{"name": "Description"},
{"name": "Tags"},
{"name": "TagSlugs"},
{"name": "Solution"},
{"name": "Submission"}
],
templates=[
{
"name": "LeetCode",
"qfmt": front_template,
"afmt": back_template
}
],
css=css
)
return anki_model
def make_note(problem):
print(f"📓 Producing note for problem: {problem.title}...")
tags = ";".join([t.name for t in problem.tags])
tags_slug = ";".join([t.slug for t in problem.tags])
try:
solution = problem.solution.get()
except Exception:
solution = None
codes = []
for item in problem.submissions:
source = re.sub(r'(\\u[\s\S]{4})',lambda x:x.group(1).encode("utf-8").decode("unicode-escape"),item.source)
output = code_to_html(source, item.language)
codes.append(output)
submissions = "\n".join(codes)
note = Note(
model=get_anki_model(),
fields=[
str(problem.display_id),
problem.title,
problem.slug,
problem.level,
problem.description,
tags,
tags_slug,
markdown_to_html(solution.content) if solution else "",
submissions
],
guid=str(problem.display_id),
sort_field=str(problem.display_id),
tags=[t.slug for t in problem.tags]
)
return note
def render_anki():
problems = Problem.select().order_by(
Problem.display_id
)
anki_deck = Deck(
deck_id=random_id(),
name="LeetCodeCN"
)
for problem in problems:
note = make_note(problem)
anki_deck.add_note(note)
path = conf.get("Anki_CN", "output")
Package(anki_deck).write_to_file(path)
if __name__ == '__main__':
render_anki()