Skip to content

Commit 131ce85

Browse files
committed
potential fix for latex issues
1 parent 20c3d31 commit 131ce85

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/problem_bank_scripts/problem_bank_scripts.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from collections import defaultdict
1717
from shutil import copy2
1818
import re
19+
import codecs
1920

2021
## Parse Markdown
2122
from markdown_it import MarkdownIt # pip install markdown-it-py
@@ -73,13 +74,13 @@ def split_body_parts(num_parts,body_parts):
7374
pa = [i for i,j in enumerate(tokens) if j.tag=='h3']
7475

7576
try:
76-
parts_dict[part]['answer']['title'] = MDRenderer().render(tokens[pa[0]+1:pa[1]], mdit.options, env)
77+
parts_dict[part]['answer']['title'] = codecs.unicode_escape_decode(MDRenderer().render(tokens[pa[0]+1:pa[1]], mdit.options, env))[0]
7778
except IndexError:
7879
print("Check the heading levels, is there one that doesn't belong? Or is the heading level incorrect? For e.g., it should be ### Answer Section (this is not necessarily where the issue is).")
7980
raise
8081

81-
parts_dict[part]['content'] = MDRenderer().render(tokens[ptt[1]+1:pa[0]], mdit.options, env)
82-
parts_dict[part]['answer']['content'] = MDRenderer().render(tokens[pa[1]+1:], mdit.options, env)
82+
parts_dict[part]['content'] = codecs.unicode_escape_decode(MDRenderer().render(tokens[ptt[1]+1:pa[0]], mdit.options, env))[0]
83+
parts_dict[part]['answer']['content'] = codecs.unicode_escape_decode(MDRenderer().render(tokens[pa[1]+1:], mdit.options, env))[0]
8384

8485
return defdict_to_dict(parts_dict,{})
8586

@@ -96,7 +97,7 @@ def read_md_problem(filepath):
9697
- `num_parts` - Number of parts in the problem (integer).
9798
"""
9899

99-
mdtext = pathlib.Path(filepath).read_text()
100+
mdtext = pathlib.Path(filepath).read_text(encoding='utf8')
100101

101102
# Deal with YAML header
102103
header_text = mdtext.rsplit('---\n')[1]
@@ -157,7 +158,7 @@ def read_md_problem(filepath):
157158

158159
for k,v in blocks.items():
159160

160-
rendered_part = MDRenderer().render(tokens[v[0]:v[1]], mdit.options, env)
161+
rendered_part = codecs.unicode_escape_decode(MDRenderer().render(tokens[v[0]:v[1]], mdit.options, env))[0]
161162

162163
if k == 'title':
163164
body_parts['title'] = rendered_part
@@ -220,7 +221,7 @@ def write_info_json(output_path, parsed_question):
220221
"topic": \"""" + parsed_question['header']['topic'] + """",
221222
"tags": """ + json.dumps(parsed_question['header']['tags']) + """,
222223
"type": "v3"
223-
}""")
224+
}""",encoding='utf8')
224225

225226
def write_server_py(output_path,parsed_question):
226227
"""
@@ -237,7 +238,7 @@ def write_server_py(output_path,parsed_question):
237238
func = func.replace('read_csv("','read_csv(data["options"]["client_files_course_path"]+"/')
238239
func = func.replace('\n','\n ')
239240

240-
(output_path / "server.py").write_text(imp+'def generate(data):'+func)
241+
(output_path / "server.py").write_text(imp+'def generate(data):'+func,encoding='utf8')
241242

242243
def process_multiple_choice(part_name,parsed_question, data_dict):
243244
"""Processes markdown format multiple-choice questions and returns PL HTML
@@ -455,7 +456,7 @@ def process_question(input_file, output_path):
455456
question_html = pl_image_path(question_html)
456457

457458
# Write question.html file
458-
(output_path / "question.html").write_text(question_html) #,encoding='raw_unicode_escape')
459+
(output_path / "question.html").write_text(question_html,encoding='utf8') #,encoding='raw_unicode_escape')
459460

460461
### TODO solve the issue with the latex escape sequences, this is a workaround
461462
# with open((output_path / "question.html"), "w") as qfile:

0 commit comments

Comments
 (0)