-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding latex parsing to main package and adding method for new quiz m…
…ult choice questions
- Loading branch information
1 parent
c38be58
commit 220f679
Showing
2 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import urllib.parse | ||
import re | ||
|
||
|
||
def convalllatex(text): | ||
"""Convert all instances of LaTeX in a string to Canvas-compatible images | ||
Args: | ||
text (str): | ||
Input text | ||
Returns: | ||
str: | ||
Output text | ||
""" | ||
|
||
p = re.compile(r"\${1,2}(.*?)\${1,2}") | ||
|
||
return p.sub(convlatex, text) | ||
|
||
|
||
def convlatex(texstr): | ||
"""Convert input latex string to Canvas's img html | ||
Args: | ||
texstr (str or re.Match): | ||
LaTeX-formatted equation string | ||
Returns: | ||
str: | ||
Canvas-style image string | ||
""" | ||
|
||
# handle case where input is re.Match | ||
if isinstance(texstr, re.Match): | ||
texstr = texstr.groups()[0] | ||
|
||
# replace problematic commands int LaTeX | ||
texsubdict = { | ||
r"\\textrm": "", | ||
} | ||
for key, val in texsubdict.items(): | ||
texstr = re.sub(key, val, texstr) | ||
|
||
convstr = urllib.parse.quote(urllib.parse.quote(texstr)) | ||
qtxt = ( | ||
f"""<img class="equation_image" title="{texstr}" """ | ||
f"""src="/equation_images/{convstr}?scale=1" """ | ||
f"""alt="LaTeX: {texstr}" """ | ||
f""" data-equation-content="{texstr}" data-ignore-a11y-check="">""" | ||
) | ||
|
||
return qtxt |