-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.py
220 lines (186 loc) · 7.34 KB
/
build.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
# Automate building demo packages from source code
import json
import os
import re
import sys
from pathlib import Path
from random import randrange
import genanki
import requests
FONTS = {
"minimal": "Inter",
"nord": "Rubik",
"dracula": "Source Sans Pro",
}
# Field contents for each note type
NOTE_FIELDS = {
"basic": [
"What is <b>Anki</b>?",
"<b>Anki</b> is a <u>free and open-source</u> flashcard program using <i>spaced repetition</i>, a technique from cognitive science for fast and long-lasting memorization.<br><br><img src='https://upload.wikimedia.org/wikipedia/commons/9/9a/Anki_2.1.6_screenshot.png'><br>Anki 2.1.6 screenshot (<a href='https://en.wikipedia.org/wiki/Anki_(software)'>https://en.wikipedia.org/wiki/Anki_(software)</a>)",
],
"basic_reverse": [
"What is <b>Anki</b>?",
"<b>Anki</b> is a <u>free and open-source</u> flashcard program using <i>spaced repetition</i>, a technique from cognitive science for fast and long-lasting memorization.<br><br><img src='https://upload.wikimedia.org/wikipedia/commons/9/9a/Anki_2.1.6_screenshot.png'><br>Anki 2.1.6 screenshot (<a href='https://en.wikipedia.org/wiki/Anki_(software)'>https://en.wikipedia.org/wiki/Anki_(software)</a>)",
],
"cloze": [
"<b>Anki</b> is a <u>free and open-source</u> {{c1::flashcard}} program using <i>spaced repetition</i>, a technique from cognitive science for fast and long-lasting memorization.<br><br><img src='https://upload.wikimedia.org/wikipedia/commons/9/9a/Anki_2.1.6_screenshot.png'>",
"Anki 2.1.6 screenshot (<a href='https://en.wikipedia.org/wiki/Anki_(software)'>https://en.wikipedia.org/wiki/Anki_(software)</a>)",
],
}
# Store the root path for future use
root = Path(f"{__file__}/../..").resolve()
semver_regex = re.compile(
r"(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)"
)
# Get latest version release number
try:
last_rel_ver = requests.get(
"https://api.github.com/repos/pranavdeshai/anki-prettify/releases/latest"
).json()["tag_name"]
except KeyError:
with open(
root / "src" / "templates" / "default" / "basic" / "basic-front.html"
) as f:
s = f.read()
last_rel_ver = semver_regex.search(s).group() # type:ignore
# Get the new version number
try:
new_ver = sys.argv[1]
if re.fullmatch(semver_regex, new_ver) is None:
print(
f"Warning: Invalid version number. Using latest release version: {last_rel_ver}"
)
else:
print(f"Building with version {new_ver}")
except IndexError:
print(
f"Warning: New version not provided. Using latest release version: {last_rel_ver}"
)
new_ver = last_rel_ver # Default to last release version if not provided
# Update version number in SCSS files
if new_ver != last_rel_ver:
for x in root.glob("**/scss/*.scss"):
with x.open("r+") as f:
s = f.read()
s = re.sub(semver_regex, new_ver, s)
f.seek(0)
f.write(s)
print("Updated version in SCSS files")
# Compile SCSS to CSS
os.system(
f"sass --no-source-map {str(root / 'src' / 'styles' / 'scss')}:{str(root / 'src' / 'styles' / 'css')}"
)
print("Compiled SCSS to CSS")
# Update genanki IDs
with open(root / "tools" / "ids.json", "r+") as ids_file:
ids = json.load(ids_file)
ids_file.seek(0)
for i in root.glob("**/css/*.css"):
theme = i.stem
if theme not in ids:
ids[theme] = {}
print(f"Added new theme to IDs: {theme}")
for j in root.glob("**/templates/default/*"):
if j.is_dir():
notetype = j.stem
if notetype not in ids[theme]:
ids[theme][notetype] = {
"model_id": randrange(1 << 30, 1 << 31),
"deck_id": randrange(1 << 30, 1 << 31),
"note_id": randrange(1 << 30, 1 << 31),
}
print(f"Added new notetype to IDs: {notetype}")
json.dump(ids, ids_file, indent=4)
print("Updated ids.json")
# Generate deck packages
decks = {}
for t in ids:
for n in ids[t]:
with open(
(root / "src" / "templates" / "default" / n / f"{n}-front.html"),
"r+",
) as f1, open(
(root / "src" / "templates" / "default" / n / f"{n}-back.html"), "r+"
) as f2, open((root / "src" / "styles" / "css" / f"{t}.css")) as f3:
front_html = f1.read()
back_html = f2.read()
css = f3.read()
# Update version number in templates
if new_ver != last_rel_ver:
front_html = re.sub(semver_regex, new_ver, front_html)
back_html = re.sub(semver_regex, new_ver, back_html)
f1.seek(0)
f2.seek(0)
f1.write(front_html)
f2.write(back_html)
if n == "basic_reverse":
# Basic reverse requires two card templates
templates = [
{
"name": "Card 1",
"qfmt": front_html,
"afmt": back_html,
},
{
"name": "Card 2",
"qfmt": front_html.replace("{{edit:Front}}", "{{edit:Back}}"),
"afmt": back_html.replace(
"{{edit:Back}}", "{{edit:Front}}"
).replace("{{edit:Front}}", "{{edit:Back}}", 1),
},
]
else:
templates = [
{
"name": "Card 1",
"qfmt": front_html,
"afmt": back_html,
}
]
model_fields = [
{
# Cloze note types have different field names
"name": "Text" if n == "cloze" else "Front",
"font": FONTS[t],
},
{
"name": "Back Extra" if n == "cloze" else "Back",
"font": FONTS[t],
},
]
model = genanki.Model(
model_id=ids[t][n]["model_id"],
name=f"prettify-{t}-{n}",
fields=model_fields,
templates=templates,
css=css,
model_type=genanki.Model.CLOZE
if n == "cloze"
else genanki.Model.FRONT_BACK,
)
deck = genanki.Deck(
ids[t][n]["deck_id"],
f"Prettify::{t.capitalize()}::{n.capitalize().replace('_',' ')}",
)
note = genanki.Note(
guid=ids[t][n]["note_id"],
fields=NOTE_FIELDS[n],
model=model,
tags=["prettify", f"prettify::{t}", f"prettify::{t}::{n}"],
)
deck.add_model(model)
deck.add_note(note)
# Note type-wise packages
genanki.Package(deck).write_to_file(
root / "themes" / t / "notetypes" / f"prettify-{t}-{n}.apkg"
)
if t not in decks:
decks[t] = []
decks[t].append(deck)
# Theme-wise packages
genanki.Package(decks[t]).write_to_file(root / "themes" / t / f"prettify-{t}.apkg")
# Master package with all the themes
genanki.Package([d for x in decks.values() for d in x]).write_to_file(
root / "prettify.apkg"
)
print("Generated all packages successfully")