From d6df7e2d96575ebc85f887fd3b819330cdefad31 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Fri, 1 Mar 2013 15:04:01 -0500 Subject: [PATCH] Added comments to clean_message_html --- common/lib/capa/capa/responsetypes.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 9875c33f0d..5aa5b917d8 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1143,19 +1143,33 @@ def sympy_check2(): return correct_map def clean_message_html(self, msg): + + # If *msg* is an empty string, then the code below + # will return "". To avoid this, we first check + # that *msg* is a non-empty string. if msg: - # try to clean up message html + + # When we parse *msg* using etree, there needs to be a root + # element, so we wrap the *msg* text in tags msg = '' + msg + '' + + # Replace < characters msg = msg.replace('<', '<') - #msg = msg.replace('<','<') + + # Use etree to prettify the HTML 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 + + # Remove the tags we introduced earlier, so we're + # left with just the prettified message markup msg = re.sub('(?ms)(.*)', '\\1', msg) + # Strip leading and trailing whitespace return msg.strip() + + # If we start with an empty string, then return an empty string else: return ""