diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 897f922e93..698ec41a0a 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -201,7 +201,17 @@ class LoncapaResponse(object): if response_msg: response_msg_div = etree.SubElement(tree, 'div') response_msg_div.set("class", "response_message") - response_msg_div.text = response_msg + + # If the response message can be represented as an XHTML tree, + # create the tree and append it to the message
+ try: + response_tree = etree.XML(response_msg) + response_msg_div.append(response_tree) + + # Otherwise, assume that the message is text (not XHTML) + # and insert it as the text of the message
+ except: + response_msg_div.text = response_msg return tree @@ -1069,20 +1079,7 @@ def sympy_check2(): if 'ok' in ret: correct = ['correct'] * len(idset) if ret['ok'] else ['incorrect'] * len(idset) msg = ret.get('msg', None) - - if 1: - # try to clean up message html - msg = '' + msg + '' - msg = msg.replace('<', '<') - #msg = msg.replace('<','<') - msg = etree.tostring(fromstring_bs(msg, convertEntities=None), - pretty_print=True) - #msg = etree.tostring(fromstring_bs(msg),pretty_print=True) - msg = msg.replace(' ', '') - #msg = re.sub('(.*)','\\1',msg,flags=re.M|re.DOTALL) # python 2.7 - msg = re.sub('(?ms)(.*)', '\\1', msg) - - messages[0] = msg + messages[0] = self.clean_message_html(msg) # Another kind of dictionary the check function can return has @@ -1101,7 +1098,8 @@ def sympy_check2(): messages = [] for input_dict in input_list: correct.append('correct' if input_dict['ok'] else 'incorrect') - messages.append(input_dict['msg'] if 'msg' in input_dict else None) + msg = self.clean_message_html(input_dict['msg']) if 'msg' in input_dict else None + messages.append(msg) # Otherwise, we do not recognize the dictionary # Raise an exception @@ -1117,13 +1115,30 @@ def sympy_check2(): # build map giving "correct"ness of the answer(s) correct_map = CorrectMap() + + overall_message = self.clean_message_html(overall_message) correct_map.set_overall_message(overall_message) + for k in range(len(idset)): npoints = self.maxpoints[idset[k]] if correct[k] == 'correct' else 0 correct_map.set(idset[k], correct[k], msg=messages[k], npoints=npoints) return correct_map + def clean_message_html(self, msg): + # try to clean up message html + msg = '' + msg + '' + msg = msg.replace('<', '<') + #msg = msg.replace('<','<') + msg = etree.tostring(fromstring_bs(msg, convertEntities=None), + pretty_print=True) + #msg = etree.tostring(fromstring_bs(msg),pretty_print=True) + msg = msg.replace(' ', '') + #msg = re.sub('(.*)','\\1',msg,flags=re.M|re.DOTALL) # python 2.7 + msg = re.sub('(?ms)(.*)', '\\1', msg) + + return msg.strip() + def get_answers(self): ''' Give correct answer expected for this response. diff --git a/common/lib/capa/capa/tests/test_html_render.py b/common/lib/capa/capa/tests/test_html_render.py index aa5312aa14..257e63b611 100644 --- a/common/lib/capa/capa/tests/test_html_render.py +++ b/common/lib/capa/capa/tests/test_html_render.py @@ -135,7 +135,7 @@ class CapaHtmlRenderTest(unittest.TestCase): # CustomResponse script that sets an overall_message script=textwrap.dedent(""" def check_func(*args): - return {'overall_message': 'Test message', + return {'overall_message': '

Test message

', 'input_list': [ {'ok': True, 'msg': '' } ] } """) @@ -159,6 +159,11 @@ class CapaHtmlRenderTest(unittest.TestCase): self.assertEqual(msg_div_element.tag, "div") self.assertEqual(msg_div_element.get('class'), "response_message") + # Expect that the
contains our message (as part of the XML tree) + msg_p_element = msg_div_element.find('p') + self.assertEqual(msg_p_element.tag, "p") + self.assertEqual(msg_p_element.text, "Test message") + def test_substitute_python_vars(self): # Generate some XML with Python variables defined in a script diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 451e6ed14b..538ee6fe50 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -712,7 +712,7 @@ class CustomResponseTest(ResponseTest): msg = correct_map.get_msg('1_2_1') self.assertEqual(correctness, 'correct') - self.assertEqual(msg, "Message text\n") + self.assertEqual(msg, "Message text") # Incorrect answer input_dict = {'1_2_1': '0'} @@ -722,7 +722,7 @@ class CustomResponseTest(ResponseTest): msg = correct_map.get_msg('1_2_1') self.assertEqual(correctness, 'incorrect') - self.assertEqual(msg, "Message text\n") + self.assertEqual(msg, "Message text") def test_function_code_multiple_input_no_msg(self):