16
16
from collections import defaultdict
17
17
from shutil import copy2
18
18
import re
19
+ import codecs
19
20
20
21
## Parse Markdown
21
22
from markdown_it import MarkdownIt # pip install markdown-it-py
@@ -73,13 +74,13 @@ def split_body_parts(num_parts,body_parts):
73
74
pa = [i for i ,j in enumerate (tokens ) if j .tag == 'h3' ]
74
75
75
76
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 ]
77
78
except IndexError :
78
79
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)." )
79
80
raise
80
81
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 ]
83
84
84
85
return defdict_to_dict (parts_dict ,{})
85
86
@@ -96,7 +97,7 @@ def read_md_problem(filepath):
96
97
- `num_parts` - Number of parts in the problem (integer).
97
98
"""
98
99
99
- mdtext = pathlib .Path (filepath ).read_text ()
100
+ mdtext = pathlib .Path (filepath ).read_text (encoding = 'utf8' )
100
101
101
102
# Deal with YAML header
102
103
header_text = mdtext .rsplit ('---\n ' )[1 ]
@@ -157,7 +158,7 @@ def read_md_problem(filepath):
157
158
158
159
for k ,v in blocks .items ():
159
160
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 ]
161
162
162
163
if k == 'title' :
163
164
body_parts ['title' ] = rendered_part
@@ -220,7 +221,7 @@ def write_info_json(output_path, parsed_question):
220
221
"topic": \" """ + parsed_question ['header' ]['topic' ] + """",
221
222
"tags": """ + json .dumps (parsed_question ['header' ]['tags' ]) + """,
222
223
"type": "v3"
223
- }""" )
224
+ }""" , encoding = 'utf8' )
224
225
225
226
def write_server_py (output_path ,parsed_question ):
226
227
"""
@@ -237,7 +238,7 @@ def write_server_py(output_path,parsed_question):
237
238
func = func .replace ('read_csv("' ,'read_csv(data["options"]["client_files_course_path"]+"/' )
238
239
func = func .replace ('\n ' ,'\n ' )
239
240
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' )
241
242
242
243
def process_multiple_choice (part_name ,parsed_question , data_dict ):
243
244
"""Processes markdown format multiple-choice questions and returns PL HTML
@@ -455,7 +456,7 @@ def process_question(input_file, output_path):
455
456
question_html = pl_image_path (question_html )
456
457
457
458
# 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')
459
460
460
461
### TODO solve the issue with the latex escape sequences, this is a workaround
461
462
# with open((output_path / "question.html"), "w") as qfile:
0 commit comments