-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit.py
executable file
·136 lines (113 loc) · 5.75 KB
/
orbit.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
#!/usr/bin/env python3
import subprocess
import sys
import pathlib
import re
from anki.collection import Collection
import util
def html_to_markdown(html_string):
try:
# markdown_strict is needed here so that things like double quotes
# aren't unnecessarily backslash-escaped. wrap=none is for Orbit;
# Orbit will faithfully translate newlines appearing in the source
# so we need to remove superfluous newlines created by pandoc's
# markdown output.
p = subprocess.run(["pandoc", "-f", "html", "-t", "markdown_strict", "--wrap=none"],
input=html_string.encode('utf-8'), check=True,
capture_output=True)
return p.stdout.decode('utf-8').replace('"', """).strip()
except subprocess.CalledProcessError as e:
print("Error running pandoc:",
"error code:", e.returncode,
"error message:", e.stderr.decode("utf-8"), file=sys.stderr)
sys.exit()
with open("docs/orbit/index.html", "w") as f:
f.write(f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<link rel="stylesheet" href="../base.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="">
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&family=Source+Serif+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
<title>Orbit index - GEB Typogenetics/DNA/RNA Flashcards</title>
</head>
<body>
{util.navbar(1)}
<main>
<h2>Orbit index</h2>
<p>Pick a section below to collect its cards:</p>
""")
f.write("<ul>")
for section in util.sections:
if not util.section_map[section]:
f.write(f"<li>{section} (no cards for this section)</li>\n")
else:
num_cards = len(util.section_map[section])
f.write(f'<li><a href="{util.slugify(section)}">{section}</a> ({num_cards} card{"" if num_cards == 1 else "s"})</li>\n')
section_dir = f"docs/orbit/{util.slugify(section)}/"
pathlib.Path(section_dir).mkdir(exist_ok=True)
with open(section_dir + "index.html", "w") as g:
g.write(f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>{section} - GEB Typogenetics/DNA/RNA Flashcards</title>
<link rel="canonical" href="https://riceissa.github.io/geb-typogenetics-dna-rna/orbit/{util.slugify(section)}/">
<meta property="og:title" content="{section}">
<meta property="og:site_name" content="GEB Typogenetics/DNA/RNA flashcards">
<link rel="stylesheet" href="../../base.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="">
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&family=Source+Serif+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
<script type="module" src="https://js.withorbit.com/orbit-web-component.js"></script>
</head>
<body>
{util.navbar(2, optional=[("Back to Orbit index", "../")])}
<main>
<h2>{section}</h2>
<orbit-reviewarea color="yellow">
""")
for note in util.section_map[section]:
note_front = html_to_markdown(note["Front"])
note_back = html_to_markdown(note["Back"])
note_notes = html_to_markdown(note["Notes"])
question_attachments = ""
answer_attachments = ""
img_regex = r'!\[\]\(([^) ]+)\)'
match = re.findall(img_regex, note_front)
if len(match) > 1:
raise ValueError("Question cannot contain more than one image!")
if len(match) == 1:
image_url = "https://riceissa.github.io/geb-typogenetics-dna-rna/browse/" + match[0]
question_attachments = f'question-attachments="{image_url}"'
note_front = re.sub(img_regex, "", note_front).strip()
match = re.findall(img_regex, note_back)
if len(match) > 1:
raise ValueError("Answer cannot contain more than one image!")
if match:
image_url = "https://riceissa.github.io/geb-typogenetics-dna-rna/browse/" + match[0]
answer_attachments = f'answer-attachments="{image_url}"'
note_back = re.sub(img_regex, "", note_back).strip()
g.write(f"""<orbit-prompt
question="{note_front}"
{question_attachments}
{answer_attachments}
answer="{note_back}
{note_notes}"
></orbit-prompt>
""")
g.write("""
</orbit-reviewarea>
</main>
</body>
</html>
""")
f.write("</ul>")
f.write("""
</main>
</body>
</html>
""")