-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceEnvironments.py
125 lines (104 loc) · 3.97 KB
/
replaceEnvironments.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
#!/usr/bin/env python
import re
from typing import Any, Dict, List, Tuple
class replaceEnvironments:
def boxString(
box_type: str,
env_name: str,
thm_txt: str,
thm_counter: int,
) -> str:
"""
box_type: The type of box we want (must be in box_files)
env_name: The name of this environment (e.g. corollary)
thm_counter: The theorem/definition/example number
thm_txt: The content in the box
"""
box_files = {
"theorem": "thmbox.html",
"definition": "defbox.html",
"example": "exbox.html"
}
env_names = {
"theorem": "Theorem",
"corollary": "Corollary",
"lemma": "Lemma",
"claim": "Claim",
"proposition": "Proposition",
"conjecture": "Conjecture",
"definition": "Definition",
"example": "Example",
"exercise": "Exercise",
"problem": "Problem",
"question": "Question",
"remark": "Remark",
}
assert (box_type in box_files), "Invalid box type"
assert (env_name in env_names), "Invalid boxed environment name"
box_string = "{{% include {0} thmname='{1}' thmnum={2} thmtxt=\"{3}\" %}} <br />".format(
box_files[box_type],
env_names[env_name],
thm_counter,
thm_txt
)
return box_string
def replaceEnvironments(
env_type: str,
input_text: str,
thmcounter=1,
) -> str:
"""
env_type: Which environment is being replaced
input_text: Text cut from between \begin{env_type} and
\end{env_type} (but not including the delimiters
output: Text, where the environments have been
replaced with markdown equivalents
"""
#Environments that should be rendered within a red box
thmbox_envs = [
"theorem",
"corollary",
"lemma",
"claim",
"proposition",
"conjecture",
]
#Environments that should be rendered within a blue box
defbox_envs = [
"definition"
]
#Environments that should be rendered within a green box
exbox_envs = [
"example",
"exercise",
"problem",
"question",
"remark",
]
if env_type in thmbox_envs:
thmcounter += 1
input_text = input_text.strip()
env_output = replaceEnvironments.boxString("theorem", env_type, input_text, thmcounter)
elif env_type in defbox_envs:
input_text = input_text.strip()
thmcounter += 1
env_output = replaceEnvironments.boxString("definition", env_type, input_text, thmcounter)
elif env_type in exbox_envs:
input_text = input_text.strip()
thmcounter += 1
env_output = replaceEnvironments.boxString("example", env_type, input_text, themcounter)
elif env_type == "itemize":
#Bullet points
env_output = re.sub(r'\\item', r'-', input_text)
env_output = re.sub(r'\t','', env_output)
elif env_type == "enumerate":
#TODO need to make this number each thing rather than bullet points
env_output = re.sub(r'\\item', r'-', input_text)
elif env_type == "proof":
env_output = "*Proof*: "+input_text
else:
#TODO warn the user that they might've made a mistake or
#be missing desired functionality
print("I don't recognize the environment", env_type)
env_output = input_text
return env_output