Skip to content

Move backticks to code tags function to PBH #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/problem_bank_helpers/problem_bank_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sigfig
import pandas as pd
import importlib.resources
import re

## Load data and dictionaries

Expand Down Expand Up @@ -300,4 +301,34 @@ def ErrorCheck(subVariable, Variable, LaTeXstr, tolerance):
else:
return None
else:
return None
return None


def backticks_to_code_tags(data):
"""
Converts backticks to <code> tags, and code fences to <pl-code> tags for a filled PrairieLearn question data dictionary.
Note: this only makes replacements multiple choice (and other similar question) answer options.

Args:
html (str): The HTML to convert
"""
params = data["params"]
for param, param_data in params.items():
if not param.startswith("part"):
continue
for answer, answer_data in param_data.items():
if any(opt in answer for opt in {"ans", "statement", "option"}):
if isinstance(value := answer_data["value"], str):
value = re.sub(
r"```(?P<language>\w+)?(?(language)(\{(?P<highlighting>[\d,-]*)\})?|)(?P<Code>[^`]+)```",
r'<pl-code language="\g<language>" highlight-lines="\g<highlighting>">\g<Code></pl-code>',
value,
flags=re.MULTILINE,
)
value = value.replace(' language=""', "") # Remove empty language attributes
value = value.replace(
' highlight-lines=""', ""
) # Remove empty highlight-lines attributes
value = re.sub(r"(?<!\\)`(?P<Code>[^`]+)`", r"<code>\g<Code></code>", value)
value = value.replace("\\`", "`") # Replace escaped backticks
data["params"][param][answer]["value"] = value